grafana/public/app/features/correlations/transformations.ts
Kristina 9d69d3173f
Correlations: Add transformation editor (#66217)
* There was an attempt

* Change disabled state based on transformation type

* Add validation to transformation type

* Revert "Add validation to transformation type"

This reverts commit 2188a3d9a93aec5eeafcdd40510391ba1a53671a.

* Add validation to transformation type

* Move transformations editor to a separate file

* Make name more descriptive

* Ensure type dropdown has always the same width

* Add tooltips around transformation options

* Slight style changes

* Remove autofocus on append, integrate read only to transformationeditor, save values that disappear so they come back

* Remove yaml changes

* Have variable background color work with alternating colors on different themes

* Make expression required for regular expressions

* Remove unused empty form object

* Fix bug about transformation’s values saved in memory

* Better validation formatting for expression

* Add labels and (for now) non working test, attempt to fix saved transformation delete/add bug

* Fix datalink comment

* Remove fancy CSS due to background change

* Fix deleting saved transformation bug, finish tests

* Consolidate transformation types

* Double check aria labels

* Change aria labels, fix tests

* Add a transformation with the create correlation test

---------

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
2023-04-18 07:17:30 -05:00

35 lines
1.3 KiB
TypeScript

import logfmt from 'logfmt';
import { ScopedVars, DataLinkTransformationConfig, SupportedTransformationType } from '@grafana/data';
import { safeStringifyValue } from 'app/core/utils/explore';
export const getTransformationVars = (
transformation: DataLinkTransformationConfig,
fieldValue: string,
fieldName: string
): ScopedVars => {
let transformationScopedVars: ScopedVars = {};
let transformVal: { [key: string]: string | boolean | null | undefined } = {};
if (transformation.type === SupportedTransformationType.Regex && transformation.expression) {
const regexp = new RegExp(transformation.expression, 'gi');
const matches = fieldValue.matchAll(regexp);
for (const match of matches) {
if (match.groups) {
transformVal = match.groups;
} else {
transformVal[transformation.mapValue || fieldName] = match[1] || match[0];
}
}
} else if (transformation.type === SupportedTransformationType.Logfmt) {
transformVal = logfmt.parse(fieldValue);
}
Object.keys(transformVal).forEach((key) => {
const transformValString =
typeof transformVal[key] === 'string' ? transformVal[key] : safeStringifyValue(transformVal[key]);
transformationScopedVars[key] = { value: transformValString };
});
return transformationScopedVars;
};