From ba2332eb96ae555509f381f9c3c4aa62043e234c Mon Sep 17 00:00:00 2001 From: Andrej Ocenas Date: Fri, 1 Apr 2022 17:51:50 +0200 Subject: [PATCH] Prometheus: bool bin op improvements (#47198) * Change param order * Align tooltip icon styling --- .../querybuilder/PromQueryModeller.test.ts | 15 +++++++++++++++ .../querybuilder/binaryScalarOperations.ts | 7 +++---- .../prometheus/querybuilder/parsing.test.ts | 4 ++-- .../datasource/prometheus/querybuilder/parsing.ts | 2 +- .../querybuilder/shared/OperationEditor.tsx | 4 ++++ 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/querybuilder/PromQueryModeller.test.ts b/public/app/plugins/datasource/prometheus/querybuilder/PromQueryModeller.test.ts index d4ef1582682..f81ab15cbc9 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/PromQueryModeller.test.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/PromQueryModeller.test.ts @@ -317,4 +317,19 @@ describe('PromQueryModeller', () => { }) ).toBe('metric_a / on(le, foo) metric_b'); }); + + it('can render bool in binary ops', () => { + expect( + modeller.renderQuery({ + metric: 'cluster_namespace_slug_dialer_name', + labels: [], + operations: [ + { + id: '__less_or_equal', + params: [2, true], + }, + ], + }) + ).toBe('cluster_namespace_slug_dialer_name <= bool 2'); + }); }); diff --git a/public/app/plugins/datasource/prometheus/querybuilder/binaryScalarOperations.ts b/public/app/plugins/datasource/prometheus/querybuilder/binaryScalarOperations.ts index 993545a5bb9..6a7876f7432 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/binaryScalarOperations.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/binaryScalarOperations.ts @@ -77,12 +77,12 @@ export const binaryScalarOperations: QueryBuilderOperationDef[] = binaryScalarDe const params: QueryBuilderOperationParamDef[] = [{ name: 'Value', type: 'number' }]; const defaultParams: any[] = [2]; if (opDef.comparison) { - params.unshift({ + params.push({ name: 'Bool', type: 'boolean', description: 'If checked comparison will return 0 or 1 for the value rather than filtering.', }); - defaultParams.unshift(false); + defaultParams.push(false); } return { @@ -102,8 +102,7 @@ function getSimpleBinaryRenderer(operator: string) { let param = model.params[0]; let bool = ''; if (model.params.length === 2) { - param = model.params[1]; - bool = model.params[0] ? ' bool' : ''; + bool = model.params[1] ? ' bool' : ''; } return `${innerExpr} ${operator}${bool} ${param}`; diff --git a/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts b/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts index 96582c9b7c1..fc7ffb8142c 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/parsing.test.ts @@ -422,7 +422,7 @@ describe('buildVisualQueryFromString', () => { operations: [ { id: '__less_or_equal', - params: [false, 2.5], + params: [2.5, false], }, ], }, @@ -438,7 +438,7 @@ describe('buildVisualQueryFromString', () => { operations: [ { id: '__less_or_equal', - params: [true, 2], + params: [2, true], }, ], }, diff --git a/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts b/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts index f53ed7cb316..8d1a7908bf3 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts +++ b/public/app/plugins/datasource/prometheus/querybuilder/parsing.ts @@ -438,7 +438,7 @@ function makeBinOp( ) { const params: any[] = [parseFloat(getString(expr, numberNode))]; if (opDef.comparison) { - params.unshift(hasBool); + params.push(hasBool); } return { id: opDef.id, diff --git a/public/app/plugins/datasource/prometheus/querybuilder/shared/OperationEditor.tsx b/public/app/plugins/datasource/prometheus/querybuilder/shared/OperationEditor.tsx index 8b584368a0f..4574ff7f829 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/shared/OperationEditor.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/shared/OperationEditor.tsx @@ -201,6 +201,10 @@ const getStyles = (theme: GrafanaTheme2) => { }), infoIcon: css({ marginLeft: theme.spacing(0.5), + color: theme.colors.text.secondary, + ':hover': { + color: theme.colors.text.primary, + }, }), body: css({ margin: theme.spacing(1, 1, 0.5, 1),