2014-09-02 00:05:24 -05:00
|
|
|
define([
|
2015-05-15 12:04:49 -05:00
|
|
|
'lodash'
|
2014-09-02 00:05:24 -05:00
|
|
|
],
|
2015-03-26 07:51:29 -05:00
|
|
|
function (_) {
|
2014-09-02 00:05:24 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function InfluxQueryBuilder(target) {
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:03:30 -05:00
|
|
|
function renderTagCondition (tag, index) {
|
|
|
|
var str = "";
|
|
|
|
if (index > 0) {
|
|
|
|
str = (tag.condition || 'AND') + ' ';
|
2015-06-26 03:38:39 -05:00
|
|
|
}
|
2015-06-29 09:03:30 -05:00
|
|
|
|
|
|
|
if (tag.value && tag.value[0] === '/' && tag.value[tag.value.length - 1] === '/') {
|
2015-07-07 13:56:36 -05:00
|
|
|
return str + '"' +tag.key + '"' + ' =~ ' + tag.value;
|
2015-06-29 09:03:30 -05:00
|
|
|
}
|
2015-07-07 13:56:36 -05:00
|
|
|
return str + '"' + tag.key + '"' + " = '" + tag.value + "'";
|
2015-06-26 03:38:39 -05:00
|
|
|
}
|
|
|
|
|
2014-09-02 00:05:24 -05:00
|
|
|
var p = InfluxQueryBuilder.prototype;
|
|
|
|
|
|
|
|
p.build = function() {
|
|
|
|
return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
|
|
|
|
};
|
|
|
|
|
2015-05-18 03:49:34 -05:00
|
|
|
p.buildExploreQuery = function(type, withKey) {
|
|
|
|
var query;
|
|
|
|
var measurement;
|
|
|
|
|
|
|
|
if (type === 'TAG_KEYS') {
|
|
|
|
query = 'SHOW TAG KEYS';
|
2015-07-08 10:41:36 -05:00
|
|
|
measurement = this.target.measurement;
|
2015-05-18 03:49:34 -05:00
|
|
|
} else if (type === 'TAG_VALUES') {
|
|
|
|
query = 'SHOW TAG VALUES';
|
2015-07-08 10:41:36 -05:00
|
|
|
measurement = this.target.measurement;
|
2015-05-18 03:49:34 -05:00
|
|
|
} else if (type === 'MEASUREMENTS') {
|
|
|
|
query = 'SHOW MEASUREMENTS';
|
2015-07-08 10:41:36 -05:00
|
|
|
} else if (type === 'FIELDS') {
|
|
|
|
query = 'SHOW FIELD KEYS FROM "' + this.target.measurement + '"';
|
|
|
|
return query;
|
2015-05-18 03:49:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (measurement) {
|
|
|
|
query += ' FROM "' + measurement + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withKey) {
|
|
|
|
query += ' WITH KEY = "' + withKey + '"';
|
|
|
|
}
|
2015-05-16 09:46:24 -05:00
|
|
|
|
2015-05-18 03:49:34 -05:00
|
|
|
if (this.target.tags && this.target.tags.length > 0) {
|
|
|
|
var whereConditions = _.reduce(this.target.tags, function(memo, tag) {
|
|
|
|
// do not add a condition for the key we want to explore for
|
|
|
|
if (tag.key === withKey) {
|
|
|
|
return memo;
|
|
|
|
}
|
2015-06-29 09:03:30 -05:00
|
|
|
memo.push(renderTagCondition(tag, memo.length));
|
2015-05-18 03:49:34 -05:00
|
|
|
return memo;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (whereConditions.length > 0) {
|
2015-06-29 09:03:30 -05:00
|
|
|
query += ' WHERE ' + whereConditions.join(' ');
|
2015-05-18 03:49:34 -05:00
|
|
|
}
|
2015-05-16 09:46:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
2014-09-02 00:05:24 -05:00
|
|
|
p._buildQuery = function() {
|
|
|
|
var target = this.target;
|
|
|
|
|
2015-03-26 07:51:29 -05:00
|
|
|
if (!target.measurement) {
|
|
|
|
throw "Metric measurement is missing";
|
2014-09-02 00:05:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:36:47 -05:00
|
|
|
if (!target.fields) {
|
|
|
|
target.fields = [{name: 'value', func: target.function || 'mean'}];
|
|
|
|
}
|
|
|
|
|
2015-03-26 07:51:29 -05:00
|
|
|
var query = 'SELECT ';
|
2015-07-09 09:36:47 -05:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < target.fields.length; i++) {
|
|
|
|
var field = target.fields[i];
|
|
|
|
if (i > 0) {
|
|
|
|
query += ', ';
|
|
|
|
}
|
|
|
|
query += field.func + '(' + field.name + ')';
|
|
|
|
}
|
2014-09-02 00:05:24 -05:00
|
|
|
|
2015-07-09 09:36:47 -05:00
|
|
|
var measurement = target.measurement;
|
2015-05-16 09:46:24 -05:00
|
|
|
if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
|
2015-03-26 07:51:29 -05:00
|
|
|
measurement = '"' + measurement+ '"';
|
2014-09-02 00:05:24 -05:00
|
|
|
}
|
|
|
|
|
2015-05-15 12:04:49 -05:00
|
|
|
query += ' FROM ' + measurement + ' WHERE ';
|
2015-06-29 09:03:30 -05:00
|
|
|
var conditions = _.map(target.tags, function(tag, index) {
|
|
|
|
return renderTagCondition(tag, index);
|
2015-05-15 12:04:49 -05:00
|
|
|
});
|
|
|
|
|
2015-06-29 09:03:30 -05:00
|
|
|
query += conditions.join(' ');
|
|
|
|
query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
|
2015-03-26 07:51:29 -05:00
|
|
|
|
|
|
|
query += ' GROUP BY time($interval)';
|
2015-05-15 12:04:49 -05:00
|
|
|
if (target.groupByTags && target.groupByTags.length > 0) {
|
2015-07-07 13:56:36 -05:00
|
|
|
query += ', "' + target.groupByTags.join('", "') + '"';
|
2015-05-15 12:04:49 -05:00
|
|
|
}
|
2014-09-02 00:05:24 -05:00
|
|
|
|
2014-09-04 07:08:31 -05:00
|
|
|
if (target.fill) {
|
|
|
|
query += ' fill(' + target.fill + ')';
|
|
|
|
}
|
|
|
|
|
2014-09-03 02:20:39 -05:00
|
|
|
target.query = query;
|
2014-09-02 00:05:24 -05:00
|
|
|
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
|
|
|
p._modifyRawQuery = function () {
|
|
|
|
var query = this.target.query.replace(";", "");
|
2015-02-25 11:43:44 -06:00
|
|
|
return query;
|
2014-09-02 00:05:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return InfluxQueryBuilder;
|
|
|
|
});
|