datasource.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import _ from 'lodash';
  2. import ResponseParser from './response_parser';
  3. export class MssqlDatasource {
  4. id: any;
  5. name: any;
  6. responseParser: ResponseParser;
  7. interval: string;
  8. /** @ngInject */
  9. constructor(instanceSettings, private backendSrv, private $q, private templateSrv) {
  10. this.name = instanceSettings.name;
  11. this.id = instanceSettings.id;
  12. this.responseParser = new ResponseParser(this.$q);
  13. this.interval = (instanceSettings.jsonData || {}).timeInterval;
  14. }
  15. interpolateVariable(value, variable) {
  16. if (typeof value === 'string') {
  17. if (variable.multi || variable.includeAll) {
  18. return "'" + value.replace(/'/g, `''`) + "'";
  19. } else {
  20. return value;
  21. }
  22. }
  23. if (typeof value === 'number') {
  24. return value;
  25. }
  26. const quotedValues = _.map(value, function(val) {
  27. if (typeof value === 'number') {
  28. return value;
  29. }
  30. return "'" + val.replace(/'/g, `''`) + "'";
  31. });
  32. return quotedValues.join(',');
  33. }
  34. query(options) {
  35. const queries = _.filter(options.targets, item => {
  36. return item.hide !== true;
  37. }).map(item => {
  38. return {
  39. refId: item.refId,
  40. intervalMs: options.intervalMs,
  41. maxDataPoints: options.maxDataPoints,
  42. datasourceId: this.id,
  43. rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
  44. format: item.format,
  45. };
  46. });
  47. if (queries.length === 0) {
  48. return this.$q.when({ data: [] });
  49. }
  50. return this.backendSrv
  51. .datasourceRequest({
  52. url: '/api/tsdb/query',
  53. method: 'POST',
  54. data: {
  55. from: options.range.from.valueOf().toString(),
  56. to: options.range.to.valueOf().toString(),
  57. queries: queries,
  58. },
  59. })
  60. .then(this.responseParser.processQueryResult);
  61. }
  62. annotationQuery(options) {
  63. if (!options.annotation.rawQuery) {
  64. return this.$q.reject({ message: 'Query missing in annotation definition' });
  65. }
  66. const query = {
  67. refId: options.annotation.name,
  68. datasourceId: this.id,
  69. rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
  70. format: 'table',
  71. };
  72. return this.backendSrv
  73. .datasourceRequest({
  74. url: '/api/tsdb/query',
  75. method: 'POST',
  76. data: {
  77. from: options.range.from.valueOf().toString(),
  78. to: options.range.to.valueOf().toString(),
  79. queries: [query],
  80. },
  81. })
  82. .then(data => this.responseParser.transformAnnotationResponse(options, data));
  83. }
  84. metricFindQuery(query, optionalOptions) {
  85. let refId = 'tempvar';
  86. if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
  87. refId = optionalOptions.variable.name;
  88. }
  89. const interpolatedQuery = {
  90. refId: refId,
  91. datasourceId: this.id,
  92. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  93. format: 'table',
  94. };
  95. return this.backendSrv
  96. .datasourceRequest({
  97. url: '/api/tsdb/query',
  98. method: 'POST',
  99. data: {
  100. queries: [interpolatedQuery],
  101. },
  102. })
  103. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  104. }
  105. testDatasource() {
  106. return this.backendSrv
  107. .datasourceRequest({
  108. url: '/api/tsdb/query',
  109. method: 'POST',
  110. data: {
  111. from: '5m',
  112. to: 'now',
  113. queries: [
  114. {
  115. refId: 'A',
  116. intervalMs: 1,
  117. maxDataPoints: 1,
  118. datasourceId: this.id,
  119. rawSql: 'SELECT 1',
  120. format: 'table',
  121. },
  122. ],
  123. },
  124. })
  125. .then(res => {
  126. return { status: 'success', message: 'Database Connection OK' };
  127. })
  128. .catch(err => {
  129. console.log(err);
  130. if (err.data && err.data.message) {
  131. return { status: 'error', message: err.data.message };
  132. } else {
  133. return { status: 'error', message: err.status };
  134. }
  135. });
  136. }
  137. }