datasource.ts 3.5 KB

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