2014-09-02 13:05:24 +08:00
|
|
|
define([
|
2015-05-16 01:04:49 +08:00
|
|
|
'lodash'
|
2014-09-02 13:05:24 +08:00
|
|
|
],
|
2015-03-26 20:51:29 +08:00
|
|
|
function (_) {
|
2014-09-02 13:05:24 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function InfluxQueryBuilder(target) {
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:03:30 +08:00
|
|
|
function renderTagCondition (tag, index) {
|
|
|
|
var str = "";
|
|
|
|
if (index > 0) {
|
|
|
|
str = (tag.condition || 'AND') + ' ';
|
2015-06-26 16:38:39 +08:00
|
|
|
}
|
2015-06-29 22:03:30 +08:00
|
|
|
|
|
|
|
if (tag.value && tag.value[0] === '/' && tag.value[tag.value.length - 1] === '/') {
|
2015-07-08 02:56:36 +08:00
|
|
|
return str + '"' +tag.key + '"' + ' =~ ' + tag.value;
|
2015-06-29 22:03:30 +08:00
|
|
|
}
|
2015-07-08 02:56:36 +08:00
|
|
|
return str + '"' + tag.key + '"' + " = '" + tag.value + "'";
|
2015-06-26 16:38:39 +08:00
|
|
|
}
|
|
|
|
|
2014-09-02 13:05:24 +08:00
|
|
|
var p = InfluxQueryBuilder.prototype;
|
|
|
|
|
|
|
|
p.build = function() {
|
|
|
|
return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
|
|
|
|
};
|
|
|
|
|
2015-05-18 16:49:34 +08:00
|
|
|
p.buildExploreQuery = function(type, withKey) {
|
|
|
|
var query;
|
|
|
|
var measurement;
|
|
|
|
|
|
|
|
if (type === 'TAG_KEYS') {
|
|
|
|
query = 'SHOW TAG KEYS';
|
|
|
|
measurement= this.target.measurement;
|
|
|
|
} else if (type === 'TAG_VALUES') {
|
|
|
|
query = 'SHOW TAG VALUES';
|
|
|
|
measurement= this.target.measurement;
|
|
|
|
} else if (type === 'MEASUREMENTS') {
|
|
|
|
query = 'SHOW MEASUREMENTS';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measurement) {
|
|
|
|
query += ' FROM "' + measurement + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withKey) {
|
|
|
|
query += ' WITH KEY = "' + withKey + '"';
|
|
|
|
}
|
2015-05-16 22:46:24 +08:00
|
|
|
|
2015-05-18 16:49:34 +08: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 22:03:30 +08:00
|
|
|
memo.push(renderTagCondition(tag, memo.length));
|
2015-05-18 16:49:34 +08:00
|
|
|
return memo;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (whereConditions.length > 0) {
|
2015-06-29 22:03:30 +08:00
|
|
|
query += ' WHERE ' + whereConditions.join(' ');
|
2015-05-18 16:49:34 +08:00
|
|
|
}
|
2015-05-16 22:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
2014-09-02 13:05:24 +08:00
|
|
|
p._buildQuery = function() {
|
|
|
|
var target = this.target;
|
|
|
|
|
2015-03-26 20:51:29 +08:00
|
|
|
if (!target.measurement) {
|
|
|
|
throw "Metric measurement is missing";
|
2014-09-02 13:05:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 20:51:29 +08:00
|
|
|
var query = 'SELECT ';
|
|
|
|
var measurement = target.measurement;
|
|
|
|
var aggregationFunc = target.function || 'mean';
|
2014-09-02 13:05:24 +08:00
|
|
|
|
2015-05-16 22:46:24 +08:00
|
|
|
if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
|
2015-03-26 20:51:29 +08:00
|
|
|
measurement = '"' + measurement+ '"';
|
2014-09-02 13:05:24 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 20:51:29 +08:00
|
|
|
query += aggregationFunc + '(value)';
|
2015-05-16 01:04:49 +08:00
|
|
|
query += ' FROM ' + measurement + ' WHERE ';
|
2015-06-29 22:03:30 +08:00
|
|
|
var conditions = _.map(target.tags, function(tag, index) {
|
|
|
|
return renderTagCondition(tag, index);
|
2015-05-16 01:04:49 +08:00
|
|
|
});
|
|
|
|
|
2015-06-29 22:03:30 +08:00
|
|
|
query += conditions.join(' ');
|
|
|
|
query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
|
2015-03-26 20:51:29 +08:00
|
|
|
|
|
|
|
query += ' GROUP BY time($interval)';
|
2015-05-16 01:04:49 +08:00
|
|
|
if (target.groupByTags && target.groupByTags.length > 0) {
|
2015-07-08 02:56:36 +08:00
|
|
|
query += ', "' + target.groupByTags.join('", "') + '"';
|
2015-05-16 01:04:49 +08:00
|
|
|
}
|
2014-09-02 13:05:24 +08:00
|
|
|
|
2014-09-04 20:08:31 +08:00
|
|
|
if (target.fill) {
|
|
|
|
query += ' fill(' + target.fill + ')';
|
|
|
|
}
|
|
|
|
|
2015-03-26 20:51:29 +08:00
|
|
|
query += " ORDER BY asc";
|
2014-09-03 15:20:39 +08:00
|
|
|
target.query = query;
|
2014-09-02 13:05:24 +08:00
|
|
|
|
|
|
|
return query;
|
|
|
|
};
|
|
|
|
|
|
|
|
p._modifyRawQuery = function () {
|
|
|
|
var query = this.target.query.replace(";", "");
|
2015-02-26 01:43:44 +08:00
|
|
|
return query;
|
2014-09-02 13:05:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return InfluxQueryBuilder;
|
|
|
|
});
|