query_ctrl.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/features/panel/panel';
  7. class PrometheusQueryCtrl extends QueryCtrl {
  8. static templateUrl = 'public/app/plugins/datasource/prometheus/partials/query.editor.html';
  9. metric: any;
  10. resolutions: any;
  11. oldTarget: any;
  12. suggestMetrics: any;
  13. linkToPrometheus: any;
  14. /** @ngInject */
  15. constructor($scope, $injector, private templateSrv) {
  16. super($scope, $injector);
  17. var target = this.target;
  18. target.expr = target.expr || '';
  19. target.intervalFactor = target.intervalFactor || 2;
  20. this.metric = '';
  21. this.resolutions = _.map([1,2,3,4,5,10], function(f) {
  22. return {factor: f, label: '1/' + f};
  23. });
  24. $scope.$on('typeahead-updated', () => {
  25. this.$scope.$apply(() => {
  26. this.target.expr += this.target.metric;
  27. this.metric = '';
  28. this.refreshMetricData();
  29. });
  30. });
  31. // called from typeahead so need this
  32. // here in order to ensure this ref
  33. this.suggestMetrics = (query, callback) => {
  34. console.log(this);
  35. this.datasource.performSuggestQuery(query).then(callback);
  36. };
  37. this.updateLink();
  38. }
  39. refreshMetricData() {
  40. if (!_.isEqual(this.oldTarget, this.target)) {
  41. this.oldTarget = angular.copy(this.target);
  42. this.panelCtrl.refresh();
  43. this.updateLink();
  44. }
  45. }
  46. updateLink() {
  47. var range = this.panelCtrl.range;
  48. var rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
  49. var endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
  50. var expr = {
  51. expr: this.templateSrv.replace(this.target.expr, this.panelCtrl.panel.scopedVars),
  52. range_input: rangeDiff + 's',
  53. end_input: endTime,
  54. step_input: '',
  55. stacked: this.panelCtrl.panel.stack,
  56. tab: 0
  57. };
  58. var hash = encodeURIComponent(JSON.stringify([expr]));
  59. this.linkToPrometheus = this.datasource.directUrl + '/graph#' + hash;
  60. }
  61. }
  62. export {PrometheusQueryCtrl};