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