datasource.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "'" + value.replace(/'/g, `''`) + "'";
  20. } else {
  21. return value;
  22. }
  23. }
  24. if (typeof value === 'number') {
  25. return value;
  26. }
  27. const quotedValues = _.map(value, function(val) {
  28. if (typeof value === 'number') {
  29. return value;
  30. }
  31. return "'" + val.replace(/'/g, `''`) + "'";
  32. });
  33. return quotedValues.join(',');
  34. }
  35. query(options) {
  36. const queries = _.filter(options.targets, target => {
  37. return target.hide !== true;
  38. }).map(target => {
  39. let queryModel = new MysqlQuery(target, this.templateSrv, options.scopedVars);
  40. return {
  41. refId: target.refId,
  42. intervalMs: options.intervalMs,
  43. maxDataPoints: options.maxDataPoints,
  44. datasourceId: this.id,
  45. rawSql: queryModel.render(this.interpolateVariable),
  46. format: target.format,
  47. };
  48. });
  49. if (queries.length === 0) {
  50. return this.$q.when({ data: [] });
  51. }
  52. return this.backendSrv
  53. .datasourceRequest({
  54. url: '/api/tsdb/query',
  55. method: 'POST',
  56. data: {
  57. from: options.range.from.valueOf().toString(),
  58. to: options.range.to.valueOf().toString(),
  59. queries: queries,
  60. },
  61. })
  62. .then(this.responseParser.processQueryResult);
  63. }
  64. annotationQuery(options) {
  65. if (!options.annotation.rawQuery) {
  66. return this.$q.reject({
  67. message: 'Query missing in annotation definition',
  68. });
  69. }
  70. const query = {
  71. refId: options.annotation.name,
  72. datasourceId: this.id,
  73. rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
  74. format: 'table',
  75. };
  76. return this.backendSrv
  77. .datasourceRequest({
  78. url: '/api/tsdb/query',
  79. method: 'POST',
  80. data: {
  81. from: options.range.from.valueOf().toString(),
  82. to: options.range.to.valueOf().toString(),
  83. queries: [query],
  84. },
  85. })
  86. .then(data => this.responseParser.transformAnnotationResponse(options, data));
  87. }
  88. metricFindQuery(query, optionalOptions) {
  89. let refId = 'tempvar';
  90. if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
  91. refId = optionalOptions.variable.name;
  92. }
  93. const interpolatedQuery = {
  94. refId: refId,
  95. datasourceId: this.id,
  96. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  97. format: 'table',
  98. };
  99. const data = {
  100. queries: [interpolatedQuery],
  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. }