2015-09-30 10:16:34 -05:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
|
|
|
|
2015-12-22 06:59:11 -06:00
|
|
|
import _ from 'lodash';
|
2015-12-16 05:21:13 -06:00
|
|
|
import queryPart from './query_part';
|
2016-09-14 10:36:28 -05:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
2015-09-30 10:16:34 -05:00
|
|
|
|
2015-12-16 05:21:13 -06:00
|
|
|
export default class InfluxQuery {
|
2015-09-30 10:16:34 -05:00
|
|
|
target: any;
|
2015-11-24 10:01:18 -06:00
|
|
|
selectModels: any[];
|
2015-09-30 10:16:34 -05:00
|
|
|
queryBuilder: any;
|
2016-02-08 03:31:26 -06:00
|
|
|
groupByParts: any;
|
2016-03-01 07:36:55 -06:00
|
|
|
templateSrv: any;
|
|
|
|
scopedVars: any;
|
2015-09-30 10:16:34 -05:00
|
|
|
|
2016-03-23 08:42:18 -05:00
|
|
|
/** @ngInject */
|
2016-03-01 09:33:01 -06:00
|
|
|
constructor(target, templateSrv?, scopedVars?) {
|
2015-09-30 10:16:34 -05:00
|
|
|
this.target = target;
|
2016-03-01 07:36:55 -06:00
|
|
|
this.templateSrv = templateSrv;
|
|
|
|
this.scopedVars = scopedVars;
|
2015-09-30 10:16:34 -05:00
|
|
|
|
2016-01-17 10:53:38 -06:00
|
|
|
target.policy = target.policy || 'default';
|
2015-12-03 08:09:39 -06:00
|
|
|
target.dsType = 'influxdb';
|
|
|
|
target.resultFormat = target.resultFormat || 'time_series';
|
2017-03-05 16:13:46 -06:00
|
|
|
target.orderByTime = target.orderByTime || 'ASC';
|
2015-09-30 10:16:34 -05:00
|
|
|
target.tags = target.tags || [];
|
2015-11-27 09:35:40 -06:00
|
|
|
target.groupBy = target.groupBy || [
|
2017-01-11 03:18:21 -06:00
|
|
|
{type: 'time', params: ['$__interval']},
|
2015-11-27 09:35:40 -06:00
|
|
|
{type: 'fill', params: ['null']},
|
|
|
|
];
|
2015-09-30 10:16:34 -05:00
|
|
|
target.select = target.select || [[
|
2015-11-25 05:30:56 -06:00
|
|
|
{type: 'field', params: ['value']},
|
|
|
|
{type: 'mean', params: []},
|
2015-09-30 10:16:34 -05:00
|
|
|
]];
|
|
|
|
|
2015-11-25 07:27:22 -06:00
|
|
|
this.updateProjection();
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:27:22 -06:00
|
|
|
updateProjection() {
|
2015-11-24 10:01:18 -06:00
|
|
|
this.selectModels = _.map(this.target.select, function(parts: any) {
|
2015-11-25 03:22:20 -06:00
|
|
|
return _.map(parts, queryPart.create);
|
|
|
|
});
|
2015-11-25 07:27:22 -06:00
|
|
|
this.groupByParts = _.map(this.target.groupBy, queryPart.create);
|
2015-11-25 03:22:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
updatePersistedParts() {
|
|
|
|
this.target.select = _.map(this.selectModels, function(selectParts) {
|
|
|
|
return _.map(selectParts, function(part: any) {
|
2015-11-25 05:30:56 -06:00
|
|
|
return {type: part.def.type, params: part.params};
|
2015-11-25 03:22:20 -06:00
|
|
|
});
|
2015-09-30 10:16:34 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:27:22 -06:00
|
|
|
hasGroupByTime() {
|
2015-11-30 01:40:11 -06:00
|
|
|
return _.find(this.target.groupBy, (g: any) => g.type === 'time');
|
2015-11-25 07:27:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hasFill() {
|
2015-11-30 01:40:11 -06:00
|
|
|
return _.find(this.target.groupBy, (g: any) => g.type === 'fill');
|
2015-11-25 07:27:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
addGroupBy(value) {
|
|
|
|
var stringParts = value.match(/^(\w+)\((.*)\)$/);
|
|
|
|
var typePart = stringParts[1];
|
|
|
|
var arg = stringParts[2];
|
|
|
|
var partModel = queryPart.create({type: typePart, params: [arg]});
|
2015-11-30 01:40:11 -06:00
|
|
|
var partCount = this.target.groupBy.length;
|
|
|
|
|
|
|
|
if (partCount === 0) {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
} else if (typePart === 'time') {
|
|
|
|
this.target.groupBy.splice(0, 0, partModel.part);
|
|
|
|
} else if (typePart === 'tag') {
|
|
|
|
if (this.target.groupBy[partCount-1].type === 'fill') {
|
|
|
|
this.target.groupBy.splice(partCount-1, 0, partModel.part);
|
|
|
|
} else {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.target.groupBy.push(partModel.part);
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:27:22 -06:00
|
|
|
this.updateProjection();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeGroupByPart(part, index) {
|
2015-11-30 03:14:42 -06:00
|
|
|
var categories = queryPart.getCategories();
|
|
|
|
|
|
|
|
if (part.def.type === 'time') {
|
|
|
|
// remove fill
|
|
|
|
this.target.groupBy = _.filter(this.target.groupBy, (g: any) => g.type !== 'fill');
|
|
|
|
// remove aggregations
|
|
|
|
this.target.select = _.map(this.target.select, (s: any) => {
|
|
|
|
return _.filter(s, (part: any) => {
|
|
|
|
var partModel = queryPart.create(part);
|
|
|
|
if (partModel.def.category === categories.Aggregations) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-30 09:28:56 -06:00
|
|
|
if (partModel.def.category === categories.Selectors) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-30 03:14:42 -06:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:27:22 -06:00
|
|
|
this.target.groupBy.splice(index, 1);
|
|
|
|
this.updateProjection();
|
|
|
|
}
|
|
|
|
|
2015-09-30 10:16:34 -05:00
|
|
|
removeSelect(index: number) {
|
|
|
|
this.target.select.splice(index, 1);
|
2015-11-25 07:27:22 -06:00
|
|
|
this.updateProjection();
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:22:20 -06:00
|
|
|
removeSelectPart(selectParts, part) {
|
2015-11-25 05:30:56 -06:00
|
|
|
// if we remove the field remove the whole statement
|
|
|
|
if (part.def.type === 'field') {
|
|
|
|
if (this.selectModels.length > 1) {
|
|
|
|
var modelsIndex = _.indexOf(this.selectModels, selectParts);
|
|
|
|
this.selectModels.splice(modelsIndex, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var partIndex = _.indexOf(selectParts, part);
|
|
|
|
selectParts.splice(partIndex, 1);
|
|
|
|
}
|
2015-11-24 10:01:18 -06:00
|
|
|
|
2015-11-25 03:22:20 -06:00
|
|
|
this.updatePersistedParts();
|
2015-11-24 10:01:18 -06:00
|
|
|
}
|
|
|
|
|
2015-11-25 05:30:56 -06:00
|
|
|
addSelectPart(selectParts, type) {
|
|
|
|
var partModel = queryPart.create({type: type});
|
|
|
|
partModel.def.addStrategy(selectParts, partModel, this);
|
|
|
|
this.updatePersistedParts();
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 09:33:01 -06:00
|
|
|
private renderTagCondition(tag, index, interpolate) {
|
2015-09-30 10:16:34 -05:00
|
|
|
var str = "";
|
|
|
|
var operator = tag.operator;
|
|
|
|
var value = tag.value;
|
|
|
|
if (index > 0) {
|
|
|
|
str = (tag.condition || 'AND') + ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!operator) {
|
2016-03-01 07:36:55 -06:00
|
|
|
if (/^\/.*\/$/.test(value)) {
|
2015-09-30 10:16:34 -05:00
|
|
|
operator = '=~';
|
|
|
|
} else {
|
|
|
|
operator = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// quote value unless regex
|
|
|
|
if (operator !== '=~' && operator !== '!~') {
|
2016-03-01 09:33:01 -06:00
|
|
|
if (interpolate) {
|
|
|
|
value = this.templateSrv.replace(value, this.scopedVars);
|
|
|
|
}
|
2016-05-03 03:25:06 -05:00
|
|
|
if (operator !== '>' && operator !== '<') {
|
2016-06-02 05:08:13 -05:00
|
|
|
value = "'" + value.replace(/\\/g, '\\\\') + "'";
|
2016-04-14 19:08:54 -05:00
|
|
|
}
|
2016-09-14 10:36:28 -05:00
|
|
|
} else if (interpolate) {
|
2016-03-01 07:36:55 -06:00
|
|
|
value = this.templateSrv.replace(value, this.scopedVars, 'regex');
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return str + '"' + tag.key + '" ' + operator + ' ' + value;
|
|
|
|
}
|
|
|
|
|
2016-04-25 07:29:51 -05:00
|
|
|
getMeasurementAndPolicy(interpolate) {
|
2016-01-19 02:08:55 -06:00
|
|
|
var policy = this.target.policy;
|
2016-04-27 03:16:04 -05:00
|
|
|
var measurement = this.target.measurement || 'measurement';
|
2016-01-18 11:38:36 -06:00
|
|
|
|
2017-03-06 00:47:50 -06:00
|
|
|
if (!measurement.match('^/.*/$')) {
|
2016-01-18 11:38:36 -06:00
|
|
|
measurement = '"' + measurement+ '"';
|
2016-04-25 07:29:51 -05:00
|
|
|
} else if (interpolate) {
|
2016-04-25 07:22:15 -05:00
|
|
|
measurement = this.templateSrv.replace(measurement, this.scopedVars, 'regex');
|
2016-01-18 11:38:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (policy !== 'default') {
|
|
|
|
policy = '"' + this.target.policy + '".';
|
|
|
|
} else {
|
|
|
|
policy = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return policy + measurement;
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:36:28 -05:00
|
|
|
interpolateQueryStr(value, variable, defaultFormatFn) {
|
|
|
|
// if no multi or include all do not regexEscape
|
|
|
|
if (!variable.multi && !variable.includeAll) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return kbn.regexEscape(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
var escapedValues = _.map(value, kbn.regexEscape);
|
2017-04-25 10:27:57 -05:00
|
|
|
return '(' + escapedValues.join('|') + ')';
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-09-14 10:36:28 -05:00
|
|
|
|
2016-03-01 09:33:01 -06:00
|
|
|
render(interpolate?) {
|
2015-09-30 10:16:34 -05:00
|
|
|
var target = this.target;
|
|
|
|
|
2015-11-25 05:30:56 -06:00
|
|
|
if (target.rawQuery) {
|
2016-03-01 09:33:01 -06:00
|
|
|
if (interpolate) {
|
2016-09-14 10:36:28 -05:00
|
|
|
return this.templateSrv.replace(target.query, this.scopedVars, this.interpolateQueryStr);
|
2016-03-01 09:33:01 -06:00
|
|
|
} else {
|
|
|
|
return target.query;
|
|
|
|
}
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var query = 'SELECT ';
|
|
|
|
var i, y;
|
2015-11-24 10:01:18 -06:00
|
|
|
for (i = 0; i < this.selectModels.length; i++) {
|
2015-11-25 03:22:20 -06:00
|
|
|
let parts = this.selectModels[i];
|
2015-09-30 10:16:34 -05:00
|
|
|
var selectText = "";
|
|
|
|
for (y = 0; y < parts.length; y++) {
|
|
|
|
let part = parts[y];
|
|
|
|
selectText = part.render(selectText);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
query += ', ';
|
|
|
|
}
|
|
|
|
query += selectText;
|
|
|
|
}
|
|
|
|
|
2016-04-25 07:29:51 -05:00
|
|
|
query += ' FROM ' + this.getMeasurementAndPolicy(interpolate) + ' WHERE ';
|
2015-09-30 10:16:34 -05:00
|
|
|
var conditions = _.map(target.tags, (tag, index) => {
|
2016-03-01 09:33:01 -06:00
|
|
|
return this.renderTagCondition(tag, index, interpolate);
|
2015-09-30 10:16:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
query += conditions.join(' ');
|
|
|
|
query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
|
|
|
|
|
2015-11-27 09:35:40 -06:00
|
|
|
var groupBySection = "";
|
2015-11-25 07:27:22 -06:00
|
|
|
for (i = 0; i < this.groupByParts.length; i++) {
|
|
|
|
var part = this.groupByParts[i];
|
|
|
|
if (i > 0) {
|
2015-11-27 09:35:40 -06:00
|
|
|
// for some reason fill has no seperator
|
|
|
|
groupBySection += part.def.type === 'fill' ? ' ' : ', ';
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
2015-11-27 09:35:40 -06:00
|
|
|
groupBySection += part.render('');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupBySection.length) {
|
|
|
|
query += ' GROUP BY ' + groupBySection;
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target.fill) {
|
|
|
|
query += ' fill(' + target.fill + ')';
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:41:16 -06:00
|
|
|
if (target.orderByTime === 'DESC') {
|
|
|
|
query += ' ORDER BY time DESC';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target.limit) {
|
|
|
|
query += ' LIMIT ' + target.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target.slimit) {
|
|
|
|
query += ' SLIMIT ' + target.slimit;
|
|
|
|
}
|
|
|
|
|
2015-09-30 10:16:34 -05:00
|
|
|
return query;
|
|
|
|
}
|
2016-09-20 09:29:48 -05:00
|
|
|
|
|
|
|
renderAdhocFilters(filters) {
|
|
|
|
var conditions = _.map(filters, (tag, index) => {
|
|
|
|
return this.renderTagCondition(tag, index, false);
|
|
|
|
});
|
|
|
|
return conditions.join(' ');
|
|
|
|
}
|
2015-09-30 10:16:34 -05:00
|
|
|
}
|