-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[ResponseOps][Alerts] Add alerts grouping aggregations endpoint #186475
Changes from 10 commits
ac35282
15f9aa7
46fea59
a47b66f
e091401
ae8b482
0529905
6f57233
e39bfa5
d848db0
46fb4fd
eaa7bd6
52485c3
e8b0329
de6c865
62194c9
238e803
1c1a1fd
0731496
ca88557
8869584
11ac1fc
6fc8aee
cb2e535
90a86d1
39e0ae2
c962e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { HttpStart } from '@kbn/core-http-browser'; | ||
import type { ToastsStart } from '@kbn/core-notifications-browser'; | ||
import { SearchResponseBody } from '@elastic/elasticsearch/lib/api/types'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import type { | ||
AggregationsAggregationContainer, | ||
QueryDslQueryContainer, | ||
SortCombinations, | ||
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { BASE_RAC_ALERTS_API_PATH } from '../constants'; | ||
|
||
export interface UseGetAlertsGroupAggregationsQueryProps { | ||
http: HttpStart; | ||
toasts: ToastsStart; | ||
enabled?: boolean; | ||
params: { | ||
featureIds: AlertConsumers[]; | ||
groupByField: string; | ||
aggregations?: Record<string, AggregationsAggregationContainer>; | ||
filters?: QueryDslQueryContainer[]; | ||
sort?: SortCombinations[]; | ||
pageIndex?: number; | ||
pageSize?: number; | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches alerts aggregations for a given groupByField. | ||
* | ||
* Some default aggregations are applied: | ||
* - `groupByFields`, to get the buckets based on the provided grouping field, | ||
* - `unitsCount`, to count the number of alerts in each bucket, | ||
* - `unitsCount`, to count the total number of alerts targeted by the query, | ||
* - `groupsCount`, to count the total number of groups. | ||
* | ||
* The provided `aggregations` are applied within `groupByFields`. Here the `groupByField` runtime | ||
* field can be used to perform grouping-based aggregations. | ||
* | ||
* Applies alerting RBAC through featureIds. | ||
*/ | ||
export const useGetAlertsGroupAggregationsQuery = <T>({ | ||
http, | ||
toasts, | ||
enabled = true, | ||
params, | ||
}: UseGetAlertsGroupAggregationsQueryProps) => { | ||
const onErrorFn = (error: Error) => { | ||
if (error) { | ||
toasts.addDanger( | ||
i18n.translate( | ||
'alertsUIShared.hooks.useFindAlertsQuery.unableToFetchAlertsGroupingAggregations', | ||
{ | ||
defaultMessage: 'Unable to fetch alerts grouping aggregations', | ||
} | ||
) | ||
); | ||
} | ||
}; | ||
|
||
return useQuery({ | ||
queryKey: ['getAlertsGroupAggregations', JSON.stringify(params)], | ||
queryFn: () => | ||
http.post<SearchResponseBody<{}, T>>(`${BASE_RAC_ALERTS_API_PATH}/_group_aggregations`, { | ||
body: JSON.stringify(params), | ||
}), | ||
onError: onErrorFn, | ||
refetchOnWindowFocus: false, | ||
enabled, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||||
* 2.0. | ||||||
*/ | ||||||
import Boom from '@hapi/boom'; | ||||||
import { v4 as uuid } from 'uuid'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit:
Suggested change
|
||||||
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||||||
import { PublicMethodsOf } from '@kbn/utility-types'; | ||||||
import { Filter, buildEsQuery, EsQueryConfig } from '@kbn/es-query'; | ||||||
|
@@ -27,6 +28,7 @@ import { | |||||
|
||||||
import { | ||||||
InlineScript, | ||||||
MappingRuntimeFields, | ||||||
QueryDslQueryContainer, | ||||||
} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||||||
import { RuleTypeParams, PluginStartContract as AlertingStart } from '@kbn/alerting-plugin/server'; | ||||||
|
@@ -41,6 +43,8 @@ import { AuditLogger } from '@kbn/security-plugin/server'; | |||||
import { FieldDescriptor, IndexPatternsFetcher } from '@kbn/data-plugin/server'; | ||||||
import { isEmpty } from 'lodash'; | ||||||
import { RuleTypeRegistry } from '@kbn/alerting-plugin/server/types'; | ||||||
import { TypeOf } from 'io-ts'; | ||||||
import { DEFAULT_ALERTS_GROUP_BY_FIELD_SIZE, MAX_ALERTS_GROUPING_QUERY_SIZE } from './constants'; | ||||||
import { BrowserFields } from '../../common'; | ||||||
import { alertAuditEvent, operationAlertAuditActionMap } from './audit_events'; | ||||||
import { | ||||||
|
@@ -53,6 +57,7 @@ import { ParsedTechnicalFields } from '../../common/parse_technical_fields'; | |||||
import { IRuleDataService } from '../rule_data_plugin_service'; | ||||||
import { getAuthzFilter, getSpacesFilter } from '../lib'; | ||||||
import { fieldDescriptorToBrowserFieldMapper } from './browser_fields'; | ||||||
import { alertsAggregationsSchema } from '../../common/types'; | ||||||
|
||||||
// TODO: Fix typings https://github.com/elastic/kibana/issues/101776 | ||||||
type NonNullableProps<Obj extends {}, Props extends keyof Obj> = Omit<Obj, Props> & { | ||||||
|
@@ -88,15 +93,15 @@ export interface ConstructorOptions { | |||||
export interface UpdateOptions<Params extends RuleTypeParams> { | ||||||
id: string; | ||||||
status: string; | ||||||
_version: string | undefined; | ||||||
_version?: string; | ||||||
index: string; | ||||||
} | ||||||
|
||||||
export interface BulkUpdateOptions<Params extends RuleTypeParams> { | ||||||
ids: string[] | undefined | null; | ||||||
ids?: string[] | null; | ||||||
status: STATUS_VALUES; | ||||||
index: string; | ||||||
query: object | string | undefined | null; | ||||||
query?: object | string | null; | ||||||
} | ||||||
|
||||||
interface MgetAndAuditAlert { | ||||||
|
@@ -129,17 +134,18 @@ interface GetAlertSummaryParams { | |||||
} | ||||||
|
||||||
interface SingleSearchAfterAndAudit { | ||||||
id?: string | null | undefined; | ||||||
query?: string | object | undefined; | ||||||
aggs?: Record<string, any> | undefined; | ||||||
id?: string | null; | ||||||
query?: string | object; | ||||||
aggs?: Record<string, any>; | ||||||
index?: string; | ||||||
_source?: string[] | undefined; | ||||||
_source?: string[] | false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious about this type, why can it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To disable the |
||||||
track_total_hits?: boolean | number; | ||||||
size?: number | undefined; | ||||||
size?: number; | ||||||
operation: WriteOperations.Update | ReadOperations.Find | ReadOperations.Get; | ||||||
sort?: estypes.SortOptions[] | undefined; | ||||||
lastSortIds?: Array<string | number> | undefined; | ||||||
sort?: estypes.SortOptions[]; | ||||||
lastSortIds?: Array<string | number>; | ||||||
featureIds?: string[]; | ||||||
runtimeMappings?: MappingRuntimeFields; | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -213,13 +219,10 @@ export class AlertsClient { | |||||
items: Array<{ | ||||||
_id: string; | ||||||
// this is typed kind of crazy to fit the output of es api response to this | ||||||
_source?: | ||||||
| { | ||||||
[ALERT_RULE_TYPE_ID]?: string | null | undefined; | ||||||
[ALERT_RULE_CONSUMER]?: string | null | undefined; | ||||||
} | ||||||
| null | ||||||
| undefined; | ||||||
_source?: { | ||||||
[ALERT_RULE_TYPE_ID]?: string | null; | ||||||
[ALERT_RULE_CONSUMER]?: string | null; | ||||||
} | null; | ||||||
}>, | ||||||
operation: ReadOperations.Find | ReadOperations.Get | WriteOperations.Update | ||||||
) { | ||||||
|
@@ -236,8 +239,8 @@ export class AlertsClient { | |||||
{ hitIds: [], ownersAndRuleTypeIds: [] } as { | ||||||
hitIds: string[]; | ||||||
ownersAndRuleTypeIds: Array<{ | ||||||
[ALERT_RULE_TYPE_ID]: string | null | undefined; | ||||||
[ALERT_RULE_CONSUMER]: string | null | undefined; | ||||||
[ALERT_RULE_TYPE_ID]?: string | null; | ||||||
[ALERT_RULE_CONSUMER]?: string | null; | ||||||
}>; | ||||||
} | ||||||
); | ||||||
|
@@ -289,6 +292,7 @@ export class AlertsClient { | |||||
sort, | ||||||
lastSortIds = [], | ||||||
featureIds, | ||||||
runtimeMappings, | ||||||
}: SingleSearchAfterAndAudit) { | ||||||
try { | ||||||
const alertSpaceId = this.spaceId; | ||||||
|
@@ -322,6 +326,7 @@ export class AlertsClient { | |||||
}, | ||||||
}, | ||||||
], | ||||||
runtime_mappings: runtimeMappings, | ||||||
}; | ||||||
|
||||||
if (lastSortIds.length > 0) { | ||||||
|
@@ -982,6 +987,7 @@ export class AlertsClient { | |||||
sort, | ||||||
track_total_hits: trackTotalHits, | ||||||
_source, | ||||||
runtimeMappings, | ||||||
}: { | ||||||
aggs?: object; | ||||||
featureIds?: string[]; | ||||||
|
@@ -991,7 +997,8 @@ export class AlertsClient { | |||||
size?: number; | ||||||
sort?: estypes.SortOptions[]; | ||||||
track_total_hits?: boolean | number; | ||||||
_source?: string[]; | ||||||
_source?: string[] | false; | ||||||
runtimeMappings?: MappingRuntimeFields; | ||||||
}) { | ||||||
try { | ||||||
let indexToUse = index; | ||||||
|
@@ -1013,6 +1020,7 @@ export class AlertsClient { | |||||
operation: ReadOperations.Find, | ||||||
sort, | ||||||
lastSortIds: searchAfter, | ||||||
runtimeMappings, | ||||||
}); | ||||||
|
||||||
if (alertsSearchResponse == null) { | ||||||
|
@@ -1028,6 +1036,104 @@ export class AlertsClient { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Performs a `find` query to extract aggregations on alert groups | ||||||
*/ | ||||||
public getGroupAggregations({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding something like Nothing too elaborate, to ensure that I know we do some testing in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||||||
featureIds, | ||||||
groupByField, | ||||||
aggregations, | ||||||
filters, | ||||||
pageIndex = 0, | ||||||
pageSize = DEFAULT_ALERTS_GROUP_BY_FIELD_SIZE, | ||||||
sort = [{ unitsCount: { order: 'desc' } }], | ||||||
}: { | ||||||
/** | ||||||
* The feature ids the alerts belong to, used for authorization | ||||||
*/ | ||||||
featureIds: string[]; | ||||||
/** | ||||||
* The field to group by | ||||||
* @example "kibana.alert.rule.name" | ||||||
*/ | ||||||
groupByField: string; | ||||||
/** | ||||||
* The aggregations to perform on the groupByField buckets | ||||||
*/ | ||||||
aggregations?: TypeOf<typeof alertsAggregationsSchema>; | ||||||
/** | ||||||
* The filters to apply to the query | ||||||
*/ | ||||||
filters?: estypes.QueryDslQueryContainer[]; | ||||||
/** | ||||||
* Any sort options to apply to the groupByField aggregations | ||||||
*/ | ||||||
sort?: object[]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the |
||||||
/** | ||||||
* The page index to start from | ||||||
*/ | ||||||
pageIndex?: number; | ||||||
/** | ||||||
* The page size | ||||||
*/ | ||||||
pageSize?: number; | ||||||
}) { | ||||||
const uniqueValue = uuid(); | ||||||
return this.find({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like you were saying yesterday, QueryDSL gives me headaches 😄 |
||||||
aggs: { | ||||||
groupByField: { | ||||||
terms: { | ||||||
field: 'groupByField', | ||||||
size: MAX_ALERTS_GROUPING_QUERY_SIZE, | ||||||
}, | ||||||
aggs: { | ||||||
unitsCount: { value_count: { field: 'groupByField' } }, | ||||||
bucket_truncate: { | ||||||
bucket_sort: { | ||||||
sort, | ||||||
from: pageIndex * pageSize, | ||||||
size: pageSize, | ||||||
}, | ||||||
}, | ||||||
...(aggregations ?? {}), | ||||||
}, | ||||||
}, | ||||||
unitsCount: { value_count: { field: 'groupByField' } }, | ||||||
groupsCount: { cardinality: { field: 'groupByField' } }, | ||||||
}, | ||||||
featureIds, | ||||||
query: { | ||||||
bool: { | ||||||
filter: filters, | ||||||
}, | ||||||
}, | ||||||
runtimeMappings: { | ||||||
groupByField: { | ||||||
type: 'keyword', | ||||||
script: { | ||||||
source: | ||||||
// When size()==0, emits a uniqueValue as the value to represent this group else join by uniqueValue. | ||||||
"if (doc[params['selectedGroup']].size()==0) { emit(params['uniqueValue']) }" + | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: can use `` to avoid concatenating the strings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I thought it was worth keeping it as it was originally written by Security though, to comment the two parts separately 🙂 |
||||||
// Else, join the values with uniqueValue. We cannot simply emit the value like doc[params['selectedGroup']].value, | ||||||
// the runtime field will only return the first value in an array. | ||||||
// The docs advise that if the field has multiple values, "Scripts can call the emit method multiple times to emit multiple values." | ||||||
// However, this gives us a group for each value instead of combining the values like we're aiming for. | ||||||
// Instead of .value, we can retrieve all values with .join(). | ||||||
// Instead of joining with a "," we should join with a unique value to avoid splitting a value that happens to contain a ",". | ||||||
// We will format into a proper array in parseGroupingQuery . | ||||||
" else { emit(doc[params['selectedGroup']].join(params['uniqueValue']))}", | ||||||
params: { | ||||||
selectedGroup: groupByField, | ||||||
uniqueValue, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
size: 0, | ||||||
_source: false, | ||||||
}); | ||||||
} | ||||||
|
||||||
public async getAuthorizedAlertsIndices(featureIds: string[]): Promise<string[] | undefined> { | ||||||
try { | ||||||
const authorizedRuleTypes = await this.authorization.getAuthorizedRuleTypes( | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/** | ||
* The maximum number of alert groups to render | ||
*/ | ||
export const DEFAULT_ALERTS_GROUP_BY_FIELD_SIZE = 10; | ||
|
||
/** | ||
* Grouping pagination breaks if the field cardinality exceeds this number | ||
* | ||
* @external https://github.com/elastic/kibana/issues/151913 | ||
*/ | ||
export const MAX_ALERTS_GROUPING_QUERY_SIZE = 10000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add tests for this in
packages/kbn-alerts-ui-shared/src/common/hooks/use_get_alerts_group_aggregations_query.test.ts
? Stuff likedisplays error toast if params are not JSON
orAPI gets called with the correct body.
.