query_ctrl.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. function PrometheusQueryCtrl($scope, templateSrv) {
  7. $scope.panelCtrl = $scope.ctrl;
  8. $scope.panel = $scope.panelCtrl.panel;
  9. $scope.init = function() {
  10. var target = $scope.target;
  11. target.expr = target.expr || '';
  12. target.intervalFactor = target.intervalFactor || 2;
  13. $scope.metric = '';
  14. $scope.resolutions = _.map([1,2,3,4,5,10], function(f) {
  15. return {factor: f, label: '1/' + f};
  16. });
  17. $scope.$on('typeahead-updated', function() {
  18. $scope.$apply($scope.inputMetric);
  19. $scope.refreshMetricData();
  20. });
  21. };
  22. $scope.refreshMetricData = function() {
  23. if (!_.isEqual($scope.oldTarget, $scope.target)) {
  24. $scope.oldTarget = angular.copy($scope.target);
  25. $scope.paneCtrl.refresh();
  26. }
  27. };
  28. $scope.inputMetric = function() {
  29. $scope.target.expr += $scope.target.metric;
  30. $scope.metric = '';
  31. };
  32. $scope.suggestMetrics = function(query, callback) {
  33. $scope.datasource
  34. .performSuggestQuery(query)
  35. .then(callback);
  36. };
  37. $scope.linkToPrometheus = function() {
  38. var range = Math.ceil(($scope.range.to.valueOf() - $scope.range.from.valueOf()) / 1000);
  39. var endTime = $scope.range.to.utc().format('YYYY-MM-DD HH:mm');
  40. var expr = {
  41. expr: templateSrv.replace($scope.target.expr, $scope.panel.scopedVars),
  42. range_input: range + 's',
  43. end_input: endTime,
  44. step_input: '',
  45. stacked: $scope.panel.stack,
  46. tab: 0
  47. };
  48. var hash = encodeURIComponent(JSON.stringify([expr]));
  49. return $scope.datasource.directUrl + '/graph#' + hash;
  50. };
  51. $scope.init();
  52. }
  53. export {PrometheusQueryCtrl};