metrics_tab.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Libraries
  2. import _ from 'lodash';
  3. import Remarkable from 'remarkable';
  4. // Services & utils
  5. import coreModule from 'app/core/core_module';
  6. import config from 'app/core/config';
  7. import { Emitter } from 'app/core/utils/emitter';
  8. // Types
  9. import { DashboardModel } from '../dashboard/dashboard_model';
  10. export class MetricsTabCtrl {
  11. dsName: string;
  12. panel: any;
  13. panelCtrl: any;
  14. datasources: any[];
  15. datasourceInstance: any;
  16. nextRefId: string;
  17. dashboard: DashboardModel;
  18. panelDsValue: any;
  19. addQueryDropdown: any;
  20. queryTroubleshooterOpen: boolean;
  21. helpOpen: boolean;
  22. optionsOpen: boolean;
  23. hasQueryHelp: boolean;
  24. helpHtml: string;
  25. queryOptions: any;
  26. events: Emitter;
  27. /** @ngInject */
  28. constructor($scope, private $sce, datasourceSrv, private backendSrv) {
  29. this.panelCtrl = $scope.ctrl;
  30. $scope.ctrl = this;
  31. this.panel = this.panelCtrl.panel;
  32. this.panel.datasource = this.panel.datasource || null;
  33. this.panel.targets = this.panel.targets || [{}];
  34. this.dashboard = this.panelCtrl.dashboard;
  35. this.datasources = datasourceSrv.getMetricSources();
  36. this.panelDsValue = this.panelCtrl.panel.datasource;
  37. // added here as old query controller expects this on panelCtrl but
  38. // they are getting MetricsTabCtrl instead
  39. this.events = this.panel.events;
  40. for (const ds of this.datasources) {
  41. if (ds.value === this.panelDsValue) {
  42. this.datasourceInstance = ds;
  43. }
  44. }
  45. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  46. // update next ref id
  47. this.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
  48. this.updateDatasourceOptions();
  49. }
  50. updateDatasourceOptions() {
  51. if (this.datasourceInstance) {
  52. this.hasQueryHelp = this.datasourceInstance.meta.hasQueryHelp;
  53. this.queryOptions = this.datasourceInstance.meta.queryOptions;
  54. }
  55. }
  56. getOptions(includeBuiltin) {
  57. return Promise.resolve(
  58. this.datasources
  59. .filter(value => {
  60. return includeBuiltin || !value.meta.builtIn;
  61. })
  62. .map(ds => {
  63. return { value: ds.value, text: ds.name, datasource: ds };
  64. })
  65. );
  66. }
  67. datasourceChanged(option) {
  68. if (!option) {
  69. return;
  70. }
  71. this.setDatasource(option.datasource);
  72. this.updateDatasourceOptions();
  73. }
  74. setDatasource(datasource) {
  75. // switching to mixed
  76. if (datasource.meta.mixed) {
  77. _.each(this.panel.targets, target => {
  78. target.datasource = this.panel.datasource;
  79. if (!target.datasource) {
  80. target.datasource = config.defaultDatasource;
  81. }
  82. });
  83. } else if (this.datasourceInstance && this.datasourceInstance.meta.mixed) {
  84. _.each(this.panel.targets, target => {
  85. delete target.datasource;
  86. });
  87. }
  88. this.datasourceInstance = datasource;
  89. this.panel.datasource = datasource.value;
  90. this.panel.refresh();
  91. }
  92. addMixedQuery(option) {
  93. if (!option) {
  94. return;
  95. }
  96. this.panelCtrl.addQuery({
  97. isNew: true,
  98. datasource: option.datasource.name,
  99. });
  100. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  101. }
  102. toggleHelp() {
  103. this.optionsOpen = false;
  104. this.queryTroubleshooterOpen = false;
  105. this.helpOpen = !this.helpOpen;
  106. this.backendSrv.get(`/api/plugins/${this.datasourceInstance.meta.id}/markdown/query_help`).then(res => {
  107. const md = new Remarkable();
  108. this.helpHtml = this.$sce.trustAsHtml(md.render(res));
  109. });
  110. }
  111. toggleOptions() {
  112. this.helpOpen = false;
  113. this.queryTroubleshooterOpen = false;
  114. this.optionsOpen = !this.optionsOpen;
  115. }
  116. toggleQueryTroubleshooter() {
  117. this.helpOpen = false;
  118. this.optionsOpen = false;
  119. this.queryTroubleshooterOpen = !this.queryTroubleshooterOpen;
  120. }
  121. addQuery(query?) {
  122. query = query || {};
  123. query.refId = this.dashboard.getNextQueryLetter(this.panel);
  124. query.isNew = true;
  125. this.panel.targets.push(query);
  126. this.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
  127. }
  128. refresh() {
  129. this.panel.refresh();
  130. }
  131. render() {
  132. this.panel.render();
  133. }
  134. removeQuery(target) {
  135. const index = _.indexOf(this.panel.targets, target);
  136. this.panel.targets.splice(index, 1);
  137. this.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
  138. this.panel.refresh();
  139. }
  140. moveQuery(target, direction) {
  141. const index = _.indexOf(this.panel.targets, target);
  142. _.move(this.panel.targets, index, index + direction);
  143. }
  144. }
  145. /** @ngInject */
  146. export function metricsTabDirective() {
  147. 'use strict';
  148. return {
  149. restrict: 'E',
  150. scope: true,
  151. templateUrl: 'public/app/features/panel/partials/metrics_tab.html',
  152. controller: MetricsTabCtrl,
  153. };
  154. }
  155. coreModule.directive('metricsTab', metricsTabDirective);