Skip to content

Commit cafeea4

Browse files
Fix filter (#132)
* fixbug and add a ut of repace logic * fixbug, stream field filter, syntax error * add CHANGELOG.md * fix CHANGELOG.md --------- Co-authored-by: worker24h <[email protected]>
1 parent 016423c commit cafeea4

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* FEATURE: implement a toggle to switch between instant and range requests. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/111).
77
* FEATURE: add options to configure the legend template, limit for number of log lines, and step. See [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/114) and [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/124) issues.
88

9+
* BUGFIX: fix support mulit options with label values of stream-fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/109).
10+
911
## v0.9.0
1012

1113
* FEATURE: Add support for the `$__range` variable in queries. It will be transformed to the `[time_from, time_to]` in the Unix format. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/112).
@@ -44,7 +46,7 @@
4446
Thanks to @yincongcyincong for [the pull request](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/69).
4547

4648
* BUGFIX: correctly pass time range filter when querying variable values. Before, time filter wasn't applied for `/field_values` and `/field_names` API calls. See [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/71) and [this](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/72) issues.
47-
* BUGFIX: fix the issue with displaying incorrect subfields when requesting logs with different set of fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/60).
49+
* BUGFIX: fix the issue with displaying incorrect subfields when requesting logs with different set of fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/60).
4850

4951
## v0.4.0
5052

src/datasource.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ describe('VictoriaLogsDatasource', () => {
113113
expect(replacedQuery.expr).toBe('foo: "bar"');
114114
});
115115

116+
it('should replace $var with an | expression for stream field when given an array of values', () => {
117+
const scopedVars = {
118+
var: { text: 'foo,bar', value: ['foo', 'bar'] },
119+
};
120+
const templateSrvMock = {
121+
replace: jest.fn((a: string) => a?.replace('$var', '("foo" OR "bar")')),
122+
} as unknown as TemplateSrv;
123+
const ds = createDatasource(templateSrvMock);
124+
const replacedQuery = ds.applyTemplateVariables(
125+
{ expr: '_stream{val=~"$var"}', refId: 'A' },
126+
scopedVars
127+
);
128+
expect(replacedQuery.expr).toBe('_stream{val=~"("foo"|"bar")"}');
129+
});
130+
116131
it('should replace $var with an OR expression when given an array of values', () => {
117132
const scopedVars = {
118133
var: { text: 'foo,bar', value: ['foo', 'bar'] },

src/datasource.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class VictoriaLogsDatasource
156156
return {
157157
...target,
158158
legendFormat: this.templateSrv.replace(target.legendFormat, rest),
159-
expr: this.templateSrv.replace(exprWithAdHoc, variables, this.interpolateQueryExpr),
159+
expr: this.interpolateString(exprWithAdHoc, variables),
160160
};
161161
}
162162

@@ -256,7 +256,21 @@ export class VictoriaLogsDatasource
256256
}
257257

258258
interpolateString(string: string, scopedVars?: ScopedVars) {
259-
return this.templateSrv.replace(string, scopedVars, this.interpolateQueryExpr);
259+
let expr: string = this.templateSrv.replace(string, scopedVars, this.interpolateQueryExpr);
260+
if (expr) {
261+
// Replace "OR" with "|" inside parentheses
262+
// The stream field filter, uses "|" as the operator for multiple values.
263+
// expr => {k="v", k2=~"(v2 OR v3 OR v4)"} Test
264+
// newExpr => {k="v", k2=~"(v2|v3|v4)"} Test
265+
expr = expr.replace(/\{([^}]+)\}/g, (outerMatch, outerContent) => {
266+
const replacedContent = outerContent.replace(/\(([^)]+)\)/g, (match: string, content: string) => {
267+
const replaced = content.replace(/\s*OR\s*/g, '|');
268+
return `(${replaced})`;
269+
});
270+
return `{${replacedContent}}`;
271+
});
272+
}
273+
return expr;
260274
}
261275

262276
private async processMetricFindQuery(query: VariableQuery, timeRange?: TimeRange): Promise<MetricFindValue[]> {

0 commit comments

Comments
 (0)