datasource.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import _ from "lodash";
  2. export class GenericDatasource {
  3. constructor(instanceSettings, $q, backendSrv, templateSrv) {
  4. this.type = instanceSettings.type;
  5. this.url = instanceSettings.url;
  6. this.name = instanceSettings.name;
  7. this.q = $q;
  8. this.backendSrv = backendSrv;
  9. this.templateSrv = templateSrv;
  10. this.withCredentials = instanceSettings.withCredentials;
  11. this.headers = {'Content-Type': 'application/json'};
  12. if (typeof instanceSettings.basicAuth === 'string' && instanceSettings.basicAuth.length > 0) {
  13. this.headers['Authorization'] = instanceSettings.basicAuth;
  14. }
  15. }
  16. query(options) {
  17. var query = this.buildQueryParameters(options);
  18. query.targets = query.targets.filter(t => !t.hide);
  19. if (query.targets.length <= 0) {
  20. return this.q.when({data: []});
  21. }
  22. return this.doRequest({
  23. url: this.url + '/query',
  24. data: query,
  25. method: 'POST'
  26. });
  27. }
  28. testDatasource() {
  29. return this.doRequest({
  30. url: this.url + '/',
  31. method: 'GET',
  32. }).then(response => {
  33. if (response.status === 200) {
  34. return { status: "success", message: "Data source is working", title: "Success" };
  35. }
  36. });
  37. }
  38. annotationQuery(options) {
  39. var query = this.templateSrv.replace(options.annotation.query, {}, 'glob');
  40. var annotationQuery = {
  41. range: options.range,
  42. annotation: {
  43. name: options.annotation.name,
  44. datasource: options.annotation.datasource,
  45. enable: options.annotation.enable,
  46. iconColor: options.annotation.iconColor,
  47. query: query
  48. },
  49. rangeRaw: options.rangeRaw
  50. };
  51. return this.doRequest({
  52. url: this.url + '/annotations',
  53. method: 'POST',
  54. data: annotationQuery
  55. }).then(result => {
  56. return result.data;
  57. });
  58. }
  59. metricFindQuery(query) {
  60. var interpolated = {
  61. target: this.templateSrv.replace(query, null, 'regex')
  62. };
  63. return this.doRequest({
  64. url: this.url + '/search',
  65. data: interpolated,
  66. method: 'POST',
  67. }).then(this.mapToTextValue);
  68. }
  69. mapToTextValue(result) {
  70. return _.map(result.data, (d, i) => {
  71. if (d && d.text && d.value) {
  72. return { text: d.text, value: d.value };
  73. } else if (_.isObject(d)) {
  74. return { text: d, value: i};
  75. }
  76. return { text: d, value: d };
  77. });
  78. }
  79. doRequest(options) {
  80. options.withCredentials = this.withCredentials;
  81. options.headers = this.headers;
  82. return this.backendSrv.datasourceRequest(options);
  83. }
  84. buildQueryParameters(options) {
  85. //remove placeholder targets
  86. options.targets = _.filter(options.targets, target => {
  87. return target.target !== 'select metric';
  88. });
  89. var targets = _.map(options.targets, target => {
  90. return {
  91. target: this.templateSrv.replace(target.target, options.scopedVars, 'regex'),
  92. refId: target.refId,
  93. hide: target.hide,
  94. type: target.type || 'timeserie'
  95. };
  96. });
  97. options.targets = targets;
  98. return options;
  99. }
  100. }