Loki: Fix parsing chained and scalar binary queries (#47132)

* Fix bin parsing for more cases

* Add test for some utils

* Removing console.log

* Simplify some code and fix typing

* Rename function
This commit is contained in:
Andrej Ocenas
2022-04-01 18:33:09 +02:00
committed by GitHub
parent ba2332eb96
commit 1110666b6b
5 changed files with 289 additions and 252 deletions

View File

@ -290,26 +290,58 @@ describe('buildVisualQueryFromString', () => {
it('parses binary query', () => {
expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / rate({project="foo"}[5m])')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'bar',
label: 'project',
},
],
labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }],
binaryQueries: [
{
operator: '/',
query: {
labels: [
labels: [{ op: '=', value: 'foo', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }],
},
},
],
})
);
});
it('parses binary scalar query', () => {
expect(buildVisualQueryFromString('rate({project="bar"}[5m]) / 2')).toEqual(
noErrors({
labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [
{ id: 'rate', params: ['5m'] },
{ id: '__divide_by', params: [2] },
],
})
);
});
it('parses chained binary query', () => {
expect(
buildVisualQueryFromString('rate({project="bar"}[5m]) * 2 / rate({project="foo"}[5m]) + rate({app="test"}[1m])')
).toEqual(
noErrors({
labels: [{ op: '=', value: 'bar', label: 'project' }],
operations: [
{ id: 'rate', params: ['5m'] },
{ id: '__multiply_by', params: [2] },
],
binaryQueries: [
{
operator: '/',
query: {
labels: [{ op: '=', value: 'foo', label: 'project' }],
operations: [{ id: 'rate', params: ['5m'] }],
binaryQueries: [
{
op: '=',
value: 'foo',
label: 'project',
operator: '+',
query: {
labels: [{ op: '=', value: 'test', label: 'app' }],
operations: [{ id: 'rate', params: ['1m'] }],
},
},
],
operations: [{ id: 'rate', params: ['5m'] }],
},
},
],