metrics_tab.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(
  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
  80. .get(
  81. `/api/plugins/${this.datasourceInstance.meta.id}/markdown/query_help`
  82. )
  83. .then(res => {
  84. var md = new Remarkable();
  85. this.helpHtml = this.$sce.trustAsHtml(md.render(res));
  86. });
  87. }
  88. toggleOptions() {
  89. this.helpOpen = false;
  90. this.queryTroubleshooterOpen = false;
  91. this.optionsOpen = !this.optionsOpen;
  92. }
  93. toggleQueryTroubleshooter() {
  94. this.helpOpen = false;
  95. this.optionsOpen = false;
  96. this.queryTroubleshooterOpen = !this.queryTroubleshooterOpen;
  97. }
  98. }
  99. /** @ngInject **/
  100. export function metricsTabDirective() {
  101. "use strict";
  102. return {
  103. restrict: "E",
  104. scope: true,
  105. templateUrl: "public/app/features/panel/partials/metrics_tab.html",
  106. controller: MetricsTabCtrl
  107. };
  108. }