Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformations: Add an All Unique Values Reducer #48653

Merged
merged 7 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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