datasource.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(
  10. instanceSettings,
  11. private backendSrv,
  12. private $q,
  13. private templateSrv
  14. ) {
  15. this.name = instanceSettings.name;
  16. this.id = instanceSettings.id;
  17. this.responseParser = new ResponseParser(this.$q);
  18. }
  19. interpolateVariable(value, variable) {
  20. if (typeof value === 'string') {
  21. if (variable.multi || variable.includeAll) {
  22. return "'" + value + "'";
  23. } else {
  24. return value;
  25. }
  26. }
  27. if (typeof value === 'number') {
  28. return value;
  29. }
  30. var quotedValues = _.map(value, function(val) {
  31. return "'" + val + "'";
  32. });
  33. return quotedValues.join(',');
  34. }
  35. query(options) {
  36. var queries = _.filter(options.targets, item => {
  37. return item.hide !== true;
  38. }).map(item => {
  39. return {
  40. refId: item.refId,
  41. intervalMs: options.intervalMs,
  42. maxDataPoints: options.maxDataPoints,
  43. datasourceId: this.id,
  44. rawSql: this.templateSrv.replace(
  45. item.rawSql,
  46. options.scopedVars,
  47. this.interpolateVariable
  48. ),
  49. format: item.format,
  50. };
  51. });
  52. if (queries.length === 0) {
  53. return this.$q.when({ data: [] });
  54. }
  55. return this.backendSrv
  56. .datasourceRequest({
  57. url: '/api/tsdb/query',
  58. method: 'POST',
  59. data: {
  60. from: options.range.from.valueOf().toString(),
  61. to: options.range.to.valueOf().toString(),
  62. queries: queries,
  63. },
  64. })
  65. .then(this.responseParser.processQueryResult);
  66. }
  67. annotationQuery(options) {
  68. if (!options.annotation.rawQuery) {
  69. return this.$q.reject({
  70. message: 'Query missing in annotation definition',
  71. });
  72. }
  73. const query = {
  74. refId: options.annotation.name,
  75. datasourceId: this.id,
  76. rawSql: this.templateSrv.replace(
  77. options.annotation.rawQuery,
  78. options.scopedVars,
  79. this.interpolateVariable
  80. ),
  81. format: 'table',
  82. };
  83. return this.backendSrv
  84. .datasourceRequest({
  85. url: '/api/tsdb/query',
  86. method: 'POST',
  87. data: {
  88. from: options.range.from.valueOf().toString(),
  89. to: options.range.to.valueOf().toString(),
  90. queries: [query],
  91. },
  92. })
  93. .then(data =>
  94. this.responseParser.transformAnnotationResponse(options, data)
  95. );
  96. }
  97. metricFindQuery(query, optionalOptions) {
  98. let refId = 'tempvar';
  99. if (
  100. optionalOptions &&
  101. optionalOptions.variable &&
  102. optionalOptions.variable.name
  103. ) {
  104. refId = optionalOptions.variable.name;
  105. }
  106. const interpolatedQuery = {
  107. refId: refId,
  108. datasourceId: this.id,
  109. rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
  110. format: 'table',
  111. };
  112. var data = {
  113. queries: [interpolatedQuery],
  114. };
  115. if (
  116. optionalOptions &&
  117. optionalOptions.range &&
  118. optionalOptions.range.from
  119. ) {
  120. data['from'] = optionalOptions.range.from.valueOf().toString();
  121. }
  122. if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
  123. data['to'] = optionalOptions.range.to.valueOf().toString();
  124. }
  125. return this.backendSrv
  126. .datasourceRequest({
  127. url: '/api/tsdb/query',
  128. method: 'POST',
  129. data: data,
  130. })
  131. .then(data =>
  132. this.responseParser.parseMetricFindQueryResult(refId, data)
  133. );
  134. }
  135. testDatasource() {
  136. return this.backendSrv
  137. .datasourceRequest({
  138. url: '/api/tsdb/query',
  139. method: 'POST',
  140. data: {
  141. from: '5m',
  142. to: 'now',
  143. queries: [
  144. {
  145. refId: 'A',
  146. intervalMs: 1,
  147. maxDataPoints: 1,
  148. datasourceId: this.id,
  149. rawSql: 'SELECT 1',
  150. format: 'table',
  151. },
  152. ],
  153. },
  154. })
  155. .then(res => {
  156. return { status: 'success', message: 'Database Connection OK' };
  157. })
  158. .catch(err => {
  159. console.log(err);
  160. if (err.data && err.data.message) {
  161. return { status: 'error', message: err.data.message };
  162. } else {
  163. return { status: 'error', message: err.status };
  164. }
  165. });
  166. }
  167. }