datasource.ts 4.2 KB

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