datasource.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import _ from 'lodash';
  2. import ResponseParser from './response_parser';
  3. import PostgresQuery from 'app/plugins/datasource/postgres/postgres_query';
  4. export class PostgresDatasource {
  5. id: any;
  6. name: any;
  7. jsonData: any;
  8. responseParser: ResponseParser;
  9. queryModel: PostgresQuery;
  10. interval: string;
  11. /** @ngInject */
  12. constructor(instanceSettings, private backendSrv, private $q, private templateSrv, private timeSrv) {
  13. this.name = instanceSettings.name;
  14. this.id = instanceSettings.id;
  15. this.jsonData = instanceSettings.jsonData;
  16. this.responseParser = new ResponseParser(this.$q);
  17. this.queryModel = new PostgresQuery({});
  18. this.interval = instanceSettings.jsonData.timeInterval;
  19. }
  20. interpolateVariable(value, variable) {
  21. if (typeof value === 'string') {
  22. if (variable.multi || variable.includeAll) {
  23. return this.queryModel.quoteLiteral(value);
  24. } else {
  25. return value;
  26. }
  27. }
  28. if (typeof value === 'number') {
  29. return value;
  30. }
  31. const quotedValues = _.map(value, v => {
  32. return this.queryModel.quoteLiteral(v);
  33. });
  34. return quotedValues.join(',');
  35. }
  36. query(options) {
  37. const queries = _.filter(options.targets, target => {
  38. return target.hide !== true;
  39. }).map(target => {
  40. const queryModel = new PostgresQuery(target, this.templateSrv, options.scopedVars);
  41. return {
  42. refId: target.refId,
  43. intervalMs: options.intervalMs,
  44. maxDataPoints: options.maxDataPoints,
  45. datasourceId: this.id,
  46. rawSql: queryModel.render(this.interpolateVariable),
  47. format: target.format,
  48. };
  49. });
  50. if (queries.length === 0) {
  51. return this.$q.when({ data: [] });
  52. }
  53. return this.backendSrv
  54. .datasourceRequest({
  55. url: '/api/tsdb/query',
  56. method: 'POST',
  57. data: {
  58. from: options.range.from.valueOf().toString(),
  59. to: options.range.to.valueOf().toString(),
  60. queries: queries,
  61. },
  62. })
  63. .then(this.responseParser.processQueryResult);
  64. }
  65. annotationQuery(options) {
  66. if (!options.annotation.rawQuery) {
  67. return this.$q.reject({
  68. message: 'Query missing in annotation definition',
  69. });
  70. }
  71. const query = {
  72. refId: options.annotation.name,
  73. datasourceId: this.id,
  74. rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
  75. format: 'table',
  76. };
  77. return this.backendSrv
  78. .datasourceRequest({
  79. url: '/api/tsdb/query',
  80. method: 'POST',
  81. data: {
  82. from: options.range.from.valueOf().toString(),
  83. to: options.range.to.valueOf().toString(),
  84. queries: [query],
  85. },
  86. })
  87. .then(data => this.responseParser.transformAnnotationResponse(options, data));
  88. }
  89. metricFindQuery(query, optionalOptions) {
  90. let refId = 'tempvar';
  91. if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
  92. refId = optionalOptions.variable.name;
  93. }
  94. const interpolatedQuery = {
  95. refId: refId,
  96. datasourceId: this.id,
  97. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  98. format: 'table',
  99. };
  100. const range = this.timeSrv.timeRange();
  101. const data = {
  102. queries: [interpolatedQuery],
  103. from: range.from.valueOf().toString(),
  104. to: range.to.valueOf().toString(),
  105. };
  106. return this.backendSrv
  107. .datasourceRequest({
  108. url: '/api/tsdb/query',
  109. method: 'POST',
  110. data: data,
  111. })
  112. .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
  113. }
  114. getVersion() {
  115. return this.metricFindQuery("SELECT current_setting('server_version_num')::int/100", {});
  116. }
  117. getTimescaleDBVersion() {
  118. return this.metricFindQuery("SELECT extversion FROM pg_extension WHERE extname = 'timescaledb'", {});
  119. }
  120. testDatasource() {
  121. return this.metricFindQuery('SELECT 1', {})
  122. .then(res => {
  123. return { status: 'success', message: 'Database Connection OK' };
  124. })
  125. .catch(err => {
  126. console.log(err);
  127. if (err.data && err.data.message) {
  128. return { status: 'error', message: err.data.message };
  129. } else {
  130. return { status: 'error', message: err.status };
  131. }
  132. });
  133. }
  134. }