From 1f6266ad1f1262a2b3c30433c7124c6054b6d921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Mon, 24 Nov 2025 15:27:09 +0100 Subject: [PATCH] feat(ncHistogramApplyTemplate): simple templating of native/classic queries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a simple templating function to be able to apply to native classic queries. Signed-off-by: György Krajcsovits --- .../test/test_native-classic-histogram.libsonnet | 10 ++++++++++ mixin-utils/utils.libsonnet | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/mixin-utils/test/test_native-classic-histogram.libsonnet b/mixin-utils/test/test_native-classic-histogram.libsonnet index f5139ef96..8a2f90e7b 100644 --- a/mixin-utils/test/test_native-classic-histogram.libsonnet +++ b/mixin-utils/test/test_native-classic-histogram.libsonnet @@ -228,3 +228,13 @@ test.new(std.thisFile) }, ) ) ++ test.case.new( + name='simple templating', + test=test.expect.eq( + actual=utils.ncHistogramApplyTemplate('label_replace(%s, "x", "$1", "y", "(.*)")', { classic: 'classic_query', native: 'native_query' }), + expected={ + classic: 'label_replace(classic_query, "x", "$1", "y", "(.*)")', + native: 'label_replace(native_query, "x", "$1", "y", "(.*)")', + } + ) +) diff --git a/mixin-utils/utils.libsonnet b/mixin-utils/utils.libsonnet index 84c46c69d..01eeda884 100644 --- a/mixin-utils/utils.libsonnet +++ b/mixin-utils/utils.libsonnet @@ -170,6 +170,16 @@ local g = import 'grafana-builder/grafana.libsonnet'; }, }, + // ncHistogramApplyTemplate (native classic histogram template applier) + // Takes a template like 'label_replace(%s, "x", "$1", "y", ".*")' + // with a single substitution and applies to both the classic and native + // histogram query. + ncHistogramApplyTemplate(template, query):: { + assert $.isNativeClassicQuery(query), + native: template % query.native, + classic: template % query.classic, + }, + // ncHistogramComment (native classic histogram comment) helps attach // comments to the query and also keep multiline strings where applicable. ncHistogramComment(query, comment):: {