Skip to content

Commit a64f0ca

Browse files
authored
feat(ncHistogramApplyTemplate): simple templating of native/classic queries (#1555)
Add a simple templating function to be able to apply to native classic queries. Signed-off-by: György Krajcsovits <[email protected]>
1 parent addd7cd commit a64f0ca

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mixin-utils/test/test_native-classic-histogram.libsonnet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,13 @@ test.new(std.thisFile)
228228
},
229229
)
230230
)
231+
+ test.case.new(
232+
name='simple templating',
233+
test=test.expect.eq(
234+
actual=utils.ncHistogramApplyTemplate('label_replace(%s, "x", "$1", "y", "(.*)")', { classic: 'classic_query', native: 'native_query' }),
235+
expected={
236+
classic: 'label_replace(classic_query, "x", "$1", "y", "(.*)")',
237+
native: 'label_replace(native_query, "x", "$1", "y", "(.*)")',
238+
}
239+
)
240+
)

mixin-utils/utils.libsonnet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ local g = import 'grafana-builder/grafana.libsonnet';
170170
},
171171
},
172172

173+
// ncHistogramApplyTemplate (native classic histogram template applier)
174+
// Takes a template like 'label_replace(%s, "x", "$1", "y", ".*")'
175+
// with a single substitution and applies to both the classic and native
176+
// histogram query.
177+
ncHistogramApplyTemplate(template, query):: {
178+
assert $.isNativeClassicQuery(query),
179+
native: template % query.native,
180+
classic: template % query.classic,
181+
},
182+
173183
// ncHistogramComment (native classic histogram comment) helps attach
174184
// comments to the query and also keep multiline strings where applicable.
175185
ncHistogramComment(query, comment):: {

0 commit comments

Comments
 (0)