datasource.ts 3.9 KB

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