datasource.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import ResponseParser from './response_parser';
  4. export class MysqlDatasource {
  5. id: any;
  6. name: any;
  7. responseParser: ResponseParser;
  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. }
  14. interpolateVariable(value, variable) {
  15. if (typeof value === 'string') {
  16. if (variable.multi || variable.includeAll) {
  17. return '\'' + value + '\'';
  18. } else {
  19. return value;
  20. }
  21. }
  22. if (typeof value === 'number') {
  23. return value;
  24. }
  25. var quotedValues = _.map(value, function(val) {
  26. if (typeof value === 'number') {
  27. return value;
  28. }
  29. return '\'' + val + '\'';
  30. });
  31. return quotedValues.join(',');
  32. }
  33. query(options) {
  34. var queries = _.filter(options.targets, item => {
  35. return item.hide !== true;
  36. }).map(item => {
  37. return {
  38. refId: item.refId,
  39. intervalMs: options.intervalMs,
  40. maxDataPoints: options.maxDataPoints,
  41. datasourceId: this.id,
  42. rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
  43. format: item.format,
  44. };
  45. });
  46. if (queries.length === 0) {
  47. return this.$q.when({data: []});
  48. }
  49. return this.backendSrv.datasourceRequest({
  50. url: '/api/tsdb/query',
  51. method: 'POST',
  52. data: {
  53. from: options.range.from.valueOf().toString(),
  54. to: options.range.to.valueOf().toString(),
  55. queries: queries,
  56. }
  57. }).then(this.responseParser.processQueryResult);
  58. }
  59. annotationQuery(options) {
  60. if (!options.annotation.rawQuery) {
  61. return this.$q.reject({message: 'Query missing in annotation definition'});
  62. }
  63. const query = {
  64. refId: options.annotation.name,
  65. datasourceId: this.id,
  66. rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
  67. format: 'table',
  68. };
  69. return this.backendSrv.datasourceRequest({
  70. url: '/api/tsdb/query',
  71. method: 'POST',
  72. data: {
  73. from: options.range.from.valueOf().toString(),
  74. to: options.range.to.valueOf().toString(),
  75. queries: [query],
  76. }
  77. }).then(data => this.responseParser.transformAnnotationResponse(options, data));
  78. }
  79. metricFindQuery(query, optionalOptions) {
  80. let refId = 'tempvar';
  81. if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
  82. refId = optionalOptions.variable.name;
  83. }
  84. const interpolatedQuery = {
  85. refId: refId,
  86. datasourceId: this.id,
  87. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  88. format: 'table',
  89. };
  90. return this.backendSrv.datasourceRequest({
  91. url: '/api/tsdb/query',
  92. method: 'POST',
  93. data: {
  94. queries: [interpolatedQuery],
  95. }
  96. })
  97. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  98. }
  99. testDatasource() {
  100. return this.backendSrv.datasourceRequest({
  101. url: '/api/tsdb/query',
  102. method: 'POST',
  103. data: {
  104. from: '5m',
  105. to: 'now',
  106. queries: [{
  107. refId: 'A',
  108. intervalMs: 1,
  109. maxDataPoints: 1,
  110. datasourceId: this.id,
  111. rawSql: "SELECT 1",
  112. format: 'table',
  113. }],
  114. }
  115. }).then(res => {
  116. return { status: "success", message: "Database Connection OK"};
  117. }).catch(err => {
  118. console.log(err);
  119. if (err.data && err.data.message) {
  120. return { status: "error", message: err.data.message };
  121. } else {
  122. return { status: "error", message: err.status };
  123. }
  124. });
  125. }
  126. }