Prometheus: Improvements to binary operations, nesting and parantheses handling (#45384)

* Prometheus: Improve query nesting ux

* Prometheus: Add parentheses around nested queries with binary ops

* removed unnessary typing change

* Fixing ts issues

* Improved paranthesis logic

* Fixing unit test

* Progress
This commit is contained in:
Torkel Ödegaard
2022-02-15 21:05:35 +01:00
committed by GitHub
parent 46360ca0c3
commit 52ae586452
7 changed files with 124 additions and 67 deletions

View File

@@ -188,6 +188,76 @@ describe('PromQueryModeller', () => {
).toBe('metric_a + metric_b + metric_c');
});
it('Can render query with nested query with binary op', () => {
expect(
modeller.renderQuery({
metric: 'metric_a',
labels: [],
operations: [],
binaryQueries: [
{
operator: '/',
query: {
metric: 'metric_b',
labels: [],
operations: [{ id: PromOperationId.MultiplyBy, params: [1000] }],
},
},
],
})
).toBe('metric_a / (metric_b * 1000)');
});
it('Can render query with nested binary query with parentheses', () => {
expect(
modeller.renderQuery({
metric: 'metric_a',
labels: [],
operations: [],
binaryQueries: [
{
operator: '/',
query: {
metric: 'metric_b',
labels: [],
operations: [],
binaryQueries: [
{
operator: '*',
query: {
metric: 'metric_c',
labels: [],
operations: [],
},
},
],
},
},
],
})
).toBe('metric_a / (metric_b * metric_c)');
});
it('Should add parantheis around first query if it has binary op', () => {
expect(
modeller.renderQuery({
metric: 'metric_a',
labels: [],
operations: [{ id: PromOperationId.MultiplyBy, params: [1000] }],
binaryQueries: [
{
operator: '/',
query: {
metric: 'metric_b',
labels: [],
operations: [],
},
},
],
})
).toBe('(metric_a * 1000) / metric_b');
});
it('Can render with binary queries with vectorMatches expression', () => {
expect(
modeller.renderQuery({