datasource.ts 4.6 KB

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