mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
(prometheus) pass dashboard time range to template query (#5007)
* (prometheus) pass dashboard time range to template query * (prometheus) add passing time test of templating
This commit is contained in:
parent
da68f7d31a
commit
66bcae353c
@ -10,7 +10,7 @@ import PrometheusMetricFindQuery from './metric_find_query';
|
||||
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
|
||||
|
||||
/** @ngInject */
|
||||
export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv) {
|
||||
export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
|
||||
this.type = 'prometheus';
|
||||
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
|
||||
this.name = instanceSettings.name;
|
||||
@ -145,7 +145,7 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
|
||||
return $q.reject(err);
|
||||
}
|
||||
|
||||
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated);
|
||||
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, timeSrv);
|
||||
return metricFindQuery.process();
|
||||
};
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
define([
|
||||
'lodash',
|
||||
'moment',
|
||||
'lodash'
|
||||
],
|
||||
function (_, moment) {
|
||||
function (_) {
|
||||
'use strict';
|
||||
|
||||
function PrometheusMetricFindQuery(datasource, query) {
|
||||
function PrometheusMetricFindQuery(datasource, query, timeSrv) {
|
||||
this.datasource = datasource;
|
||||
this.query = query;
|
||||
this.range = timeSrv.timeRange();
|
||||
}
|
||||
|
||||
PrometheusMetricFindQuery.prototype.process = function() {
|
||||
@ -51,7 +51,9 @@ function (_, moment) {
|
||||
});
|
||||
});
|
||||
} else {
|
||||
url = '/api/v1/series?match[]=' + encodeURIComponent(metric);
|
||||
url = '/api/v1/series?match[]=' + encodeURIComponent(metric)
|
||||
+ '&start=' + (this.range.from.valueOf() / 1000)
|
||||
+ '&end=' + (this.range.to.valueOf() / 1000);
|
||||
|
||||
return this.datasource._request('GET', url)
|
||||
.then(function(result) {
|
||||
@ -86,7 +88,7 @@ function (_, moment) {
|
||||
};
|
||||
|
||||
PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
|
||||
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (moment().valueOf() / 1000);
|
||||
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (this.range.to.valueOf() / 1000);
|
||||
|
||||
return this.datasource._request('GET', url)
|
||||
.then(function(result) {
|
||||
@ -107,7 +109,9 @@ function (_, moment) {
|
||||
};
|
||||
|
||||
PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
|
||||
var url = '/api/v1/series?match[]=' + encodeURIComponent(query);
|
||||
var url = '/api/v1/series?match[]=' + encodeURIComponent(query)
|
||||
+ '&start=' + (this.range.from.valueOf() / 1000)
|
||||
+ '&end=' + (this.range.to.valueOf() / 1000);
|
||||
|
||||
var self = this;
|
||||
return this.datasource._request('GET', url)
|
||||
|
@ -28,7 +28,7 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
data: ["value1", "value2", "value3"]
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/resource/values').respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)');
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
@ -43,13 +43,22 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
{__name__: "metric", resource: "value3"}
|
||||
]
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)');
|
||||
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
expect(results.length).to.be(3);
|
||||
});
|
||||
it('label_values(metric, resource) should pass correct time', function() {
|
||||
ctx.timeSrv.setTime({ from: moment.utc('2011-01-01'), to: moment.utc('2015-01-01') });
|
||||
ctx.$httpBackend.expect('GET',
|
||||
/proxied\/api\/v1\/series\?match\[\]=metric&start=1293840000&end=1420070400/).respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
});
|
||||
it('label_values(metric{label1="foo", label2="bar", label3="baz"}, resource) should generate series query', function() {
|
||||
response = {
|
||||
status: "success",
|
||||
@ -59,8 +68,8 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
{__name__: "metric", resource: "value3"}
|
||||
]
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)');
|
||||
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
@ -72,7 +81,7 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
data: ["metric1","metric2","metric3","nomatch"]
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/__name__/values').respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)');
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
@ -90,7 +99,7 @@ describe('PrometheusMetricFindQuery', function() {
|
||||
}
|
||||
};
|
||||
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/query\?query=metric&time=.*/).respond(response);
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)');
|
||||
var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)', ctx.timeSrv);
|
||||
pm.process().then(function(data) { results = data; });
|
||||
ctx.$httpBackend.flush();
|
||||
ctx.$rootScope.$apply();
|
||||
|
@ -138,6 +138,10 @@ define([
|
||||
this.replace = function(target) {
|
||||
return target;
|
||||
};
|
||||
|
||||
this.setTime = function(time) {
|
||||
this.time = time;
|
||||
};
|
||||
}
|
||||
|
||||
function ContextSrvStub() {
|
||||
|
Loading…
Reference in New Issue
Block a user