datasource.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import _ from 'lodash';
  2. import ResponseParser from './response_parser';
  3. export class MysqlDatasource {
  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({
  63. message: 'Query missing in annotation definition',
  64. });
  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 data = {
  96. queries: [interpolatedQuery],
  97. };
  98. if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
  99. data['from'] = optionalOptions.range.from.valueOf().toString();
  100. }
  101. if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
  102. data['to'] = optionalOptions.range.to.valueOf().toString();
  103. }
  104. return this.backendSrv
  105. .datasourceRequest({
  106. url: '/api/tsdb/query',
  107. method: 'POST',
  108. data: data,
  109. })
  110. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  111. }
  112. testDatasource() {
  113. return this.backendSrv
  114. .datasourceRequest({
  115. url: '/api/tsdb/query',
  116. method: 'POST',
  117. data: {
  118. from: '5m',
  119. to: 'now',
  120. queries: [
  121. {
  122. refId: 'A',
  123. intervalMs: 1,
  124. maxDataPoints: 1,
  125. datasourceId: this.id,
  126. rawSql: 'SELECT 1',
  127. format: 'table',
  128. },
  129. ],
  130. },
  131. })
  132. .then(res => {
  133. return { status: 'success', message: 'Database Connection OK' };
  134. })
  135. .catch(err => {
  136. console.log(err);
  137. if (err.data && err.data.message) {
  138. return { status: 'error', message: err.data.message };
  139. } else {
  140. return { status: 'error', message: err.status };
  141. }
  142. });
  143. }
  144. }