query_ctrl.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. import * as dateMath from 'app/core/utils/datemath';
  6. import {QueryCtrl} from 'app/plugins/sdk';
  7. import {PromQuery, getQueryPartCategories} from './prom_query';
  8. class PrometheusQueryCtrl extends QueryCtrl {
  9. static templateUrl = 'partials/query.editor.html';
  10. query: any;
  11. metricSegment: any;
  12. addQueryPartMenu: any[];
  13. resolutions: any;
  14. oldTarget: any;
  15. suggestMetrics: any;
  16. linkToPrometheus: any;
  17. /** @ngInject */
  18. constructor($scope, $injector, private templateSrv, private uiSegmentSrv) {
  19. super($scope, $injector);
  20. this.query = new PromQuery(this.target, templateSrv);
  21. if (this.target.metric) {
  22. this.metricSegment = uiSegmentSrv.newSegment(this.target.metric);
  23. } else {
  24. this.metricSegment = uiSegmentSrv.newSegment({value: 'select metric', fake: true});
  25. }
  26. this.resolutions = _.map([1,2,3,4,5,10], function(f) {
  27. return {factor: f, label: '1/' + f};
  28. });
  29. this.updateLink();
  30. this.buildQueryPartMenu();
  31. }
  32. buildQueryPartMenu() {
  33. var categories = getQueryPartCategories();
  34. this.addQueryPartMenu = _.reduce(categories, function(memo, cat, key) {
  35. var menu = {
  36. text: key,
  37. submenu: cat.map(item => {
  38. return {text: item.type, value: item.type};
  39. }),
  40. };
  41. memo.push(menu);
  42. return memo;
  43. }, []);
  44. }
  45. addQueryPart(item, subItem) {
  46. this.query.addQueryPart(item, subItem);
  47. this.panelCtrl.refresh();
  48. }
  49. getMetricOptions() {
  50. return this.datasource.performSuggestQuery('').then(res => {
  51. return _.map(res, metric => {
  52. return this.uiSegmentSrv.newSegment(metric);
  53. });
  54. });
  55. }
  56. queryChanged() {
  57. this.target.metric = this.metricSegment.value;
  58. }
  59. refreshMetricData() {
  60. if (!_.isEqual(this.oldTarget, this.target)) {
  61. this.oldTarget = angular.copy(this.target);
  62. this.panelCtrl.refresh();
  63. this.updateLink();
  64. }
  65. }
  66. updateLink() {
  67. var range = this.panelCtrl.range;
  68. var rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
  69. var endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
  70. var expr = {
  71. expr: this.templateSrv.replace(this.target.expr, this.panelCtrl.panel.scopedVars),
  72. range_input: rangeDiff + 's',
  73. end_input: endTime,
  74. step_input: '',
  75. stacked: this.panelCtrl.panel.stack,
  76. tab: 0
  77. };
  78. var hash = encodeURIComponent(JSON.stringify([expr]));
  79. this.linkToPrometheus = this.datasource.directUrl + '/graph#' + hash;
  80. }
  81. }
  82. export {PrometheusQueryCtrl};