query_ctrl.ts 1.9 KB

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