datasource.ts 4.4 KB

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