From 075976bea6bc0ad7e1f9e7fb5f0a06ac2ebec044 Mon Sep 17 00:00:00 2001 From: Kyle Cunningham Date: Wed, 3 Apr 2024 14:44:52 -0500 Subject: [PATCH] Transformations: Add applicability function to group to nested table (#85501) * Add applicability function to group to nested table * codeincarnate/group-to-nested-cond/ run linter --------- Co-authored-by: jev forsberg --- .../transformations/transformers/groupBy.ts | 24 ++++++------------- .../transformers/groupToNestedTable.ts | 18 ++++++++++++-- .../src/transformations/transformers/utils.ts | 19 +++++++++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/packages/grafana-data/src/transformations/transformers/groupBy.ts b/packages/grafana-data/src/transformations/transformers/groupBy.ts index fd5e28aa7e6..a8faf733e44 100644 --- a/packages/grafana-data/src/transformations/transformers/groupBy.ts +++ b/packages/grafana-data/src/transformations/transformers/groupBy.ts @@ -7,6 +7,9 @@ import { DataTransformerInfo } from '../../types/transformations'; import { reduceField, ReducerID } from '../fieldReducer'; import { DataTransformerID } from './ids'; +import { findMaxFields } from './utils'; + +const MINIMUM_FIELDS_REQUIRED = 2; export enum GroupByOperationID { aggregate = 'aggregate', @@ -34,32 +37,19 @@ export const groupByTransformer: DataTransformerInfo fields: {}, }, isApplicable: (data: DataFrame[]) => { - let maxFields = 0; - // Group by needs at least two fields // a field to group on and a field to aggregate // We make sure that at least one frame has at // least two fields - for (const frame of data) { - if (frame.fields.length > maxFields) { - maxFields = frame.fields.length; - } - } + const maxFields = findMaxFields(data); - return maxFields >= 2 + return maxFields >= MINIMUM_FIELDS_REQUIRED ? TransformationApplicabilityLevels.Applicable : TransformationApplicabilityLevels.NotApplicable; }, isApplicableDescription: (data: DataFrame[]) => { - let maxFields = 0; - - for (const frame of data) { - if (frame.fields.length > maxFields) { - maxFields = frame.fields.length; - } - } - - return `The Group by transformation requires a series with at least two fields to work. The maximum number of fields found on a series is ${maxFields}`; + const maxFields = findMaxFields(data); + return `The Group by transformation requires a series with at least ${MINIMUM_FIELDS_REQUIRED} fields to work. The maximum number of fields found on a series is ${maxFields}`; }, /** * Return a modified copy of the series. If the transform is not or should not diff --git a/packages/grafana-data/src/transformations/transformers/groupToNestedTable.ts b/packages/grafana-data/src/transformations/transformers/groupToNestedTable.ts index aa2d1f9c883..3f6843afeb1 100644 --- a/packages/grafana-data/src/transformations/transformers/groupToNestedTable.ts +++ b/packages/grafana-data/src/transformations/transformers/groupToNestedTable.ts @@ -3,13 +3,15 @@ import { map } from 'rxjs/operators'; import { guessFieldTypeForField } from '../../dataframe/processDataFrame'; import { getFieldDisplayName } from '../../field/fieldState'; import { DataFrame, Field, FieldType } from '../../types/dataFrame'; -import { DataTransformerInfo } from '../../types/transformations'; +import { DataTransformerInfo, TransformationApplicabilityLevels } from '../../types/transformations'; import { ReducerID, reduceField } from '../fieldReducer'; import { GroupByFieldOptions, createGroupedFields, groupValuesByKey } from './groupBy'; import { DataTransformerID } from './ids'; +import { findMaxFields } from './utils'; export const SHOW_NESTED_HEADERS_DEFAULT = true; +const MINIMUM_FIELDS_REQUIRED = 2; enum GroupByOperationID { aggregate = 'aggregate', @@ -33,7 +35,19 @@ export const groupToNestedTable: DataTransformerInfo { + // Group to nested table needs at least two fields + // a field to group on and to show in the nested table + const maxFields = findMaxFields(data); + + return maxFields >= MINIMUM_FIELDS_REQUIRED + ? TransformationApplicabilityLevels.Applicable + : TransformationApplicabilityLevels.NotApplicable; + }, + isApplicableDescription: (data: DataFrame[]) => { + const maxFields = findMaxFields(data); + return `The Group to nested table transformation requires a series with at least ${MINIMUM_FIELDS_REQUIRED} fields to work. The maximum number of fields found on a series is ${maxFields}`; + }, /** * Return a modified copy of the series. If the transform is not or should not * be applied, just return the input series diff --git a/packages/grafana-data/src/transformations/transformers/utils.ts b/packages/grafana-data/src/transformations/transformers/utils.ts index 64f762200fb..eda97f0fef3 100644 --- a/packages/grafana-data/src/transformations/transformers/utils.ts +++ b/packages/grafana-data/src/transformations/transformers/utils.ts @@ -1,3 +1,22 @@ +import { DataFrame } from '../../types'; + export const transformationsVariableSupport = () => { return (window as any)?.grafanaBootData?.settings?.featureToggles?.transformationsVariableSupport; }; + +/** + * Retrieve the maximum number of fields in a series of a dataframe. + */ +export function findMaxFields(data: DataFrame[]) { + let maxFields = 0; + + // Group to nested table needs at least two fields + // a field to group on and to show in the nested table + for (const frame of data) { + if (frame.fields.length > maxFields) { + maxFields = frame.fields.length; + } + } + + return maxFields; +}