datasource.ts 4.6 KB

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