mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 16:57:14 -06:00
93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
define([
|
|
'lodash'
|
|
],
|
|
function (_) {
|
|
'use strict';
|
|
|
|
function InfluxQueryBuilder(target, database) {
|
|
this.target = target;
|
|
this.database = database;
|
|
}
|
|
|
|
function renderTagCondition (tag, index) {
|
|
var str = "";
|
|
var operator = tag.operator;
|
|
var value = tag.value;
|
|
if (index > 0) {
|
|
str = (tag.condition || 'AND') + ' ';
|
|
}
|
|
|
|
if (!operator) {
|
|
if (/^\/.*\/$/.test(tag.value)) {
|
|
operator = '=~';
|
|
} else {
|
|
operator = '=';
|
|
}
|
|
}
|
|
|
|
// quote value unless regex
|
|
if (operator !== '=~' && operator !== '!~') {
|
|
value = "'" + value + "'";
|
|
}
|
|
|
|
return str + '"' + tag.key + '" ' + operator + ' ' + value;
|
|
}
|
|
|
|
var p = InfluxQueryBuilder.prototype;
|
|
|
|
p.build = function() {
|
|
return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
|
|
};
|
|
|
|
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';
|
|
} else if (type === 'FIELDS') {
|
|
query = 'SHOW FIELD KEYS FROM "' + this.target.measurement + '"';
|
|
return query;
|
|
} else if (type === 'RETENTION POLICIES') {
|
|
query = 'SHOW RETENTION POLICIES on "' + this.database + '"';
|
|
return query;
|
|
}
|
|
|
|
if (measurement) {
|
|
if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
|
|
measurement = '"' + measurement+ '"';
|
|
}
|
|
query += ' FROM ' + measurement;
|
|
}
|
|
|
|
if (withKey) {
|
|
query += ' WITH KEY = "' + withKey + '"';
|
|
}
|
|
|
|
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;
|
|
}
|
|
memo.push(renderTagCondition(tag, memo.length));
|
|
return memo;
|
|
}, []);
|
|
|
|
if (whereConditions.length > 0) {
|
|
query += ' WHERE ' + whereConditions.join(' ');
|
|
}
|
|
}
|
|
|
|
return query;
|
|
};
|
|
|
|
return InfluxQueryBuilder;
|
|
});
|