datasource.ts 3.7 KB

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