-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmodifyQuery.ts
More file actions
44 lines (41 loc) · 1.26 KB
/
modifyQuery.ts
File metadata and controls
44 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { escapeFilter, escapeFilterValue, concatenate, LuceneQuery } from 'utils/lucene';
import { AdHocVariableFilter } from '@grafana/data';
/**
* Adds a label:"value" expression to the query.
*/
export function addAddHocFilter(query: string, filter: AdHocVariableFilter): string {
if (!filter.key || !filter.value) {
return query;
}
filter = {
...filter,
// Type is defined as string, but it can be a number.
value: filter.value.toString(),
};
const equalityFilters = ['=', '!='];
if (equalityFilters.includes(filter.operator)) {
return LuceneQuery.parse(query).addFilter(filter.key, filter.value, filter.operator === '=' ? '' : '-').toString();
}
/**
* Keys and values in ad hoc filters may contain characters such as
* colons, which needs to be escaped.
*/
const key = escapeFilter(filter.key);
const value = escapeFilterValue(filter.value);
let addHocFilter = '';
switch (filter.operator) {
case '=~':
addHocFilter = `${key}:/${value}/`;
break;
case '!~':
addHocFilter = `-${key}:/${value}/`;
break;
case '>':
addHocFilter = `${key}:>${value}`;
break;
case '<':
addHocFilter = `${key}:<${value}`;
break;
}
return concatenate(query, addHocFilter,'AND');
}