datasource.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. System.register(['lodash'], function (_export, _context) {
  3. "use strict";
  4. var _, _createClass, GenericDatasource;
  5. function _classCallCheck(instance, Constructor) {
  6. if (!(instance instanceof Constructor)) {
  7. throw new TypeError("Cannot call a class as a function");
  8. }
  9. }
  10. return {
  11. setters: [function (_lodash) {
  12. _ = _lodash.default;
  13. }],
  14. execute: function () {
  15. _createClass = function () {
  16. function defineProperties(target, props) {
  17. for (var i = 0; i < props.length; i++) {
  18. var descriptor = props[i];
  19. descriptor.enumerable = descriptor.enumerable || false;
  20. descriptor.configurable = true;
  21. if ("value" in descriptor) descriptor.writable = true;
  22. Object.defineProperty(target, descriptor.key, descriptor);
  23. }
  24. }
  25. return function (Constructor, protoProps, staticProps) {
  26. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  27. if (staticProps) defineProperties(Constructor, staticProps);
  28. return Constructor;
  29. };
  30. }();
  31. _export('GenericDatasource', GenericDatasource = function () {
  32. function GenericDatasource(instanceSettings, $q, backendSrv, templateSrv) {
  33. _classCallCheck(this, GenericDatasource);
  34. this.type = instanceSettings.type;
  35. this.url = instanceSettings.url;
  36. this.name = instanceSettings.name;
  37. this.q = $q;
  38. this.backendSrv = backendSrv;
  39. this.templateSrv = templateSrv;
  40. this.withCredentials = instanceSettings.withCredentials;
  41. this.headers = { 'Content-Type': 'application/json' };
  42. if (typeof instanceSettings.basicAuth === 'string' && instanceSettings.basicAuth.length > 0) {
  43. this.headers['Authorization'] = instanceSettings.basicAuth;
  44. }
  45. }
  46. _createClass(GenericDatasource, [{
  47. key: 'query',
  48. value: function query(options) {
  49. var query = this.buildQueryParameters(options);
  50. query.targets = query.targets.filter(function (t) {
  51. return !t.hide;
  52. });
  53. if (query.targets.length <= 0) {
  54. return this.q.when({ data: [] });
  55. }
  56. if (this.templateSrv.getAdhocFilters) {
  57. query.adhocFilters = this.templateSrv.getAdhocFilters(this.name);
  58. } else {
  59. query.adhocFilters = [];
  60. }
  61. return this.doRequest({
  62. url: this.url + '/query',
  63. data: query,
  64. method: 'POST'
  65. });
  66. }
  67. }, {
  68. key: 'testDatasource',
  69. value: function testDatasource() {
  70. return this.doRequest({
  71. url: this.url + '/',
  72. method: 'GET'
  73. }).then(function (response) {
  74. if (response.status === 200) {
  75. return { status: "success", message: "Data source is working", title: "Success" };
  76. }
  77. });
  78. }
  79. }, {
  80. key: 'annotationQuery',
  81. value: function annotationQuery(options) {
  82. var query = this.templateSrv.replace(options.annotation.query, {}, 'glob');
  83. var annotationQuery = {
  84. range: options.range,
  85. annotation: {
  86. name: options.annotation.name,
  87. datasource: options.annotation.datasource,
  88. enable: options.annotation.enable,
  89. iconColor: options.annotation.iconColor,
  90. query: query
  91. },
  92. rangeRaw: options.rangeRaw
  93. };
  94. return this.doRequest({
  95. url: this.url + '/annotations',
  96. method: 'POST',
  97. data: annotationQuery
  98. }).then(function (result) {
  99. return result.data;
  100. });
  101. }
  102. }, {
  103. key: 'metricFindQuery',
  104. value: function metricFindQuery(query) {
  105. var interpolated = {
  106. target: this.templateSrv.replace(query, null, 'regex')
  107. };
  108. return this.doRequest({
  109. url: this.url + '/search',
  110. data: interpolated,
  111. method: 'POST'
  112. }).then(this.mapToTextValue);
  113. }
  114. }, {
  115. key: 'mapToTextValue',
  116. value: function mapToTextValue(result) {
  117. return _.map(result.data, function (d, i) {
  118. if (d && d.text && d.value) {
  119. return { text: d.text, value: d.value };
  120. } else if (_.isObject(d)) {
  121. return { text: d, value: i };
  122. }
  123. return { text: d, value: d };
  124. });
  125. }
  126. }, {
  127. key: 'doRequest',
  128. value: function doRequest(options) {
  129. options.withCredentials = this.withCredentials;
  130. options.headers = this.headers;
  131. return this.backendSrv.datasourceRequest(options);
  132. }
  133. }, {
  134. key: 'buildQueryParameters',
  135. value: function buildQueryParameters(options) {
  136. var _this = this;
  137. //remove placeholder targets
  138. options.targets = _.filter(options.targets, function (target) {
  139. return target.target !== 'select metric';
  140. });
  141. var targets = _.map(options.targets, function (target) {
  142. return {
  143. target: _this.templateSrv.replace(target.target, options.scopedVars, 'regex'),
  144. refId: target.refId,
  145. hide: target.hide,
  146. type: target.type || 'timeserie'
  147. };
  148. });
  149. options.targets = targets;
  150. return options;
  151. }
  152. }, {
  153. key: 'getTagKeys',
  154. value: function getTagKeys(options) {
  155. var _this2 = this;
  156. return new Promise(function (resolve, reject) {
  157. _this2.doRequest({
  158. url: _this2.url + '/tag-keys',
  159. method: 'POST',
  160. data: options
  161. }).then(function (result) {
  162. return resolve(result.data);
  163. });
  164. });
  165. }
  166. }, {
  167. key: 'getTagValues',
  168. value: function getTagValues(options) {
  169. var _this3 = this;
  170. return new Promise(function (resolve, reject) {
  171. _this3.doRequest({
  172. url: _this3.url + '/tag-values',
  173. method: 'POST',
  174. data: options
  175. }).then(function (result) {
  176. return resolve(result.data);
  177. });
  178. });
  179. }
  180. }]);
  181. return GenericDatasource;
  182. }());
  183. _export('GenericDatasource', GenericDatasource);
  184. }
  185. };
  186. });
  187. //# sourceMappingURL=datasource.js.map