datasource.ts 4.5 KB

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