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

[Security solution] Grouping count bug #156206

Merged
merged 6 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const mockGroupingProps = {
activePage: 0,
data: {
groupsCount: {
value: 2,
value: 3,
},
groupByFields: {
doc_count_error_upper_bound: 0,
Expand Down Expand Up @@ -121,7 +121,10 @@ export const mockGroupingProps = {
],
},
unitsCount: {
value: 3,
value: 14,
},
unitsCountWithoutNull: {
value: 14,
},
},
groupingId: 'test-grouping-id',
Expand Down
14 changes: 2 additions & 12 deletions packages/kbn-securitysolution-grouping/src/components/grouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,13 @@ const GroupingComponent = <T,>({
const [trigger, setTrigger] = useState<Record<string, { state: 'open' | 'closed' | undefined }>>(
{}
);
const [nullCount, setNullCount] = useState({ unit: 0, group: 0 });

const unitCount = useMemo(
() => (data?.unitsCount?.value ?? 0) + nullCount.unit,
[data?.unitsCount?.value, nullCount.unit]
);
const unitCount = useMemo(() => data?.unitsCount?.value ?? 0, [data?.unitsCount?.value]);
const unitCountText = useMemo(() => {
return `${unitCount.toLocaleString()} ${unit && unit(unitCount)}`;
}, [unitCount, unit]);

const groupCount = useMemo(
() => (data?.groupsCount?.value ?? 0) + nullCount.group,
[data?.groupsCount?.value, nullCount.group]
);
const groupCount = useMemo(() => data?.groupsCount?.value ?? 0, [data?.groupsCount?.value]);
const groupCountText = useMemo(
() => `${groupCount.toLocaleString()} ${GROUPS_UNIT(groupCount)}`,
[groupCount]
Expand All @@ -106,9 +99,6 @@ const GroupingComponent = <T,>({
const nullGroupMessage = isNullGroup
? NULL_GROUP(selectedGroup, unit(groupBucket.doc_count))
: undefined;
if (isNullGroup) {
setNullCount({ unit: groupBucket.doc_count, group: 1 });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the dumb thing 🙄

}

return (
<span key={groupKey}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export interface RootAggregation<T> {
unitsCount?: {
value?: number | null;
};
unitsCountWithoutNull?: {
value?: number | null;
};
}

export type GroupingFieldTotalAggregation<T> = Record<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import type { GroupingQueryArgs } from './types';
import { getGroupingQuery, parseGroupingQuery } from '.';
import { getEmptyValue } from './helpers';
import { GroupingAggregation } from '../../..';

const testProps: GroupingQueryArgs = {
additionalFilters: [],
Expand Down Expand Up @@ -154,43 +155,76 @@ describe('group selector', () => {
});
});
});

const groupingAggs = {
groupByFields: {
buckets: [
{
key: ['20.80.64.28', '20.80.64.28'],
key_as_string: '20.80.64.28|20.80.64.28',
doc_count: 75,
},
{
key: ['0.0.0.0', '0.0.0.0'],
key_as_string: '0.0.0.0|0.0.0.0',
doc_count: 75,
},
{
key: ['0.0.0.0', '::'],
key_as_string: '0.0.0.0|::',
doc_count: 75,
},
],
},
unitsCount: {
value: 100,
},
unitsCountWithoutNull: {
value: 100,
},
groupsCount: {
value: 20,
},
};
it('parseGroupingQuery finds and flags the null group', () => {
const data = [
{
key: ['20.80.64.28', '20.80.64.28'],
key_as_string: '20.80.64.28|20.80.64.28',
doc_count: 75,
},
{
key: ['0.0.0.0', '0.0.0.0'],
key_as_string: '0.0.0.0|0.0.0.0',
doc_count: 75,
},
{
key: ['0.0.0.0', '::'],
key_as_string: '0.0.0.0|::',
doc_count: 75,
const result = parseGroupingQuery(groupingAggs);
expect(result).toEqual({
groupByFields: {
buckets: [
{
key: ['20.80.64.28'],
key_as_string: '20.80.64.28',
doc_count: 75,
},
{
key: ['0.0.0.0'],
key_as_string: '0.0.0.0',
doc_count: 75,
},
{
key: [getEmptyValue()],
key_as_string: getEmptyValue(),
isNullGroup: true,
doc_count: 75,
},
],
},
];
const result = parseGroupingQuery(data);
expect(result).toEqual([
{
key: ['20.80.64.28'],
key_as_string: '20.80.64.28',
doc_count: 75,
unitsCount: {
value: 100,
},
{
key: ['0.0.0.0'],
key_as_string: '0.0.0.0',
doc_count: 75,
unitsCountWithoutNull: {
value: 100,
},
{
key: [getEmptyValue()],
key_as_string: getEmptyValue(),
isNullGroup: true,
doc_count: 75,
groupsCount: {
value: 20,
},
]);
});
});
it('parseGroupingQuery adjust group count when null field group is present', () => {
const result: GroupingAggregation<{}> = parseGroupingQuery({
...groupingAggs,
unitsCountWithoutNull: { value: 99 },
});

expect(result.groupsCount?.value).toEqual(21);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/

import { getEmptyValue, getFieldTypeMissingValues } from './helpers';
import { GroupingBucket } from '../..';
import { RawBucket } from '../../..';
import { GroupingAggregation } from '../..';
import type { GroupingQueryArgs, GroupingQuery } from './types';
/** The maximum number of groups to render */
export const DEFAULT_GROUP_BY_FIELD_SIZE = 10;
Expand Down Expand Up @@ -84,6 +83,16 @@ export const getGroupingQuery = ({
: {}),
},
},

unitsCountWithoutNull: { value_count: { field: groupByField } },
unitsCount: {
value_count: {
field: groupByField,
missing: getFieldTypeMissingValues(selectedGroupEsTypes)[0],
},
},
groupsCount: { cardinality: { field: groupByField } },

...(rootAggregations
? rootAggregations.reduce((aggObj, subAgg) => Object.assign(aggObj, subAgg), {})
: {}),
Expand Down Expand Up @@ -113,9 +122,12 @@ export const getGroupingQuery = ({
* @param buckets buckets returned from the grouping query
*/
export const parseGroupingQuery = <T>(
buckets: Array<RawBucket<T>>
): Array<RawBucket<T> & GroupingBucket> =>
buckets.map((group) => {
aggs?: GroupingAggregation<T>
): GroupingAggregation<T> | {} => {
if (!aggs) {
return {};
}
const groupByFields = aggs?.groupByFields?.buckets?.map((group) => {
if (!Array.isArray(group.key)) {
return group;
}
Expand All @@ -135,3 +147,15 @@ export const parseGroupingQuery = <T>(
isNullGroup: true,
};
});

return {
...aggs,
groupByFields: { buckets: groupByFields },
groupsCount: {
value:
(aggs.unitsCount?.value !== aggs.unitsCountWithoutNull?.value
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if these are not equal, there is one extra group to add to the count

? (aggs.groupsCount?.value ?? 0) + 1
: aggs.groupsCount?.value) ?? 0,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,17 @@ describe('GroupedAlertsTable', () => {
},
},
groupsCount: { cardinality: { field: 'kibana.alert.rule.name' } },
unitsCount: { value_count: { field: 'kibana.alert.rule.name' } },
unitsCount: {
value_count: {
field: 'kibana.alert.rule.name',
missing: '-',
},
},
unitsCountWithoutNull: {
value_count: {
field: 'kibana.alert.rule.name',
},
},
},
query: {
bool: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ export const GroupedSubLevelComponent: React.FC<AlertsTableComponentProps> = ({
skip: isNoneGroup([selectedGroup]),
});

const buckets = useMemo(
() => parseGroupingQuery(alertsGroupsData?.aggregations?.groupByFields?.buckets ?? []),
const aggs = useMemo(
() => parseGroupingQuery(alertsGroupsData?.aggregations),
[alertsGroupsData]
);

Expand Down Expand Up @@ -248,10 +248,7 @@ export const GroupedSubLevelComponent: React.FC<AlertsTableComponentProps> = ({
activePage: pageIndex,
data: {
...alertsGroupsData?.aggregations,
groupByFields: {
...alertsGroupsData?.aggregations?.groupByFields,
buckets,
},
...aggs,
},
groupingLevel,
inspectButton: inspect,
Expand All @@ -268,7 +265,7 @@ export const GroupedSubLevelComponent: React.FC<AlertsTableComponentProps> = ({
getGrouping,
pageIndex,
alertsGroupsData,
buckets,
aggs,
groupingLevel,
inspect,
loading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ RuleNameGroupContent.displayName = 'RuleNameGroup';

const HostNameGroupContent = React.memo<{ hostName: string | string[]; nullGroupMessage?: string }>(
({ hostName, nullGroupMessage }) => (
<EuiFlexGroup
data-test-subj="host-name-group-renderer"
gutterSize="s"
alignItems="center"
justifyContent="center"
>
<EuiFlexGroup data-test-subj="host-name-group-renderer" gutterSize="s" alignItems="center">
<EuiFlexItem
grow={false}
style={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ describe('getAlertsGroupingQuery', () => {
_source: false,
aggs: {
unitsCount: {
value_count: {
field: 'kibana.alert.rule.name',
missing: '-',
},
},
unitsCountWithoutNull: {
value_count: {
field: 'kibana.alert.rule.name',
},
Expand Down Expand Up @@ -197,6 +203,12 @@ describe('getAlertsGroupingQuery', () => {
_source: false,
aggs: {
unitsCount: {
value_count: {
field: 'process.name',
missing: '-',
},
},
unitsCountWithoutNull: {
value_count: {
field: 'process.name',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ export const getAlertsGroupingQuery = ({
? getAggregationsByGroupField(selectedGroup)
: [],
pageNumber: pageIndex * pageSize,
rootAggregations: [
{
unitsCount: { value_count: { field: selectedGroup } },
},
...(!isNoneGroup([selectedGroup])
? [{ groupsCount: { cardinality: { field: selectedGroup } } }]
: []),
],
runtimeMappings,
selectedGroupEsTypes,
size: pageSize,
Expand Down