metrics_tab.ts 3.0 KB

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