datasource.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return this.doRequest({
  57. url: this.url + '/query',
  58. data: query,
  59. method: 'POST'
  60. });
  61. }
  62. }, {
  63. key: 'testDatasource',
  64. value: function testDatasource() {
  65. return this.doRequest({
  66. url: this.url + '/',
  67. method: 'GET'
  68. }).then(function (response) {
  69. if (response.status === 200) {
  70. return { status: "success", message: "Data source is working", title: "Success" };
  71. }
  72. });
  73. }
  74. }, {
  75. key: 'annotationQuery',
  76. value: function annotationQuery(options) {
  77. var query = this.templateSrv.replace(options.annotation.query, {}, 'glob');
  78. var annotationQuery = {
  79. range: options.range,
  80. annotation: {
  81. name: options.annotation.name,
  82. datasource: options.annotation.datasource,
  83. enable: options.annotation.enable,
  84. iconColor: options.annotation.iconColor,
  85. query: query
  86. },
  87. rangeRaw: options.rangeRaw
  88. };
  89. return this.doRequest({
  90. url: this.url + '/annotations',
  91. method: 'POST',
  92. data: annotationQuery
  93. }).then(function (result) {
  94. return result.data;
  95. });
  96. }
  97. }, {
  98. key: 'metricFindQuery',
  99. value: function metricFindQuery(query) {
  100. var interpolated = {
  101. target: this.templateSrv.replace(query, null, 'regex')
  102. };
  103. return this.doRequest({
  104. url: this.url + '/search',
  105. data: interpolated,
  106. method: 'POST'
  107. }).then(this.mapToTextValue);
  108. }
  109. }, {
  110. key: 'mapToTextValue',
  111. value: function mapToTextValue(result) {
  112. return _.map(result.data, function (d, i) {
  113. if (d && d.text && d.value) {
  114. return { text: d.text, value: d.value };
  115. } else if (_.isObject(d)) {
  116. return { text: d, value: i };
  117. }
  118. return { text: d, value: d };
  119. });
  120. }
  121. }, {
  122. key: 'doRequest',
  123. value: function doRequest(options) {
  124. options.withCredentials = this.withCredentials;
  125. options.headers = this.headers;
  126. return this.backendSrv.datasourceRequest(options);
  127. }
  128. }, {
  129. key: 'buildQueryParameters',
  130. value: function buildQueryParameters(options) {
  131. var _this = this;
  132. //remove placeholder targets
  133. options.targets = _.filter(options.targets, function (target) {
  134. return target.target !== 'select metric';
  135. });
  136. var targets = _.map(options.targets, function (target) {
  137. return {
  138. target: _this.templateSrv.replace(target.target, options.scopedVars, 'regex'),
  139. refId: target.refId,
  140. hide: target.hide,
  141. type: target.type || 'timeserie'
  142. };
  143. });
  144. options.targets = targets;
  145. return options;
  146. }
  147. }]);
  148. return GenericDatasource;
  149. }());
  150. _export('GenericDatasource', GenericDatasource);
  151. }
  152. };
  153. });
  154. //# sourceMappingURL=datasource.js.map