grafana/public/app/plugins/datasource/prometheus/query_ctrl.ts
2016-02-02 09:12:58 +01:00

69 lines
1.9 KiB
TypeScript

///<reference path="../../../headers/common.d.ts" />
import angular from 'angular';
import _ from 'lodash';
import moment from 'moment';
import * as dateMath from 'app/core/utils/datemath';
import {QueryEditorCtrl} from 'app/features/panel/panel';
/** @ngInject */
class PrometheusQueryCtrl extends QueryEditorCtrl {
static templateUrl = 'public/app/plugins/datasource/prometheus/partials/query.editor.html';
metric: any;
resolutions: any;
oldTarget: any;
constructor($scope, $injector, private templateSrv) {
super($scope, $injector);
var target = this.target;
target.expr = target.expr || '';
target.intervalFactor = target.intervalFactor || 2;
this.metric = '';
this.resolutions = _.map([1,2,3,4,5,10], function(f) {
return {factor: f, label: '1/' + f};
});
$scope.$on('typeahead-updated', () => {
$scope.$apply(this.inputMetric);
this.refreshMetricData();
});
}
refreshMetricData() {
if (!_.isEqual(this.oldTarget, this.target)) {
this.oldTarget = angular.copy(this.target);
this.panelCtrl.refresh();
}
}
inputMetric() {
this.target.expr += this.target.metric;
this.metric = '';
}
suggestMetrics(query, callback) {
this.datasource.performSuggestQuery(query).then(callback);
}
linkToPrometheus() {
var range = this.panelCtrl.range;
var rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
var endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
var expr = {
expr: this.templateSrv.replace(this.target.expr, this.panelCtrl.panel.scopedVars),
range_input: rangeDiff + 's',
end_input: endTime,
step_input: '',
stacked: this.panelCtrl.panel.stack,
tab: 0
};
var hash = encodeURIComponent(JSON.stringify([expr]));
return this.datasource.directUrl + '/graph#' + hash;
};
}
export {PrometheusQueryCtrl};