Skip to content

Commit 66bcae3

Browse files
mtandatorkelo
authored andcommitted
(prometheus) pass dashboard time range to template query (grafana#5007)
* (prometheus) pass dashboard time range to template query * (prometheus) add passing time test of templating
1 parent da68f7d commit 66bcae3

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

public/app/plugins/datasource/prometheus/datasource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import PrometheusMetricFindQuery from './metric_find_query';
1010
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
1111

1212
/** @ngInject */
13-
export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv) {
13+
export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
1414
this.type = 'prometheus';
1515
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
1616
this.name = instanceSettings.name;
@@ -145,7 +145,7 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
145145
return $q.reject(err);
146146
}
147147

148-
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated);
148+
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, timeSrv);
149149
return metricFindQuery.process();
150150
};
151151

public/app/plugins/datasource/prometheus/metric_find_query.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
define([
2-
'lodash',
3-
'moment',
2+
'lodash'
43
],
5-
function (_, moment) {
4+
function (_) {
65
'use strict';
76

8-
function PrometheusMetricFindQuery(datasource, query) {
7+
function PrometheusMetricFindQuery(datasource, query, timeSrv) {
98
this.datasource = datasource;
109
this.query = query;
10+
this.range = timeSrv.timeRange();
1111
}
1212

1313
PrometheusMetricFindQuery.prototype.process = function() {
@@ -51,7 +51,9 @@ function (_, moment) {
5151
});
5252
});
5353
} else {
54-
url = '/api/v1/series?match[]=' + encodeURIComponent(metric);
54+
url = '/api/v1/series?match[]=' + encodeURIComponent(metric)
55+
+ '&start=' + (this.range.from.valueOf() / 1000)
56+
+ '&end=' + (this.range.to.valueOf() / 1000);
5557

5658
return this.datasource._request('GET', url)
5759
.then(function(result) {
@@ -86,7 +88,7 @@ function (_, moment) {
8688
};
8789

8890
PrometheusMetricFindQuery.prototype.queryResultQuery = function(query) {
89-
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (moment().valueOf() / 1000);
91+
var url = '/api/v1/query?query=' + encodeURIComponent(query) + '&time=' + (this.range.to.valueOf() / 1000);
9092

9193
return this.datasource._request('GET', url)
9294
.then(function(result) {
@@ -107,7 +109,9 @@ function (_, moment) {
107109
};
108110

109111
PrometheusMetricFindQuery.prototype.metricNameAndLabelsQuery = function(query) {
110-
var url = '/api/v1/series?match[]=' + encodeURIComponent(query);
112+
var url = '/api/v1/series?match[]=' + encodeURIComponent(query)
113+
+ '&start=' + (this.range.from.valueOf() / 1000)
114+
+ '&end=' + (this.range.to.valueOf() / 1000);
111115

112116
var self = this;
113117
return this.datasource._request('GET', url)

public/app/plugins/datasource/prometheus/specs/metric_find_query_specs.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('PrometheusMetricFindQuery', function() {
2828
data: ["value1", "value2", "value3"]
2929
};
3030
ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/resource/values').respond(response);
31-
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)');
31+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(resource)', ctx.timeSrv);
3232
pm.process().then(function(data) { results = data; });
3333
ctx.$httpBackend.flush();
3434
ctx.$rootScope.$apply();
@@ -43,13 +43,22 @@ describe('PrometheusMetricFindQuery', function() {
4343
{__name__: "metric", resource: "value3"}
4444
]
4545
};
46-
ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response);
47-
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)');
46+
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response);
47+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
4848
pm.process().then(function(data) { results = data; });
4949
ctx.$httpBackend.flush();
5050
ctx.$rootScope.$apply();
5151
expect(results.length).to.be(3);
5252
});
53+
it('label_values(metric, resource) should pass correct time', function() {
54+
ctx.timeSrv.setTime({ from: moment.utc('2011-01-01'), to: moment.utc('2015-01-01') });
55+
ctx.$httpBackend.expect('GET',
56+
/proxied\/api\/v1\/series\?match\[\]=metric&start=1293840000&end=1420070400/).respond(response);
57+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
58+
pm.process().then(function(data) { results = data; });
59+
ctx.$httpBackend.flush();
60+
ctx.$rootScope.$apply();
61+
});
5362
it('label_values(metric{label1="foo", label2="bar", label3="baz"}, resource) should generate series query', function() {
5463
response = {
5564
status: "success",
@@ -59,8 +68,8 @@ describe('PrometheusMetricFindQuery', function() {
5968
{__name__: "metric", resource: "value3"}
6069
]
6170
};
62-
ctx.$httpBackend.expect('GET', 'proxied/api/v1/series?match[]=metric').respond(response);
63-
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)');
71+
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/series\?match\[\]=metric&start=.*&end=.*/).respond(response);
72+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'label_values(metric, resource)', ctx.timeSrv);
6473
pm.process().then(function(data) { results = data; });
6574
ctx.$httpBackend.flush();
6675
ctx.$rootScope.$apply();
@@ -72,7 +81,7 @@ describe('PrometheusMetricFindQuery', function() {
7281
data: ["metric1","metric2","metric3","nomatch"]
7382
};
7483
ctx.$httpBackend.expect('GET', 'proxied/api/v1/label/__name__/values').respond(response);
75-
var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)');
84+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'metrics(metric.*)', ctx.timeSrv);
7685
pm.process().then(function(data) { results = data; });
7786
ctx.$httpBackend.flush();
7887
ctx.$rootScope.$apply();
@@ -90,7 +99,7 @@ describe('PrometheusMetricFindQuery', function() {
9099
}
91100
};
92101
ctx.$httpBackend.expect('GET', /proxied\/api\/v1\/query\?query=metric&time=.*/).respond(response);
93-
var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)');
102+
var pm = new PrometheusMetricFindQuery(ctx.ds, 'query_result(metric)', ctx.timeSrv);
94103
pm.process().then(function(data) { results = data; });
95104
ctx.$httpBackend.flush();
96105
ctx.$rootScope.$apply();

public/test/specs/helpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ define([
138138
this.replace = function(target) {
139139
return target;
140140
};
141+
142+
this.setTime = function(time) {
143+
this.time = time;
144+
};
141145
}
142146

143147
function ContextSrvStub() {

0 commit comments

Comments
 (0)