Merge pull request #5875 from mtanda/prometheus_time

(prometheus) refactor time calculation
This commit is contained in:
Carl Bergquist 2016-08-22 14:25:52 +02:00 committed by GitHub
commit 1c25d22f9e
2 changed files with 16 additions and 11 deletions

View File

@ -62,8 +62,8 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
// Called once per panel (graph) // Called once per panel (graph)
this.query = function(options) { this.query = function(options) {
var self = this; var self = this;
var start = getPrometheusTime(options.range.from, false); var start = this.getPrometheusTime(options.range.from, false);
var end = getPrometheusTime(options.range.to, true); var end = this.getPrometheusTime(options.range.to, true);
var queries = []; var queries = [];
var activeTargets = []; var activeTargets = [];
@ -172,8 +172,8 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
step: '60s' step: '60s'
}; };
var start = getPrometheusTime(options.range.from, false); var start = this.getPrometheusTime(options.range.from, false);
var end = getPrometheusTime(options.range.to, true); var end = this.getPrometheusTime(options.range.to, true);
var self = this; var self = this;
return this.performTimeSeriesQuery(query, start, end).then(function(results) { return this.performTimeSeriesQuery(query, start, end).then(function(results) {
@ -279,10 +279,10 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
return metricName + '{' + labelPart + '}'; return metricName + '{' + labelPart + '}';
}; };
function getPrometheusTime(date, roundUp): number { this.getPrometheusTime = function(date, roundUp) {
if (_.isString(date)) { if (_.isString(date)) {
date = dateMath.parse(date, roundUp); date = dateMath.parse(date, roundUp);
} }
return Math.ceil(date.valueOf() / 1000); return Math.ceil(date.valueOf() / 1000);
} };
} }

View File

@ -51,9 +51,11 @@ function (_) {
}); });
}); });
} else { } else {
var start = this.datasource.getPrometheusTime(this.range.from, false);
var end = this.datasource.getPrometheusTime(this.range.to, true);
url = '/api/v1/series?match[]=' + encodeURIComponent(metric) url = '/api/v1/series?match[]=' + encodeURIComponent(metric)
+ '&start=' + (this.range.from.valueOf() / 1000) + '&start=' + start
+ '&end=' + (this.range.to.valueOf() / 1000); + '&end=' + end;
return this.datasource._request('GET', url) return this.datasource._request('GET', url)
.then(function(result) { .then(function(result) {
@ -88,7 +90,8 @@ function (_) {
}; };
PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) { PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (this.range.to.valueOf() / 1000); var end = this.datasource.getPrometheusTime(this.range.to, true);
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + end;
return this.datasource._request('GET', url) return this.datasource._request('GET', url)
.then(function(result) { .then(function(result) {
@ -109,9 +112,11 @@ function (_) {
}; };
PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) { PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
var start = this.datasource.getPrometheusTime(this.range.from, false);
var end = this.datasource.getPrometheusTime(this.range.to, true);
var url = '/api/v1/series?match[]=' + encodeURIComponent(query) var url = '/api/v1/series?match[]=' + encodeURIComponent(query)
+ '&start=' + (this.range.from.valueOf() / 1000) + '&start=' + start
+ '&end=' + (this.range.to.valueOf() / 1000); + '&end=' + end;
var self = this; var self = this;
return this.datasource._request('GET', url) return this.datasource._request('GET', url)