forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAC][Rule Registry] Paginate results for fetching existing alerts (e…
…lastic#122474) (elastic#126516) * [RAC][Rule Registry] Paginate results for fetching existing alerts * Change to difference to increase performance by 2 seconds for 50K alerts * Changing the pagination to break up the request into 10K chunks * Updating NOTICE.txt per CI instructions * Changing warning message to debug * Prefix log message with [Rule Registry] (cherry picked from commit 0236c8a) # Conflicts: # x-pack/plugins/rule_registry/server/utils/create_lifecycle_executor.ts
- Loading branch information
1 parent
599f75a
commit e2bd785
Showing
2 changed files
with
88 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
x-pack/plugins/rule_registry/server/utils/fetch_existing_alerts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { chunk } from 'lodash'; | ||
import { PublicContract } from '@kbn/utility-types'; | ||
import { IRuleDataClient } from '../rule_data_client'; | ||
import { | ||
ALERT_RULE_UUID, | ||
ALERT_UUID, | ||
TIMESTAMP, | ||
} from '../../common/technical_rule_data_field_names'; | ||
|
||
const CHUNK_SIZE = 10000; | ||
|
||
interface TrackedAlertState { | ||
alertId: string; | ||
alertUuid: string; | ||
started: string; | ||
} | ||
type RuleDataClient = PublicContract<IRuleDataClient>; | ||
|
||
const fetchAlertsForStates = async ( | ||
ruleDataClient: RuleDataClient, | ||
states: TrackedAlertState[], | ||
commonRuleFields: any | ||
) => { | ||
const request = { | ||
body: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
term: { | ||
[ALERT_RULE_UUID]: commonRuleFields[ALERT_RULE_UUID], | ||
}, | ||
}, | ||
{ | ||
terms: { | ||
[ALERT_UUID]: states.map((state) => state.alertUuid), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
size: states.length, | ||
collapse: { | ||
field: ALERT_UUID, | ||
}, | ||
sort: { | ||
[TIMESTAMP]: 'desc' as const, | ||
}, | ||
}, | ||
allow_no_indices: true, | ||
} as any; | ||
const { hits } = await ruleDataClient.getReader().search(request); | ||
return hits.hits; | ||
}; | ||
|
||
export const fetchExistingAlerts = async ( | ||
ruleDataClient: RuleDataClient, | ||
trackedAlertStates: TrackedAlertState[], | ||
commonRuleFields: any | ||
) => { | ||
const results = await Promise.all( | ||
chunk(trackedAlertStates, CHUNK_SIZE).map((states) => | ||
fetchAlertsForStates(ruleDataClient, states, commonRuleFields) | ||
) | ||
); | ||
return results.flat(); | ||
}; |