metrics_tab.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { DashboardModel } from '../dashboard/dashboard_model';
  2. import Remarkable from 'remarkable';
  3. export class MetricsTabCtrl {
  4. dsName: string;
  5. panel: any;
  6. panelCtrl: any;
  7. datasources: any[];
  8. datasourceInstance: any;
  9. nextRefId: string;
  10. dashboard: DashboardModel;
  11. panelDsValue: any;
  12. addQueryDropdown: any;
  13. queryTroubleshooterOpen: boolean;
  14. helpOpen: boolean;
  15. optionsOpen: boolean;
  16. hasQueryHelp: boolean;
  17. helpHtml: string;
  18. queryOptions: any;
  19. /** @ngInject */
  20. constructor($scope, private $sce, datasourceSrv, private backendSrv) {
  21. this.panelCtrl = $scope.ctrl;
  22. $scope.ctrl = this;
  23. this.panel = this.panelCtrl.panel;
  24. this.dashboard = this.panelCtrl.dashboard;
  25. this.datasources = datasourceSrv.getMetricSources();
  26. this.panelDsValue = this.panelCtrl.panel.datasource;
  27. for (const ds of this.datasources) {
  28. if (ds.value === this.panelDsValue) {
  29. this.datasourceInstance = ds;
  30. }
  31. }
  32. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  33. // update next ref id
  34. this.panelCtrl.nextRefId = this.dashboard.getNextQueryLetter(this.panel);
  35. this.updateDatasourceOptions();
  36. }
  37. updateDatasourceOptions() {
  38. if (this.datasourceInstance) {
  39. this.hasQueryHelp = this.datasourceInstance.meta.hasQueryHelp;
  40. this.queryOptions = this.datasourceInstance.meta.queryOptions;
  41. }
  42. }
  43. getOptions(includeBuiltin) {
  44. return Promise.resolve(
  45. this.datasources
  46. .filter(value => {
  47. return includeBuiltin || !value.meta.builtIn;
  48. })
  49. .map(ds => {
  50. return { value: ds.value, text: ds.name, datasource: ds };
  51. })
  52. );
  53. }
  54. datasourceChanged(option) {
  55. if (!option) {
  56. return;
  57. }
  58. this.datasourceInstance = option.datasource;
  59. this.panelCtrl.setDatasource(option.datasource);
  60. this.updateDatasourceOptions();
  61. }
  62. addMixedQuery(option) {
  63. if (!option) {
  64. return;
  65. }
  66. this.panelCtrl.addQuery({
  67. isNew: true,
  68. datasource: option.datasource.name,
  69. });
  70. this.addQueryDropdown = { text: 'Add Query', value: null, fake: true };
  71. }
  72. addQuery() {
  73. this.panelCtrl.addQuery({ isNew: true });
  74. }
  75. toggleHelp() {
  76. this.optionsOpen = false;
  77. this.queryTroubleshooterOpen = false;
  78. this.helpOpen = !this.helpOpen;
  79. this.backendSrv.get(`/api/plugins/${this.datasourceInstance.meta.id}/markdown/query_help`).then(res => {
  80. const md = new Remarkable();
  81. this.helpHtml = this.$sce.trustAsHtml(md.render(res));
  82. });
  83. }
  84. toggleOptions() {
  85. this.helpOpen = false;
  86. this.queryTroubleshooterOpen = false;
  87. this.optionsOpen = !this.optionsOpen;
  88. }
  89. toggleQueryTroubleshooter() {
  90. this.helpOpen = false;
  91. this.optionsOpen = false;
  92. this.queryTroubleshooterOpen = !this.queryTroubleshooterOpen;
  93. }
  94. }
  95. /** @ngInject */
  96. export function metricsTabDirective() {
  97. 'use strict';
  98. return {
  99. restrict: 'E',
  100. scope: true,
  101. templateUrl: 'public/app/features/panel/partials/metrics_tab.html',
  102. controller: MetricsTabCtrl,
  103. };
  104. }