datasource.js 5.3 KB

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