Files
grafana/public/app/features/transformers/utils.ts
Oscar Kilhed 40cdb30336 Transformations: Add support for dashboard variable in limit, sort by, filter by value, heatmap and histogram (#75372)
* variables for filterforvalue

* use datalinkinput for basic matcher

* fix user select issue

* heatmap transformation variable interpolation

* clean code

* interpolate sort by

* add options interpolation in histogram transformation

* interpolation for limit

* Add suggestions UI to Filter by data value Transformation

Co-authored-by: oscarkilhed <oscar.kilhed@grafana.com>

* add validation for number/variable fields

* Add variables to add field from calculation

* Add validator to limit transformation

* Refactor validator

* Refactor suggestionInput styles

* Add variable support in heatmap calculate options to be in sync with tranform

* Refactor SuggestionsInput

* Fix histogram, limit and filter by value matchers

* clean up weird state ref

* Only interpolate when the feature toggle is set

* Add feature toggle to ui

* Fix number of variable test

* Fix issue with characters typed after opening suggestions still remains after selecting a suggestion

* Clean up from review

* Add more tests for numberOrVariableValidator

---------

Co-authored-by: Victor Marin <victor.marin@grafana.com>
2023-10-04 17:28:46 +03:00

64 lines
1.7 KiB
TypeScript

import { useMemo } from 'react';
import { DataFrame, getFieldDisplayName, TransformerCategory } from '@grafana/data';
import { config } from '@grafana/runtime';
export function useAllFieldNamesFromDataFrames(input: DataFrame[]): string[] {
return useMemo(() => {
if (!Array.isArray(input)) {
return [];
}
return Object.keys(
input.reduce<Record<string, boolean>>((names, frame) => {
if (!frame || !Array.isArray(frame.fields)) {
return names;
}
return frame.fields.reduce((names, field) => {
const t = getFieldDisplayName(field, frame, input);
names[t] = true;
return names;
}, names);
}, {})
);
}, [input]);
}
export function getDistinctLabels(input: DataFrame[]): Set<string> {
const distinct = new Set<string>();
for (const frame of input) {
for (const field of frame.fields) {
if (field.labels) {
for (const k of Object.keys(field.labels)) {
distinct.add(k);
}
}
}
}
return distinct;
}
export const categoriesLabels: { [K in TransformerCategory]: string } = {
combine: 'Combine',
calculateNewFields: 'Calculate new fields',
createNewVisualization: 'Create new visualization',
filter: 'Filter',
performSpatialOperations: 'Perform spatial operations',
reformat: 'Reformat',
reorderAndRename: 'Reorder and rename',
};
export const numberOrVariableValidator = (value: string | number) => {
if (typeof value === 'number') {
return true;
}
if (!Number.isNaN(Number(value))) {
return true;
}
if (/^\$[A-Za-z0-9_]+$/.test(value) && config.featureToggles.transformationsVariableSupport) {
return true;
}
return false;
};