metrics_tab.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 (let 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(this.datasources.filter(value => {
  45. return includeBuiltin || !value.meta.builtIn;
  46. }).map(ds => {
  47. return {value: ds.value, text: ds.name, datasource: ds};
  48. }));
  49. }
  50. datasourceChanged(option) {
  51. if (!option) {
  52. return;
  53. }
  54. this.datasourceInstance = option.datasource;
  55. this.panelCtrl.setDatasource(option.datasource);
  56. this.updateDatasourceOptions();
  57. }
  58. addMixedQuery(option) {
  59. if (!option) {
  60. return;
  61. }
  62. this.panelCtrl.addQuery({isNew: true, datasource: option.datasource.name});
  63. this.addQueryDropdown = {text: 'Add Query', value: null, fake: true};
  64. }
  65. addQuery() {
  66. this.panelCtrl.addQuery({isNew: true});
  67. }
  68. toggleHelp() {
  69. this.optionsOpen = false;
  70. this.queryTroubleshooterOpen = false;
  71. this.helpOpen = !this.helpOpen;
  72. this.backendSrv.get(`/api/plugins/${this.datasourceInstance.meta.id}/markdown/query_help`).then(res => {
  73. var md = new Remarkable();
  74. this.helpHtml = this.$sce.trustAsHtml(md.render(res));
  75. });
  76. }
  77. toggleOptions() {
  78. this.helpOpen = false;
  79. this.queryTroubleshooterOpen = false;
  80. this.optionsOpen = !this.optionsOpen;
  81. }
  82. toggleQueryTroubleshooter() {
  83. this.helpOpen = false;
  84. this.optionsOpen = false;
  85. this.queryTroubleshooterOpen = !this.queryTroubleshooterOpen;
  86. }
  87. }
  88. /** @ngInject **/
  89. export function metricsTabDirective() {
  90. 'use strict';
  91. return {
  92. restrict: 'E',
  93. scope: true,
  94. templateUrl: 'public/app/features/panel/partials/metrics_tab.html',
  95. controller: MetricsTabCtrl,
  96. };
  97. }