diff --git a/docs/sources/panels-visualizations/query-transform-data/transform-data/index.md b/docs/sources/panels-visualizations/query-transform-data/transform-data/index.md index 529c517f2f9..7782acad459 100644 --- a/docs/sources/panels-visualizations/query-transform-data/transform-data/index.md +++ b/docs/sources/panels-visualizations/query-transform-data/transform-data/index.md @@ -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 diff --git a/packages/grafana-data/src/transformations/transformers/groupBy.ts b/packages/grafana-data/src/transformations/transformers/groupBy.ts index f00219a5657..f6eb212a138 100644 --- a/packages/grafana-data/src/transformations/transformers/groupBy.ts +++ b/packages/grafana-data/src/transformations/transformers/groupBy.ts @@ -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 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; }; /** diff --git a/public/app/features/transformers/docs/content.ts b/public/app/features/transformers/docs/content.ts index f9dd0074977..a441149469d 100644 --- a/public/app/features/transformers/docs/content.ts +++ b/public/app/features/transformers/docs/content.ts @@ -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 diff --git a/public/app/features/transformers/editors/GroupByTransformerEditor.tsx b/public/app/features/transformers/editors/GroupByTransformerEditor.tsx index e26aced6fcd..ebd2a55661d 100644 --- a/public/app/features/transformers/editors/GroupByTransformerEditor.tsx +++ b/public/app/features/transformers/editors/GroupByTransformerEditor.tsx @@ -26,11 +26,7 @@ interface FieldProps { onConfigChange: (config: GroupByFieldOptions) => void; } -export const GroupByTransformerEditor = ({ - input, - options, - onChange, -}: TransformerUIProps) => { +const GroupByTransformerEditor = ({ input, options, onChange }: TransformerUIProps) => { 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 }: /> - {config?.operation === GroupByOperationID.aggregate && ( + {config?.operation && ( { onConfigChange({ ...config, aggregations: stats as ReducerID[] }); }} + filterOptions={(option) => + config?.operation === GroupByOperationID.groupBy ? option.id === ReducerID.count : true + } /> )}