datasource.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, private timeSrv) {
  10. this.name = instanceSettings.name;
  11. this.id = instanceSettings.id;
  12. this.responseParser = new ResponseParser(this.$q);
  13. this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
  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, 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. const range = this.timeSrv.timeRange();
  96. const data = {
  97. queries: [interpolatedQuery],
  98. from: range.from.valueOf().toString(),
  99. to: range.to.valueOf().toString(),
  100. };
  101. return this.backendSrv
  102. .datasourceRequest({
  103. url: '/api/tsdb/query',
  104. method: 'POST',
  105. data: data,
  106. })
  107. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  108. }
  109. testDatasource() {
  110. return this.backendSrv
  111. .datasourceRequest({
  112. url: '/api/tsdb/query',
  113. method: 'POST',
  114. data: {
  115. from: '5m',
  116. to: 'now',
  117. queries: [
  118. {
  119. refId: 'A',
  120. intervalMs: 1,
  121. maxDataPoints: 1,
  122. datasourceId: this.id,
  123. rawSql: 'SELECT 1',
  124. format: 'table',
  125. },
  126. ],
  127. },
  128. })
  129. .then(res => {
  130. return { status: 'success', message: 'Database Connection OK' };
  131. })
  132. .catch(err => {
  133. console.log(err);
  134. if (err.data && err.data.message) {
  135. return { status: 'error', message: err.data.message };
  136. } else {
  137. return { status: 'error', message: err.status };
  138. }
  139. });
  140. }
  141. }