Commit 080c46f8 by Alin Sinpalean

Address review comments.

parent bb884978
///<reference path="../../../headers/common.d.ts" /> ///<reference path="../../../headers/common.d.ts" />
import _ from 'lodash'; import _ from 'lodash';
import moment from 'moment';
import kbn from 'app/core/utils/kbn'; import kbn from 'app/core/utils/kbn';
import * as dateMath from 'app/core/utils/datemath'; import * as dateMath from 'app/core/utils/datemath';
import PrometheusMetricFindQuery from './metric_find_query'; import PrometheusMetricFindQuery from './metric_find_query';
import TableModel from 'app/core/table_model'; import TableModel from 'app/core/table_model';
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
function prometheusSpecialRegexEscape(value) { function prometheusSpecialRegexEscape(value) {
return value.replace(/[\\^$*+?.()|[\]{}]/g, '\\\\$&'); return value.replace(/[\\^$*+?.()|[\]{}]/g, '\\\\$&');
} }
...@@ -83,6 +80,7 @@ export class PrometheusDatasource { ...@@ -83,6 +80,7 @@ export class PrometheusDatasource {
var self = this; var self = this;
var start = this.getPrometheusTime(options.range.from, false); var start = this.getPrometheusTime(options.range.from, false);
var end = this.getPrometheusTime(options.range.to, true); var end = this.getPrometheusTime(options.range.to, true);
var range = Math.ceil(end - start);
var queries = []; var queries = [];
var activeTargets = []; var activeTargets = [];
...@@ -95,33 +93,7 @@ export class PrometheusDatasource { ...@@ -95,33 +93,7 @@ export class PrometheusDatasource {
} }
activeTargets.push(target); activeTargets.push(target);
queries.push(this.createQuery(target, options, range));
var query: any = {};
query.instant = target.instant;
var interval = this.intervalSeconds(options.interval);
// Minimum interval ("Min step"), if specified for the query. or same as interval otherwise
var minInterval = this.intervalSeconds(this.templateSrv.replace(target.interval, options.scopedVars) || options.interval);
var intervalFactor = target.intervalFactor || 1;
var range = Math.ceil(end - start);
// Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits
var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor);
var scopedVars = options.scopedVars;
// If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars
if (interval !== adjustedInterval) {
interval = adjustedInterval;
scopedVars = Object.assign({}, options.scopedVars, {
"__interval": {text: interval + "s", value: interval + "s"},
"__interval_ms": {text: interval * 1000, value: interval * 1000},
});
}
target.step = query.step = interval;
// Only replace vars in expression after having (possibly) updated interval vars
query.expr = this.templateSrv.replace(target.expr, scopedVars, self.interpolateQueryExpr);
query.requestId = options.panelId + target.refId;
queries.push(query);
} }
// No valid targets, return the empty result to save a round trip. // No valid targets, return the empty result to save a round trip.
...@@ -162,14 +134,41 @@ export class PrometheusDatasource { ...@@ -162,14 +134,41 @@ export class PrometheusDatasource {
}); });
} }
createQuery(target, options, range) {
var query: any = {};
query.instant = target.instant;
var interval = kbn.interval_to_seconds(options.interval);
// Minimum interval ("Min step"), if specified for the query. or same as interval otherwise
var minInterval = kbn.interval_to_seconds(this.templateSrv.replace(target.interval, options.scopedVars) || options.interval);
var intervalFactor = target.intervalFactor || 1;
// Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits
var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor);
var scopedVars = options.scopedVars;
// If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars
if (interval !== adjustedInterval) {
interval = adjustedInterval;
scopedVars = Object.assign({}, options.scopedVars, {
"__interval": {text: interval + "s", value: interval + "s"},
"__interval_ms": {text: interval * 1000, value: interval * 1000},
});
}
target.step = query.step = interval;
// 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;
return query;
}
adjustInterval(interval, minInterval, range, intervalFactor) { adjustInterval(interval, minInterval, range, intervalFactor) {
// Prometheus will drop queries that might return more than 11000 data points. // Prometheus will drop queries that might return more than 11000 data points.
// Calibrate interval if it is too small. // Calibrate interval if it is too small.
if (interval !== 0 && range / intervalFactor / interval > 11000) { if (interval !== 0 && range / intervalFactor / interval > 11000) {
interval = Math.ceil(range / intervalFactor / 11000); interval = Math.ceil(range / intervalFactor / 11000);
} }
interval = Math.max(interval * intervalFactor, minInterval); return Math.max(interval * intervalFactor, minInterval);
return interval;
} }
performTimeSeriesQuery(query, start, end) { performTimeSeriesQuery(query, start, end) {
...@@ -273,21 +272,6 @@ export class PrometheusDatasource { ...@@ -273,21 +272,6 @@ export class PrometheusDatasource {
}); });
} }
calculateInterval(interval, intervalFactor) {
return Math.ceil(this.intervalSeconds(interval) * intervalFactor);
}
intervalSeconds(interval) {
var m = interval.match(durationSplitRegexp);
var dur = moment.duration(parseInt(m[1]), m[2]);
var sec = dur.asSeconds();
if (sec < 1) {
sec = 1;
}
return sec;
}
transformMetricData(md, options, start, end) { transformMetricData(md, options, start, end) {
var dps = [], var dps = [],
metricLabel = null; metricLabel = null;
......
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