datasource.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import _ from 'lodash';
  2. import ResponseParser from './response_parser';
  3. export class MysqlDatasource {
  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({
  65. message: 'Query missing in annotation definition',
  66. });
  67. }
  68. const query = {
  69. refId: options.annotation.name,
  70. datasourceId: this.id,
  71. rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
  72. format: 'table',
  73. };
  74. return this.backendSrv
  75. .datasourceRequest({
  76. url: '/api/tsdb/query',
  77. method: 'POST',
  78. data: {
  79. from: options.range.from.valueOf().toString(),
  80. to: options.range.to.valueOf().toString(),
  81. queries: [query],
  82. },
  83. })
  84. .then(data => this.responseParser.transformAnnotationResponse(options, data));
  85. }
  86. metricFindQuery(query, optionalOptions) {
  87. let refId = 'tempvar';
  88. if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
  89. refId = optionalOptions.variable.name;
  90. }
  91. const interpolatedQuery = {
  92. refId: refId,
  93. datasourceId: this.id,
  94. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  95. format: 'table',
  96. };
  97. const data = {
  98. queries: [interpolatedQuery],
  99. };
  100. if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
  101. data['from'] = optionalOptions.range.from.valueOf().toString();
  102. }
  103. if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
  104. data['to'] = optionalOptions.range.to.valueOf().toString();
  105. }
  106. return this.backendSrv
  107. .datasourceRequest({
  108. url: '/api/tsdb/query',
  109. method: 'POST',
  110. data: data,
  111. })
  112. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  113. }
  114. testDatasource() {
  115. return this.backendSrv
  116. .datasourceRequest({
  117. url: '/api/tsdb/query',
  118. method: 'POST',
  119. data: {
  120. from: '5m',
  121. to: 'now',
  122. queries: [
  123. {
  124. refId: 'A',
  125. intervalMs: 1,
  126. maxDataPoints: 1,
  127. datasourceId: this.id,
  128. rawSql: 'SELECT 1',
  129. format: 'table',
  130. },
  131. ],
  132. },
  133. })
  134. .then(res => {
  135. return { status: 'success', message: 'Database Connection OK' };
  136. })
  137. .catch(err => {
  138. console.log(err);
  139. if (err.data && err.data.message) {
  140. return { status: 'error', message: err.data.message };
  141. } else {
  142. return { status: 'error', message: err.status };
  143. }
  144. });
  145. }
  146. }