query_ctrl.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import { QueryCtrl } from 'app/plugins/sdk';
  4. import { PromCompleter } from './completer';
  5. import './mode-prometheus';
  6. import './snippets/prometheus';
  7. import { TemplateSrv } from 'app/features/templating/template_srv';
  8. class PrometheusQueryCtrl extends QueryCtrl {
  9. static templateUrl = 'partials/query.editor.html';
  10. metric: any;
  11. resolutions: any;
  12. formats: any;
  13. instant: any;
  14. oldTarget: any;
  15. suggestMetrics: any;
  16. getMetricsAutocomplete: any;
  17. linkToPrometheus: any;
  18. /** @ngInject */
  19. constructor($scope: any, $injector: angular.auto.IInjectorService, private templateSrv: TemplateSrv) {
  20. super($scope, $injector);
  21. const target = this.target;
  22. target.expr = target.expr || '';
  23. target.intervalFactor = target.intervalFactor || 1;
  24. target.format = target.format || this.getDefaultFormat();
  25. this.metric = '';
  26. this.resolutions = _.map([1, 2, 3, 4, 5, 10], f => {
  27. return { factor: f, label: '1/' + f };
  28. });
  29. this.formats = [
  30. { text: 'Time series', value: 'time_series' },
  31. { text: 'Table', value: 'table' },
  32. { text: 'Heatmap', value: 'heatmap' },
  33. ];
  34. this.instant = false;
  35. this.updateLink();
  36. }
  37. getCompleter(query: string) {
  38. return new PromCompleter(this.datasource, this.templateSrv);
  39. }
  40. getDefaultFormat() {
  41. if (this.panelCtrl.panel.type === 'table') {
  42. return 'table';
  43. } else if (this.panelCtrl.panel.type === 'heatmap') {
  44. return 'heatmap';
  45. }
  46. return 'time_series';
  47. }
  48. refreshMetricData() {
  49. if (!_.isEqual(this.oldTarget, this.target)) {
  50. this.oldTarget = angular.copy(this.target);
  51. this.panelCtrl.refresh();
  52. this.updateLink();
  53. }
  54. }
  55. updateLink() {
  56. const range = this.panelCtrl.range;
  57. if (!range) {
  58. return;
  59. }
  60. const rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
  61. const endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
  62. const expr = {
  63. 'g0.expr': this.templateSrv.replace(
  64. this.target.expr,
  65. this.panelCtrl.panel.scopedVars,
  66. this.datasource.interpolateQueryExpr
  67. ),
  68. 'g0.range_input': rangeDiff + 's',
  69. 'g0.end_input': endTime,
  70. 'g0.step_input': this.target.step,
  71. 'g0.stacked': this.panelCtrl.panel.stack ? 1 : 0,
  72. 'g0.tab': 0,
  73. };
  74. const args = _.map(expr, (v, k) => {
  75. return k + '=' + encodeURIComponent(v);
  76. }).join('&');
  77. this.linkToPrometheus = this.datasource.directUrl + '/graph?' + args;
  78. }
  79. }
  80. export { PrometheusQueryCtrl };