datasource.ts 4.5 KB

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