metrics_tab.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Types
  8. import { DashboardModel } from '../dashboard/dashboard_model';
  9. export class MetricsTabCtrl {
  10. dsName: string;
  11. panel: any;
  12. panelCtrl: any;
  13. datasources: any[];
  14. datasourceInstance: any;
  15. nextRefId: string;
  16. dashboard: DashboardModel;
  17. panelDsValue: any;
  18. addQueryDropdown: any;
  19. queryTroubleshooterOpen: boolean;
  20. helpOpen: boolean;
  21. optionsOpen: boolean;
  22. hasQueryHelp: boolean;
  23. helpHtml: string;
  24. queryOptions: any;
  25. /** @ngInject */
  26. constructor($scope, private $sce, datasourceSrv, private backendSrv) {
  27. this.panelCtrl = $scope.ctrl;
  28. $scope.ctrl = this;
  29. this.panel = this.panelCtrl.panel;
  30. this.panel.datasource = this.panel.datasource || null;
  31. this.panel.targets = this.panel.targets || [{}];
  32. this.dashboard = this.panelCtrl.dashboard;
  33. this.datasources = datasourceSrv.getMetricSources();
  34. this.panelDsValue = this.panelCtrl.panel.datasource;
  35. for (const ds of this.datasources) {
  36. if (ds.value === this.panelDsValue) {
  37. this.datasourceInstance = ds;
  38. }
  39. }
  40. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  41. // update next ref id
  42. this.panelCtrl.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
  43. this.updateDatasourceOptions();
  44. }
  45. updateDatasourceOptions() {
  46. if (this.datasourceInstance) {
  47. this.hasQueryHelp = this.datasourceInstance.meta.hasQueryHelp;
  48. this.queryOptions = this.datasourceInstance.meta.queryOptions;
  49. }
  50. }
  51. getOptions(includeBuiltin) {
  52. return Promise.resolve(
  53. this.datasources
  54. .filter(value => {
  55. return includeBuiltin || !value.meta.builtIn;
  56. })
  57. .map(ds => {
  58. return { value: ds.value, text: ds.name, datasource: ds };
  59. })
  60. );
  61. }
  62. datasourceChanged(option) {
  63. if (!option) {
  64. return;
  65. }
  66. this.datasourceInstance = option.datasource;
  67. this.setDatasource(option.datasource);
  68. this.updateDatasourceOptions();
  69. }
  70. setDatasource(datasource) {
  71. // switching to mixed
  72. if (datasource.meta.mixed) {
  73. _.each(this.panel.targets, target => {
  74. target.datasource = this.panel.datasource;
  75. if (!target.datasource) {
  76. target.datasource = config.defaultDatasource;
  77. }
  78. });
  79. } else if (this.datasourceInstance && this.datasourceInstance.meta.mixed) {
  80. _.each(this.panel.targets, target => {
  81. delete target.datasource;
  82. });
  83. }
  84. this.panel.datasource = datasource.value;
  85. this.panel.refresh();
  86. }
  87. addMixedQuery(option) {
  88. if (!option) {
  89. return;
  90. }
  91. this.panelCtrl.addQuery({
  92. isNew: true,
  93. datasource: option.datasource.name,
  94. });
  95. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  96. }
  97. addQuery() {
  98. this.panelCtrl.addQuery({ isNew: true });
  99. }
  100. toggleHelp() {
  101. this.optionsOpen = false;
  102. this.queryTroubleshooterOpen = false;
  103. this.helpOpen = !this.helpOpen;
  104. this.backendSrv.get(`/api/plugins/${this.datasourceInstance.meta.id}/markdown/query_help`).then(res => {
  105. const md = new Remarkable();
  106. this.helpHtml = this.$sce.trustAsHtml(md.render(res));
  107. });
  108. }
  109. toggleOptions() {
  110. this.helpOpen = false;
  111. this.queryTroubleshooterOpen = false;
  112. this.optionsOpen = !this.optionsOpen;
  113. }
  114. toggleQueryTroubleshooter() {
  115. this.helpOpen = false;
  116. this.optionsOpen = false;
  117. this.queryTroubleshooterOpen = !this.queryTroubleshooterOpen;
  118. }
  119. }
  120. /** @ngInject */
  121. export function metricsTabDirective() {
  122. 'use strict';
  123. return {
  124. restrict: 'E',
  125. scope: true,
  126. templateUrl: 'public/app/features/panel/partials/metrics_tab.html',
  127. controller: MetricsTabCtrl,
  128. };
  129. }
  130. coreModule.directive('metricsTab', metricsTabDirective);