Transformations: Allow count aggregation for groupBy field (#108079)

* feat(transformations): allow groupBy aggregation

* add some details to the documentation

* Update public/app/features/transformers/docs/content.ts

Co-authored-by: Isabel Matwawana <76437239+imatwawana@users.noreply.github.com>

---------

Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
Co-authored-by: Isabel Matwawana <76437239+imatwawana@users.noreply.github.com>
This commit is contained in:
Ihor Yeromin 2025-07-16 12:39:19 +02:00 committed by GitHub
parent 5b0ebb1693
commit f657fc0236
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 15 deletions

View File

@ -635,7 +635,7 @@ This transformation goes in two steps. First you specify one or multiple fields
| 2020-07-07 10:31:22 | **_server 3_** | 55 | OK |
| 2020-07-07 09:30:57 | **_server 3_** | 62 | Rebooting |
All rows with the same value of Server ID are grouped together.
All rows with the same value of Server ID are grouped together. Optionally, you can add a count of how may values fall in the selected group.
After choosing which field you want to group your data by, you can add various calculations on the other fields, and apply the calculation to each group of rows. For instance, we could want to calculate the average CPU temperature for each of those servers. So we can add the _mean_ calculation applied on the CPU Temperature field to get the following:
@ -645,6 +645,14 @@ After choosing which field you want to group your data by, you can add various c
| server 2 | 88.6 |
| server 3 | 59.6 |
If you had added the count stat to the group by, there would be an extra column showing that the count of each server from above was 3.
| Server ID | CPU Temperature (mean) | Server ID (count) |
| --------- | ---------------------- | ----------------- |
| server 1 | 82 | 3 |
| server 2 | 88.6 | 3 |
| server 3 | 59.6 | 3 |
And we can add more than one calculation. For instance:
- For field Time, we can calculate the _Last_ value, to know when the last data point was received for each server

View File

@ -10,7 +10,7 @@ import { FieldMatcherID } from '../matchers/ids';
import { DataTransformerID } from './ids';
import { findMaxFields } from './utils';
const MINIMUM_FIELDS_REQUIRED = 2;
const MINIMUM_FIELDS_REQUIRED = 1;
export enum GroupByOperationID {
aggregate = 'aggregate',
@ -146,11 +146,7 @@ export const groupByTransformer: DataTransformerInfo<GroupByTransformerOptions>
const shouldCalculateField = (field: Field, options: GroupByTransformerOptions): boolean => {
const fieldName = getFieldDisplayName(field);
return (
options?.fields[fieldName]?.operation === GroupByOperationID.aggregate &&
Array.isArray(options?.fields[fieldName].aggregations) &&
options?.fields[fieldName].aggregations.length > 0
);
return Array.isArray(options?.fields[fieldName]?.aggregations) && options?.fields[fieldName].aggregations.length > 0;
};
/**

View File

@ -581,7 +581,7 @@ This transformation goes in two steps. First you specify one or multiple fields
| 2020-07-07 10:31:22 | **_server 3_** | 55 | OK |
| 2020-07-07 09:30:57 | **_server 3_** | 62 | Rebooting |
All rows with the same value of Server ID are grouped together.
All rows with the same value of Server ID are grouped together. Optionally, you can add a count of how may values fall in the selected group.
After choosing which field you want to group your data by, you can add various calculations on the other fields, and apply the calculation to each group of rows. For instance, we could want to calculate the average CPU temperature for each of those servers. So we can add the _mean_ calculation applied on the CPU Temperature field to get the following:
@ -591,6 +591,14 @@ After choosing which field you want to group your data by, you can add various c
| server 2 | 88.6 |
| server 3 | 59.6 |
If you had added the count stat to the group by transformation, there would be an extra column showing that the count of each server from above was 3.
| Server ID | CPU Temperature (mean) | Server ID (count) |
| --------- | ---------------------- | ----------------- |
| server 1 | 82 | 3 |
| server 2 | 88.6 | 3 |
| server 3 | 59.6 | 3 |
And we can add more than one calculation. For instance:
- For field Time, we can calculate the _Last_ value, to know when the last data point was received for each server

View File

@ -26,11 +26,7 @@ interface FieldProps {
onConfigChange: (config: GroupByFieldOptions) => void;
}
export const GroupByTransformerEditor = ({
input,
options,
onChange,
}: TransformerUIProps<GroupByTransformerOptions>) => {
const GroupByTransformerEditor = ({ input, options, onChange }: TransformerUIProps<GroupByTransformerOptions>) => {
const fieldNames = useAllFieldNamesFromDataFrames(input, true);
const onConfigChange = useCallback(
@ -88,7 +84,7 @@ export const GroupByTransformerEditor = ({
);
};
export const GroupByFieldConfiguration = ({ fieldName, config, onConfigChange }: FieldProps) => {
const GroupByFieldConfiguration = ({ fieldName, config, onConfigChange }: FieldProps) => {
const theme = useTheme2();
const styles = getStyles(theme);
@ -127,7 +123,7 @@ export const GroupByFieldConfiguration = ({ fieldName, config, onConfigChange }:
/>
</div>
{config?.operation === GroupByOperationID.aggregate && (
{config?.operation && (
<StatsPicker
className={styles.aggregations}
placeholder={t('transformers.group-by-field-configuration.placeholder-select-stats', 'Select stats')}
@ -136,6 +132,9 @@ export const GroupByFieldConfiguration = ({ fieldName, config, onConfigChange }:
onChange={(stats) => {
onConfigChange({ ...config, aggregations: stats as ReducerID[] });
}}
filterOptions={(option) =>
config?.operation === GroupByOperationID.groupBy ? option.id === ReducerID.count : true
}
/>
)}
</Stack>