Commit 06b54d87 authored by Tristan Read's avatar Tristan Read Committed by Tim Zallmann

Fix Grafana chart label templates

parent 1f3f923a
/* eslint-disable import/prefer-default-export */
import _ from 'underscore';
/** /**
* @param {Array} queryResults - Array of Result objects * @param {Array} queryResults - Array of Result objects
* @param {Object} defaultConfig - Default chart config values (e.g. lineStyle, name) * @param {Object} defaultConfig - Default chart config values (e.g. lineStyle, name)
* @returns {Array} The formatted values * @returns {Array} The formatted values
*/ */
// eslint-disable-next-line import/prefer-default-export
export const makeDataSeries = (queryResults, defaultConfig) => export const makeDataSeries = (queryResults, defaultConfig) =>
queryResults queryResults
.map(result => { .map(result => {
...@@ -19,10 +17,13 @@ export const makeDataSeries = (queryResults, defaultConfig) => ...@@ -19,10 +17,13 @@ export const makeDataSeries = (queryResults, defaultConfig) =>
if (name) { if (name) {
series.name = `${defaultConfig.name}: ${name}`; series.name = `${defaultConfig.name}: ${name}`;
} else { } else {
const template = _.template(defaultConfig.name, { series.name = defaultConfig.name;
interpolate: /\{\{(.+?)\}\}/g, Object.keys(result.metric).forEach(templateVar => {
const value = result.metric[templateVar];
const regex = new RegExp(`{{\\s*${templateVar}\\s*}}`, 'g');
series.name = series.name.replace(regex, value);
}); });
series.name = template(result.metric);
} }
return { ...defaultConfig, ...series }; return { ...defaultConfig, ...series };
......
...@@ -81,6 +81,17 @@ describe('monitor helper', () => { ...@@ -81,6 +81,17 @@ describe('monitor helper', () => {
expect(result.name).toEqual('brpop, brpop'); expect(result.name).toEqual('brpop, brpop');
}); });
it('supports hyphenated template variables', () => {
const config = { ...defaultConfig, name: 'expired - {{ test-attribute }}' };
const [result] = monitorHelper.makeDataSeries(
[{ metric: { 'test-attribute': 'test-attribute-value' }, values: series }],
config,
);
expect(result.name).toEqual('expired - test-attribute-value');
});
it('updates multiple series names from templates', () => { it('updates multiple series names from templates', () => {
const config = { const config = {
...defaultConfig, ...defaultConfig,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment