Commit 15e6a426 by Torkel Ödegaard

Fixed OR statement for influxdb 0.9 editor, #1525

parent 11170dd3
......@@ -8,11 +8,16 @@ function (_) {
this.target = target;
}
function renderTagCondition (key, value) {
if (value && value[0] === '/' && value[value.length - 1] === '/') {
return key + ' =~ ' + value;
function renderTagCondition (tag, index) {
var str = "";
if (index > 0) {
str = (tag.condition || 'AND') + ' ';
}
return key + " = '" + value + "'";
if (tag.value && tag.value[0] === '/' && tag.value[tag.value.length - 1] === '/') {
return str + tag.key + ' =~ ' + tag.value;
}
return str + tag.key + " = '" + tag.value + "'";
}
var p = InfluxQueryBuilder.prototype;
......@@ -49,12 +54,12 @@ function (_) {
if (tag.key === withKey) {
return memo;
}
memo.push(renderTagCondition(tag.key, tag.value));
memo.push(renderTagCondition(tag, memo.length));
return memo;
}, []);
if (whereConditions.length > 0) {
query += ' WHERE ' + whereConditions.join(' AND ');
query += ' WHERE ' + whereConditions.join(' ');
}
}
......@@ -78,12 +83,12 @@ function (_) {
query += aggregationFunc + '(value)';
query += ' FROM ' + measurement + ' WHERE ';
var conditions = _.map(target.tags, function(tag) {
return renderTagCondition(tag.key, tag.value);
var conditions = _.map(target.tags, function(tag, index) {
return renderTagCondition(tag, index);
});
conditions.push('$timeFilter');
query += conditions.join(' AND ');
query += conditions.join(' ');
query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
query += ' GROUP BY time($interval)';
if (target.groupByTags && target.groupByTags.length > 0) {
......
......@@ -52,6 +52,20 @@ define([
});
});
describe('series with tags OR condition', function() {
var builder = new InfluxQueryBuilder({
measurement: 'cpu',
tags: [{key: 'hostname', value: 'server1'}, {key: 'hostname', value: 'server2', condition: "OR"}]
});
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('SELECT mean(value) FROM "cpu" WHERE hostname = \'server1\' OR hostname = \'server2\' AND ' +
'$timeFilter GROUP BY time($interval) ORDER BY asc');
});
});
describe('series with groupByTag', function() {
it('should generate correct query', function() {
var builder = new InfluxQueryBuilder({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment