datasource.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. export class MysqlDatasource {
  4. id: any;
  5. name: any;
  6. /** @ngInject **/
  7. constructor(instanceSettings, private backendSrv, private $q, private templateSrv) {
  8. this.name = instanceSettings.name;
  9. this.id = instanceSettings.id;
  10. }
  11. interpolateVariable(value) {
  12. if (typeof value === 'string') {
  13. return '\"' + value + '\"';
  14. }
  15. var quotedValues = _.map(value, function(val) {
  16. return '\"' + val + '\"';
  17. });
  18. return quotedValues.join(',');
  19. }
  20. query(options) {
  21. var queries = _.filter(options.targets, item => {
  22. return item.hide !== true;
  23. }).map(item => {
  24. return {
  25. refId: item.refId,
  26. intervalMs: options.intervalMs,
  27. maxDataPoints: options.maxDataPoints,
  28. datasourceId: this.id,
  29. rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
  30. format: item.format,
  31. };
  32. });
  33. if (queries.length === 0) {
  34. return this.$q.when({data: []});
  35. }
  36. return this.backendSrv.datasourceRequest({
  37. url: '/api/tsdb/query',
  38. method: 'POST',
  39. data: {
  40. from: options.range.from.valueOf().toString(),
  41. to: options.range.to.valueOf().toString(),
  42. queries: queries,
  43. }
  44. }).then(this.processQueryResult.bind(this));
  45. }
  46. testDatasource() {
  47. return this.backendSrv.datasourceRequest({
  48. url: '/api/tsdb/query',
  49. method: 'POST',
  50. data: {
  51. from: '5m',
  52. to: 'now',
  53. queries: [{
  54. refId: 'A',
  55. intervalMs: 1,
  56. maxDataPoints: 1,
  57. datasourceId: this.id,
  58. rawSql: "SELECT 1",
  59. format: 'table',
  60. }],
  61. }
  62. }).then(res => {
  63. return { status: "success", message: "Database Connection OK", title: "Success" };
  64. }).catch(err => {
  65. console.log(err);
  66. if (err.data && err.data.message) {
  67. return { status: "error", message: err.data.message, title: "Error" };
  68. } else {
  69. return { status: "error", message: err.status, title: "Error" };
  70. }
  71. });
  72. }
  73. processQueryResult(res) {
  74. var data = [];
  75. if (!res.data.results) {
  76. return {data: data};
  77. }
  78. for (let key in res.data.results) {
  79. let queryRes = res.data.results[key];
  80. if (queryRes.series) {
  81. for (let series of queryRes.series) {
  82. data.push({
  83. target: series.name,
  84. datapoints: series.points,
  85. refId: queryRes.refId,
  86. meta: queryRes.meta,
  87. });
  88. }
  89. }
  90. if (queryRes.tables) {
  91. for (let table of queryRes.tables) {
  92. table.type = 'table';
  93. table.refId = queryRes.refId;
  94. table.meta = queryRes.meta;
  95. data.push(table);
  96. }
  97. }
  98. }
  99. return {data: data};
  100. }
  101. }