Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

valueMappings variable subst #62

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ The yaml drive is identical in nature to the fillColor drive.
New config terms:
- cells.cell-name.strokeColor

Grafana Variable Substitution in ValueMappings
----------------------------------------------
Adds support for passing valueMappings text through grafana variable substitution. i.e. to substitute
all values of a timeseries with a variable value:
cell abc:
valueMappings:
- {text = "${myVariableName}"}

## 1.12.0
URL Grafana Variables
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ anchorLinks:
- valueMappings: &valuemap1
- {value: null, text: "panel null"}
- {value: 0, text: "panel off"}
- {valueMax: 300, text: "panel low"}
- {valueMax: 300, text: "panel ${exampleVar}"}
- {valueMin: 300, valueMax: 600, text: "panel medium"}
- {valueMin: 600, text: "panel high"}
- thresholds: &thresholds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ valueMappings:
dbTrans:
- {value: null, text: "site null"}
- {value: 0, text: "site off"}
- {valueMax: 300, text: "site low"}
- {valueMax: 300, text: "site ${exampleVar}"}
- {valueMin: 300, valueMax: 600, text: "site medium"}
- {valueMin: 600, text: "site high"}
262 changes: 145 additions & 117 deletions provisioning/dashboards/valueMappings.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/components/Config.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VariableInterpolation, getTemplateSrv } from "@grafana/runtime";
import { createUrl } from "./Utils";

export type DatapointMode = 'last' | 'lastNotNull';
Expand Down Expand Up @@ -39,6 +40,7 @@ export type FlowValueMapping = {
valueMin: number | undefined;
valueMax: number | undefined;
text: string;
variableSubst: boolean;
valid: boolean;
};

Expand Down Expand Up @@ -238,6 +240,12 @@ function panelConfigDereference(siteConfig: SiteConfig, panelConfig: PanelConfig
(typeof mapping.text === 'string') &&
((typeof mapping.valueMin === 'number') || (typeof mapping.valueMin === 'undefined')) &&
((typeof mapping.valueMax === 'number') || (typeof mapping.valueMax === 'undefined')));

if (mapping.valid && (typeof mapping.variableSubst === 'undefined')) {
let interpolations: VariableInterpolation[] = [];
getTemplateSrv().replace(mapping.text, undefined, undefined, interpolations);
mapping.variableSubst = interpolations.length > 0;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/SvgUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { HighlightState } from './Highlighter';
import {
CellFillLevelDriver, getClipper, isShapeElement } from 'components/FillLevel';
import { getTemplateSrv } from '@grafana/runtime';
// Defines the metadata stored against each drivable svg cell
export type SvgCell = {
cellId: string;
Expand Down Expand Up @@ -261,7 +262,7 @@ export function valueMapping(valueMappings: FlowValueMapping[], value: number |
((typeof mapping.valueMax === 'undefined') || (value <= mapping.valueMax));
}
if (match) {
return mapping.text;
return mapping.variableSubst ? getTemplateSrv().replace(mapping.text) : mapping.text;
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/test/SvgUpdater.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ test('zero', () => {
});

test('valuemapping', () => {
// Note getTemplateSrv is not available in the test framework so variableSubst is always false
let fvm: FlowValueMapping[] = [
{valid: true, value: -3, valueMin: undefined, valueMax: undefined, text: 'val_-3'},
{valid: true, value: 0, valueMin: undefined, valueMax: undefined, text: 'val_0'},
{valid: true, value: 3, valueMin: undefined, valueMax: undefined, text: 'val_3'},
{valid: true, value: undefined, valueMin: 10, valueMax: 20, text: 'hello1'},
{valid: true, value: undefined, valueMin: 21, valueMax: 30, text: 'hello2'},
{valid: true, value: undefined, valueMin: 1000, valueMax: undefined, text: 'high'},
{valid: true, value: undefined, valueMin: undefined, valueMax: -1000, text: 'low'},
{valid: true, value: -3, valueMin: undefined, valueMax: undefined, text: 'val_-3', variableSubst: false},
{valid: true, value: 0, valueMin: undefined, valueMax: undefined, text: 'val_0', variableSubst: false},
{valid: true, value: 3, valueMin: undefined, valueMax: undefined, text: 'val_3', variableSubst: false},
{valid: true, value: undefined, valueMin: 10, valueMax: 20, text: 'hello1', variableSubst: false},
{valid: true, value: undefined, valueMin: 21, valueMax: 30, text: 'hello2', variableSubst: false},
{valid: true, value: undefined, valueMin: 1000, valueMax: undefined, text: 'high', variableSubst: false},
{valid: true, value: undefined, valueMin: undefined, valueMax: -1000, text: 'low', variableSubst: false},
];
expect(valueMapping(fvm, -4)).toEqual(null);
expect(valueMapping(fvm, -3)).toEqual('val_-3');
Expand All @@ -163,7 +164,7 @@ test('valuemapping', () => {

test('valuemappingnocriteria', () => {
let fvm: FlowValueMapping[] = [
{valid: true, value: undefined, valueMin: undefined, valueMax: undefined, text: 'custom'},
{valid: true, value: undefined, valueMin: undefined, valueMax: undefined, text: 'custom', variableSubst: false},
];
expect(valueMapping(fvm, -4)).toEqual('custom');
expect(valueMapping(fvm, 0)).toEqual('custom');
Expand Down
Loading