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 <jev.forsberg@grafana.com>
testinfra-symlink-public
Kyle Cunningham 1 year ago committed by GitHub
parent ddf5fbe591
commit 075976bea6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      packages/grafana-data/src/transformations/transformers/groupBy.ts
  2. 18
      packages/grafana-data/src/transformations/transformers/groupToNestedTable.ts
  3. 19
      packages/grafana-data/src/transformations/transformers/utils.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<GroupByTransformerOptions>
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

@ -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<GroupToNestedTableTransform
showSubframeHeaders: SHOW_NESTED_HEADERS_DEFAULT,
fields: {},
},
isApplicable: (data) => {
// 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

@ -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;
}

Loading…
Cancel
Save