Skip to content

Commit

Permalink
[9.0] Set from & to as optional args in @kbn/grouping (#213212) (#213265
Browse files Browse the repository at this point in the history
)

# Backport

This will backport the following commits from `main` to `9.0`:
- [Set from & to as optional args in @kbn/grouping
(#213212)](#213212)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Alberto
Blázquez","email":"albertoblaz@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-03-05T16:21:54Z","message":"Set
from & to as optional args in @kbn/grouping (#213212)\n\n##
Summary\n\nSet `from` and `to` as optional args in `getGroupingQuery`, a
function\nexposed by `@kbn/grouping`.\n\nIt will unblock this PR:\n-
https://github.com/elastic/kibana/pull/212955\n\n### Motivation
\n\n`getGroupingQuery` returns an ES aggregation for grouping
documents.\nThis function assumes data will be queried in a certain
interval of\ntime. However, Asset Inventory needs to query data from the
beginning of\ntime because the UI will not provide any time-range
filter. So in order\nto reuse this logic, we need to set both args as
optional.\n\nReason for wrapping both fields in an optional `timeRange`
record is to\nhave either both present or both undefined, not only one
of them\npresent.\n\n### Checklist\n\n- [ ] Any text added follows
[EUI's
writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\nsentence case text and includes
[i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n-
[
]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas
added for features that require explanation or tutorials\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[x] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [x] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n###
Risks\n\nNo risk at all. It would be a breaking change otherwise, if we
had to\nrequire args that were optional
before.","sha":"af147b5cc6612bc9e5632c2004f868f8c2b2a7f9","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Cloud
Security","backport:prev-minor","v9.1.0"],"title":"Set from & to as
optional args in
@kbn/grouping","number":213212,"url":"https://github.com/elastic/kibana/pull/213212","mergeCommit":{"message":"Set
from & to as optional args in @kbn/grouping (#213212)\n\n##
Summary\n\nSet `from` and `to` as optional args in `getGroupingQuery`, a
function\nexposed by `@kbn/grouping`.\n\nIt will unblock this PR:\n-
https://github.com/elastic/kibana/pull/212955\n\n### Motivation
\n\n`getGroupingQuery` returns an ES aggregation for grouping
documents.\nThis function assumes data will be queried in a certain
interval of\ntime. However, Asset Inventory needs to query data from the
beginning of\ntime because the UI will not provide any time-range
filter. So in order\nto reuse this logic, we need to set both args as
optional.\n\nReason for wrapping both fields in an optional `timeRange`
record is to\nhave either both present or both undefined, not only one
of them\npresent.\n\n### Checklist\n\n- [ ] Any text added follows
[EUI's
writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\nsentence case text and includes
[i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n-
[
]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas
added for features that require explanation or tutorials\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[x] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [x] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n###
Risks\n\nNo risk at all. It would be a breaking change otherwise, if we
had to\nrequire args that were optional
before.","sha":"af147b5cc6612bc9e5632c2004f868f8c2b2a7f9"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/213212","number":213212,"mergeCommit":{"message":"Set
from & to as optional args in @kbn/grouping (#213212)\n\n##
Summary\n\nSet `from` and `to` as optional args in `getGroupingQuery`, a
function\nexposed by `@kbn/grouping`.\n\nIt will unblock this PR:\n-
https://github.com/elastic/kibana/pull/212955\n\n### Motivation
\n\n`getGroupingQuery` returns an ES aggregation for grouping
documents.\nThis function assumes data will be queried in a certain
interval of\ntime. However, Asset Inventory needs to query data from the
beginning of\ntime because the UI will not provide any time-range
filter. So in order\nto reuse this logic, we need to set both args as
optional.\n\nReason for wrapping both fields in an optional `timeRange`
record is to\nhave either both present or both undefined, not only one
of them\npresent.\n\n### Checklist\n\n- [ ] Any text added follows
[EUI's
writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\nsentence case text and includes
[i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n-
[
]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas
added for features that require explanation or tutorials\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n- [ ] If a plugin
configuration key changed, check if it needs to be\nallowlisted in the
cloud and added to the
[docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n-
[x] This was checked for breaking HTTP API changes, and any
breaking\nchanges have been approved by the breaking-change committee.
The\n`release_note:breaking` label should be applied in these
situations.\n- [ ] [Flaky
Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\nused on any tests changed\n- [x] The PR description includes the
appropriate Release Notes section,\nand the correct `release_note:*`
label is applied per
the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n###
Risks\n\nNo risk at all. It would be a breaking change otherwise, if we
had to\nrequire args that were optional
before.","sha":"af147b5cc6612bc9e5632c2004f868f8c2b2a7f9"}}]}]
BACKPORT-->

Co-authored-by: Alberto Blázquez <albertoblaz@users.noreply.github.com>
  • Loading branch information
kibanamachine and albertoblaz authored Mar 5, 2025
1 parent 9bb526a commit ab49592
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { groupingBucket } from '../../mocks';

const testProps: GroupingQueryArgs = {
additionalFilters: [],
from: '2022-12-28T15:35:32.871Z',
timeRange: {
from: '2022-12-28T15:35:32.871Z',
to: '2023-02-23T06:59:59.999Z',
},
groupByField: 'host.name',
statsAggregations: [
{
Expand Down Expand Up @@ -59,7 +62,6 @@ const testProps: GroupingQueryArgs = {
runtimeMappings: {},
uniqueValue: 'whatAGreatAndUniqueValue',
size: 25,
to: '2023-02-23T06:59:59.999Z',
};
describe('group selector', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ export const MAX_QUERY_SIZE = 10000;

export const getGroupingQuery = ({
additionalFilters = [],
from,
groupByField,
pageNumber,
rootAggregations,
runtimeMappings,
size = DEFAULT_GROUP_BY_FIELD_SIZE,
sort,
statsAggregations,
to,
uniqueValue,
timeRange,
}: GroupingQueryArgs): GroupingQuery => ({
size: 0,
runtime_mappings: {
Expand Down Expand Up @@ -104,14 +103,18 @@ export const getGroupingQuery = ({
bool: {
filter: [
...additionalFilters,
{
range: {
'@timestamp': {
gte: from,
lte: to,
},
},
},
...(timeRange
? [
{
range: {
'@timestamp': {
gte: timeRange.from,
lte: timeRange.to,
},
},
},
]
: []),
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export type NamedAggregation = Record<string, estypes.AggregationsAggregationCon

export interface GroupingQueryArgs {
additionalFilters: BoolAgg[];
from: string;
groupByField: string;
rootAggregations?: NamedAggregation[];
runtimeMappings?: RunTimeMappings;
Expand All @@ -42,7 +41,10 @@ export interface GroupingQueryArgs {
size?: number;
sort?: Array<{ [category: string]: { order: 'asc' | 'desc' } }>;
statsAggregations?: NamedAggregation[];
to: string;
timeRange?: {
from: string;
to: string;
};
}

export interface MainAggregation extends NamedAggregation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ export const useLatestFindingsGrouping = ({
additionalFilters: query ? [query, additionalFilters] : [additionalFilters],
groupByField: currentSelectedGroup,
uniqueValue,
from: `now-${CDR_3RD_PARTY_RETENTION_POLICY}`,
to: 'now',
timeRange: {
from: `now-${CDR_3RD_PARTY_RETENTION_POLICY}`,
to: 'now',
},
pageNumber: activePageIndex * pageSize,
size: pageSize,
sort: [{ groupByField: { order: 'desc' } }, { complianceScore: { order: 'asc' } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ export const useLatestVulnerabilitiesGrouping = ({
additionalFilters: query ? [query, additionalFilters] : [additionalFilters],
groupByField: currentSelectedGroup,
uniqueValue,
from: `now-${CDR_3RD_PARTY_RETENTION_POLICY}`,
to: 'now',
timeRange: {
from: `now-${CDR_3RD_PARTY_RETENTION_POLICY}`,
to: 'now',
},
pageNumber: activePageIndex * pageSize,
size: pageSize,
sort: [{ groupByField: { order: 'desc' } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export const getAlertsGroupingQuery = ({
}: AlertsGroupingQueryParams) =>
getGroupingQuery({
additionalFilters,
from,
timeRange: {
from,
to,
},
groupByField: selectedGroup,
statsAggregations: !isNoneGroup([selectedGroup])
? getAggregationsByGroupField(selectedGroup)
Expand All @@ -45,7 +48,6 @@ export const getAlertsGroupingQuery = ({
uniqueValue,
size: pageSize,
sort: [{ unitsCount: { order: 'desc' } }],
to,
});

const getAggregationsByGroupField = (field: string): NamedAggregation[] => {
Expand Down

0 comments on commit ab49592

Please sign in to comment.