Commit 65f9970a by David Committed by GitHub

Merge pull request #10434 from craig-miskell-fluxfederation/prometheus_align_queries

Align queries to prometheus with the step
parents ca25a253 a1b92369
......@@ -107,10 +107,18 @@ export class PrometheusDatasource {
return this.templateSrv.variableExists(target.expr);
}
clampRange(start, end, step) {
const clampedEnd = Math.ceil(end / step) * step;
const clampedRange = Math.floor((end - start) / step) * step;
return {
end: clampedEnd,
start: clampedEnd - clampedRange,
};
}
query(options) {
var start = this.getPrometheusTime(options.range.from, false);
var end = this.getPrometheusTime(options.range.to, true);
var range = Math.ceil(end - start);
var queries = [];
var activeTargets = [];
......@@ -123,7 +131,7 @@ export class PrometheusDatasource {
}
activeTargets.push(target);
queries.push(this.createQuery(target, options, range));
queries.push(this.createQuery(target, options, start, end));
}
// No valid targets, return the empty result to save a round trip.
......@@ -133,7 +141,7 @@ export class PrometheusDatasource {
var allQueryPromise = _.map(queries, query => {
if (!query.instant) {
return this.performTimeSeriesQuery(query, start, end);
return this.performTimeSeriesQuery(query, query.start, query.end);
} else {
return this.performInstantQuery(query, end);
}
......@@ -147,7 +155,8 @@ export class PrometheusDatasource {
throw response.error;
}
let transformerOptions = {
// Keeping original start/end for transformers
const transformerOptions = {
format: activeTargets[index].format,
step: queries[index].step,
legendFormat: activeTargets[index].legendFormat,
......@@ -165,9 +174,10 @@ export class PrometheusDatasource {
});
}
createQuery(target, options, range) {
createQuery(target, options, start, end) {
var query: any = {};
query.instant = target.instant;
var range = Math.ceil(end - start);
var interval = kbn.interval_to_seconds(options.interval);
// Minimum interval ("Min step"), if specified for the query. or same as interval otherwise
......@@ -191,6 +201,12 @@ export class PrometheusDatasource {
// Only replace vars in expression after having (possibly) updated interval vars
query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr);
query.requestId = options.panelId + target.refId;
// Align query interval with step
const adjusted = this.clampRange(start, end, query.step);
query.start = adjusted.start;
query.end = adjusted.end;
return query;
}
......@@ -270,22 +286,18 @@ export class PrometheusDatasource {
return this.$q.when([]);
}
var interpolated = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr);
var step = '60s';
if (annotation.step) {
step = this.templateSrv.replace(annotation.step);
}
var step = annotation.step || '60s';
var start = this.getPrometheusTime(options.range.from, false);
var end = this.getPrometheusTime(options.range.to, true);
var query = {
expr: interpolated,
step: this.adjustInterval(kbn.interval_to_seconds(step), 0, Math.ceil(end - start), 1) + 's',
// Unsetting min interval
const queryOptions = {
...options,
interval: '0s',
};
const query = this.createQuery({ expr, interval: step }, queryOptions, start, end);
var self = this;
return this.performTimeSeriesQuery(query, start, end).then(function(results) {
return this.performTimeSeriesQuery(query, query.start, query.end).then(function(results) {
var eventList = [];
tagKeys = tagKeys.split(',');
......
......@@ -14,8 +14,8 @@
data-min-length=0 data-items=1000 ng-model-onblur ng-change="ctrl.refreshMetricData()">
</input>
<info-popover mode="right-absolute">
Controls the name of the time series, using name or pattern. For example <span ng-non-bindable>{{hostname}}</span> will be replaced with label value for
the label hostname.
Controls the name of the time series, using name or pattern. For example
<span ng-non-bindable>{{hostname}}</span> will be replaced with label value for the label hostname.
</info-popover>
</div>
......@@ -25,7 +25,8 @@
placeholder="{{ctrl.panelCtrl.interval}}" data-min-length=0 data-items=100 ng-model-onblur ng-change="ctrl.refreshMetricData()"
/>
<info-popover mode="right-absolute">
Leave blank for auto handling based on time range and panel width
Leave blank for auto handling based on time range and panel width. Note that the actual dates used in the query will be adjusted
to a multiple of the interval step.
</info-popover>
</div>
......@@ -57,4 +58,4 @@
<div class="gf-form-label gf-form-label--grow"></div>
</div>
</div>
</query-editor-row>
</query-editor-row>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment