Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
import logfmt from 'logfmt';
|
|
|
|
|
|
2023-04-18 07:17:30 -05:00
|
|
|
import { ScopedVars, DataLinkTransformationConfig, SupportedTransformationType } from '@grafana/data';
|
Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
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 } = {};
|
2023-04-18 07:17:30 -05:00
|
|
|
if (transformation.type === SupportedTransformationType.Regex && transformation.expression) {
|
Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
const regexp = new RegExp(transformation.expression, 'gi');
|
2023-05-16 07:47:15 -05:00
|
|
|
const stringFieldVal = typeof fieldValue === 'string' ? fieldValue : safeStringifyValue(fieldValue);
|
|
|
|
|
|
|
|
|
|
const matches = stringFieldVal.matchAll(regexp);
|
Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
for (const match of matches) {
|
|
|
|
|
if (match.groups) {
|
|
|
|
|
transformVal = match.groups;
|
|
|
|
|
} else {
|
|
|
|
|
transformVal[transformation.mapValue || fieldName] = match[1] || match[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-18 07:17:30 -05:00
|
|
|
} else if (transformation.type === SupportedTransformationType.Logfmt) {
|
Explore: Add transformations to correlation data links (#61799)
* bring in source from database
* bring in transformations from database
* add regex transformations to scopevar
* Consolidate types, add better example, cleanup
* Add var only if match
* Change ScopedVar to not require text, do not leak transformation-made variables between links
* Add mappings and start implementing logfmt
* Add mappings and start implementing logfmt
* Remove mappings, turn off global regex
* Add example yaml and omit transformations if empty
* Fix the yaml
* Add logfmt transformation
* Cleanup transformations and yaml
* add transformation field to FE types and use it, safeStringify logfmt values
* Add tests, only safe stringify if non-string, fix bug with safe stringify where it would return empty string with false value
* Add test for transformation field
* Do not add null transformations object
* Break out transformation logic, add tests to backend code
* Fix lint errors I understand 😅
* Fix the backend lint error
* Remove unnecessary code and mark new Transformations object as internal
* Add support for named capture groups
* Remove type assertion
* Remove variable name from transformation
* Add test for overriding regexes
* Add back variable name field, but change to mapValue
* fix go api test
* Change transformation types to enum, add better provisioning checks for bad type name and format
* Check for expression with regex transformations
2023-02-22 06:53:03 -06:00
|
|
|
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;
|
|
|
|
|
};
|