grafana/public/app/features/templating/dataMacros.ts
Torkel Ödegaard b7b608418d
Templating: Introduce macros to simplify and optimize some scopedVars (#65317)
* Templating: Introduce macros to simplify and optimize some scopedVars

* Fixing tests

* fix test

* minor fix

* refactoring so macros work with formatting

* remove breaking change and keep current inconsistency

* Rename valueIndex to rowIndex

* Minor fixes

* Added test dashboard

* Added tags to dashboard

* Update

* Added test to check it returns match

* Update

* Fixed dashboard

* fix
2023-03-28 19:22:34 +02:00

77 lines
1.9 KiB
TypeScript

import { DisplayProcessor, FieldType, formattedValueToString, getDisplayProcessor, ScopedVars } from '@grafana/data';
import { VariableCustomFormatterFn } from '@grafana/scenes';
import { formatVariableValue } from './formatVariableValue';
/**
* ${__value.raw/nummeric/text/time} macro
*/
export function valueMacro(
match: string,
fieldPath?: string,
scopedVars?: ScopedVars,
format?: string | VariableCustomFormatterFn
) {
const value = getValueForValueMacro(match, fieldPath, scopedVars);
return formatVariableValue(value, format);
}
function getValueForValueMacro(match: string, fieldPath?: string, scopedVars?: ScopedVars) {
const dataContext = scopedVars?.__dataContext;
if (!dataContext) {
return match;
}
const { frame, rowIndex, field, calculatedValue } = dataContext.value;
if (calculatedValue) {
switch (fieldPath) {
case 'numeric':
return calculatedValue.numeric.toString();
case 'raw':
return calculatedValue.numeric;
case 'time':
return '';
case 'text':
default:
return formattedValueToString(calculatedValue);
}
}
if (rowIndex === undefined) {
return match;
}
if (fieldPath === 'time') {
const timeField = frame.fields.find((f) => f.type === FieldType.time);
return timeField ? timeField.values.get(rowIndex) : undefined;
}
const value = field.values.get(rowIndex);
if (fieldPath === 'raw') {
return value;
}
const displayProcessor = field.display ?? getFallbackDisplayProcessor();
const result = displayProcessor(value);
switch (fieldPath) {
case 'numeric':
return result.numeric;
case 'text':
return result.text;
default:
return formattedValueToString(result);
}
}
let fallbackDisplayProcessor: DisplayProcessor | undefined;
function getFallbackDisplayProcessor() {
if (!fallbackDisplayProcessor) {
fallbackDisplayProcessor = getDisplayProcessor();
}
return fallbackDisplayProcessor;
}