Skip to content

Commit

Permalink
Transformations: Add an All Unique Values Reducer (#48653) (#48733)
Browse files Browse the repository at this point in the history
(cherry picked from commit 570ff07)

Co-authored-by: Josiah (Jay) Goodson <josiah.goodson@gmail.com>
  • Loading branch information
grafanabot and josiahg committed May 25, 2022
1 parent c505337 commit 4487842
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Expand Up @@ -97,6 +97,15 @@ describe('Stats Calculators', () => {
expect(stats.delta).toEqual(300);
});

it('should calculate unique values', () => {
const stats = reduceField({
field: createField('x', [1, 2, 2, 3, 1]),
reducers: [ReducerID.uniqueValues],
});

expect(stats.uniqueValues).toEqual([1, 2, 3]);
});

it('consistently check allIsNull/allIsZero', () => {
const empty = createField('x');
const allNull = createField('x', [null, null, null, null]);
Expand Down
10 changes: 10 additions & 0 deletions packages/grafana-data/src/transformations/fieldReducer.ts
Expand Up @@ -25,6 +25,7 @@ export enum ReducerID {
allIsZero = 'allIsZero',
allIsNull = 'allIsNull',
allValues = 'allValues',
uniqueValues = 'uniqueValues',
}

// Internal function
Expand Down Expand Up @@ -237,6 +238,15 @@ export const fieldReducers = new Registry<FieldReducerInfo>(() => [
standard: false,
reduce: (field: Field) => ({ allValues: field.values.toArray() }),
},
{
id: ReducerID.uniqueValues,
name: 'All unique values',
description: 'Returns an array with all unique values',
standard: false,
reduce: (field: Field) => ({
uniqueValues: [...new Set(field.values.toArray())],
}),
},
]);

export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs {
Expand Down

0 comments on commit 4487842

Please sign in to comment.