grafana/public/app/plugins/datasource/prometheus/query_ctrl.ts
Torkel Ödegaard 0c86241c5b Webpack (#9391)
* webpack poc, this is not going to work for plugins, dam

* tech: webpack and systemjs for plugins starting to work

* tech: webpack and systemjs combo starting to work

* tech: webpack + karma tests progress

* tech: webpack + karma progress

* tech: working on tests

* tech: webpack

* tech: webpack + karma, all tests pass

* tech: webpack + karma, all tests pass

* tech: webpack all tests pass

* webpack: getting closer

* tech: webpack progress

* webpack: further build refinements

* webpack: ng annotate fixes

* webpack: optimized build fix

* tech: minor fix for elasticsearch

* tech: webpack + ace editor

* tech: restored lodash move mixin compatability

* tech: added enzyme react test and upgraded to react v16

* tech: package version fix

* tech: added testdata to built in bundle

* webpack: sass progress

* tech: prod & dev build is working for the sass

* tech: clean up unused grunt stuff and moved to scripts folder

* tech: added vendor and manifest chunks, updated readme and docs

* tech: webpack finishing touches
2017-10-01 20:02:25 +02:00

89 lines
2.3 KiB
TypeScript

import angular from 'angular';
import _ from 'lodash';
import {QueryCtrl} from 'app/plugins/sdk';
import {PromCompleter} from './completer';
import './mode-prometheus';
import './snippets/prometheus';
class PrometheusQueryCtrl extends QueryCtrl {
static templateUrl = 'partials/query.editor.html';
metric: any;
resolutions: any;
formats: any;
instant: any;
oldTarget: any;
suggestMetrics: any;
getMetricsAutocomplete: any;
linkToPrometheus: any;
/** @ngInject */
constructor($scope, $injector, private templateSrv) {
super($scope, $injector);
var target = this.target;
target.expr = target.expr || '';
target.intervalFactor = target.intervalFactor || 2;
target.format = target.format || this.getDefaultFormat();
this.metric = '';
this.resolutions = _.map([1,2,3,4,5,10], function(f) {
return {factor: f, label: '1/' + f};
});
this.formats = [
{text: 'Time series', value: 'time_series'},
{text: 'Table', value: 'table'},
];
this.instant = false;
this.updateLink();
}
getCompleter(query) {
return new PromCompleter(this.datasource);
}
getDefaultFormat() {
if (this.panelCtrl.panel.type === 'table') {
return 'table';
}
return 'time_series';
}
refreshMetricData() {
if (!_.isEqual(this.oldTarget, this.target)) {
this.oldTarget = angular.copy(this.target);
this.panelCtrl.refresh();
this.updateLink();
}
}
updateLink() {
var range = this.panelCtrl.range;
if (!range) {
return;
}
var rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
var endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
var expr = {
'g0.expr': this.templateSrv.replace(this.target.expr, this.panelCtrl.panel.scopedVars, this.datasource.interpolateQueryExpr),
'g0.range_input': rangeDiff + 's',
'g0.end_input': endTime,
'g0.step_input': this.target.step,
'g0.stacked': this.panelCtrl.panel.stack ? 1 : 0,
'g0.tab': 0
};
var args = _.map(expr, (v, k) => { return k + '=' + encodeURIComponent(v); }).join('&');
this.linkToPrometheus = this.datasource.directUrl + '/graph?' + args;
}
getCollapsedText() {
return this.target.expr;
}
}
export {PrometheusQueryCtrl};