2014-09-02 07:05:24 +02:00
|
|
|
define([
|
2015-05-15 19:04:49 +02:00
|
|
|
'lodash'
|
2014-09-02 07:05:24 +02:00
|
|
|
],
|
2015-03-26 13:51:29 +01:00
|
|
|
function (_) {
|
2014-09-02 07:05:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-01-18 07:28:01 +01:00
|
|
|
function InfluxQueryBuilder(target, database) {
|
2014-09-02 07:05:24 +02:00
|
|
|
this.target = target;
|
2016-01-18 07:28:01 +01:00
|
|
|
this.database = database;
|
2014-09-02 07:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-29 16:03:30 +02:00
|
|
|
function renderTagCondition (tag, index) {
|
|
|
|
|
var str = "";
|
2015-08-10 09:09:39 +02:00
|
|
|
var operator = tag.operator;
|
|
|
|
|
var value = tag.value;
|
2015-06-29 16:03:30 +02:00
|
|
|
if (index > 0) {
|
|
|
|
|
str = (tag.condition || 'AND') + ' ';
|
2015-06-26 10:38:39 +02:00
|
|
|
}
|
2015-06-29 16:03:30 +02:00
|
|
|
|
2015-08-10 09:09:39 +02:00
|
|
|
if (!operator) {
|
|
|
|
|
if (/^\/.*\/$/.test(tag.value)) {
|
|
|
|
|
operator = '=~';
|
|
|
|
|
} else {
|
|
|
|
|
operator = '=';
|
|
|
|
|
}
|
2015-06-29 16:03:30 +02:00
|
|
|
}
|
2015-08-08 13:31:37 +02:00
|
|
|
|
2016-04-14 17:08:54 -07:00
|
|
|
// quote value unless regex or number
|
|
|
|
|
if (operator !== '=~' && operator !== '!~' && isNaN(+value)) {
|
2015-08-10 09:09:39 +02:00
|
|
|
value = "'" + value + "'";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return str + '"' + tag.key + '" ' + operator + ' ' + value;
|
2015-06-26 10:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-02 07:05:24 +02:00
|
|
|
var p = InfluxQueryBuilder.prototype;
|
|
|
|
|
|
|
|
|
|
p.build = function() {
|
|
|
|
|
return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-15 10:11:38 -04:00
|
|
|
p.buildExploreQuery = function(type, withKey, withMeasurementFilter) {
|
2015-05-18 10:49:34 +02:00
|
|
|
var query;
|
|
|
|
|
var measurement;
|
|
|
|
|
|
|
|
|
|
if (type === 'TAG_KEYS') {
|
|
|
|
|
query = 'SHOW TAG KEYS';
|
2015-07-08 17:41:36 +02:00
|
|
|
measurement = this.target.measurement;
|
2015-05-18 10:49:34 +02:00
|
|
|
} else if (type === 'TAG_VALUES') {
|
|
|
|
|
query = 'SHOW TAG VALUES';
|
2015-07-08 17:41:36 +02:00
|
|
|
measurement = this.target.measurement;
|
2015-05-18 10:49:34 +02:00
|
|
|
} else if (type === 'MEASUREMENTS') {
|
|
|
|
|
query = 'SHOW MEASUREMENTS';
|
2016-09-15 10:11:38 -04:00
|
|
|
if (withMeasurementFilter)
|
|
|
|
|
{
|
|
|
|
|
query += ' WITH MEASUREMENT =~ /' + withMeasurementFilter +'/';
|
|
|
|
|
}
|
2015-07-08 17:41:36 +02:00
|
|
|
} else if (type === 'FIELDS') {
|
2016-12-16 17:08:04 +01:00
|
|
|
if (!this.target.measurement.match('^/.*/')) {
|
|
|
|
|
return 'SHOW FIELD KEYS FROM "' + this.target.measurement + '"';
|
|
|
|
|
} else {
|
|
|
|
|
return 'SHOW FIELD KEYS FROM ' + this.target.measurement;
|
|
|
|
|
}
|
2016-01-17 17:53:38 +01:00
|
|
|
} else if (type === 'RETENTION POLICIES') {
|
2016-01-18 07:28:01 +01:00
|
|
|
query = 'SHOW RETENTION POLICIES on "' + this.database + '"';
|
2016-01-17 17:53:38 +01:00
|
|
|
return query;
|
2015-05-18 10:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (measurement) {
|
2015-07-31 09:17:14 +02:00
|
|
|
if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
|
|
|
|
|
measurement = '"' + measurement+ '"';
|
|
|
|
|
}
|
|
|
|
|
query += ' FROM ' + measurement;
|
2015-05-18 10:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (withKey) {
|
|
|
|
|
query += ' WITH KEY = "' + withKey + '"';
|
|
|
|
|
}
|
2015-05-16 16:46:24 +02:00
|
|
|
|
2015-05-18 10:49:34 +02: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 16:03:30 +02:00
|
|
|
memo.push(renderTagCondition(tag, memo.length));
|
2015-05-18 10:49:34 +02:00
|
|
|
return memo;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
if (whereConditions.length > 0) {
|
2015-06-29 16:03:30 +02:00
|
|
|
query += ' WHERE ' + whereConditions.join(' ');
|
2015-05-18 10:49:34 +02:00
|
|
|
}
|
2015-05-16 16:46:24 +02:00
|
|
|
}
|
2017-04-24 04:44:29 -05:00
|
|
|
if (type === 'MEASUREMENTS')
|
|
|
|
|
{
|
|
|
|
|
query += ' LIMIT 100';
|
|
|
|
|
//Solve issue #2524 by limiting the number of measurements returned
|
|
|
|
|
//LIMIT must be after WITH MEASUREMENT and WHERE clauses
|
|
|
|
|
//This also could be used for TAG KEYS and TAG VALUES, if desired
|
|
|
|
|
}
|
2015-05-16 16:46:24 +02:00
|
|
|
return query;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-02 07:05:24 +02:00
|
|
|
return InfluxQueryBuilder;
|
|
|
|
|
});
|