-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Alerting V2] bulk get alert actions #258353
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
Changes from all commits
dc676d6
2989159
48431fc
7fab723
d3bac88
1b180ac
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,25 @@ | ||
| /* | ||
| * 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 { esql, type EsqlRequest } from '@elastic/esql'; | ||
| import { ALERT_ACTIONS_DATA_STREAM } from '../../resources/alert_actions'; | ||
|
|
||
| export const getBulkGetAlertActionsQuery = (episodeIds: string[]): EsqlRequest => { | ||
| const episodeIdValues = episodeIds.map((id) => esql.str(id)); | ||
|
|
||
| return esql` | ||
| FROM ${ALERT_ACTIONS_DATA_STREAM} | ||
| | WHERE episode_id IN (${episodeIdValues}) | ||
| | WHERE action_type IN ("ack", "unack", "deactivate", "activate", "snooze", "unsnooze") | ||
| | STATS | ||
| last_ack_action = LAST(action_type, @timestamp) WHERE action_type IN ("ack", "unack"), | ||
| last_deactivate_action = LAST(action_type, @timestamp) WHERE action_type IN ("deactivate", "activate"), | ||
| last_snooze_action = LAST(action_type, @timestamp) WHERE action_type IN ("snooze", "unsnooze") | ||
adcoelho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| BY episode_id, rule_id, group_hash | ||
| | KEEP episode_id, rule_id, group_hash, last_ack_action, last_deactivate_action, last_snooze_action | ||
| `.toRequest(); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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 Boom from '@hapi/boom'; | ||
| import { Request, Response, type RouteHandler } from '@kbn/core-di-server'; | ||
| import type { KibanaRequest, KibanaResponseFactory, RouteSecurity } from '@kbn/core-http-server'; | ||
| import { buildRouteValidationWithZod } from '@kbn/zod-helpers'; | ||
| import { inject, injectable } from 'inversify'; | ||
| import { | ||
| bulkGetAlertActionsBodySchema, | ||
| bulkGetAlertActionsResponseSchema, | ||
| type BulkGetAlertActionsBody, | ||
| } from '@kbn/alerting-v2-schemas'; | ||
| import { AlertActionsClient } from '../../lib/alert_actions_client'; | ||
| import { ALERTING_V2_API_PRIVILEGES } from '../../lib/security/privileges'; | ||
| import { INTERNAL_ALERTING_V2_ALERT_API_PATH } from '../constants'; | ||
|
|
||
| @injectable() | ||
| export class BulkGetAlertActionsRoute implements RouteHandler { | ||
| static method = 'post' as const; | ||
| static path = `${INTERNAL_ALERTING_V2_ALERT_API_PATH}/action/_bulk_get`; | ||
| static security: RouteSecurity = { | ||
| authz: { | ||
| requiredPrivileges: [ALERTING_V2_API_PRIVILEGES.alerts.read], | ||
| }, | ||
| }; | ||
| static options = { access: 'internal' } as const; | ||
| static validate = { | ||
| request: { | ||
| body: buildRouteValidationWithZod(bulkGetAlertActionsBodySchema), | ||
| }, | ||
| response: { | ||
|
Contributor
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. nice I need to start adding these into the other routes 👍🏻
Contributor
Author
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.
I will keep in mind and update whenever I need to change something too 👌 I had actually forgotten initially, but then, when working on a different PR, I needed to know the types and noticed they were missing. |
||
| 200: { | ||
| body: buildRouteValidationWithZod(bulkGetAlertActionsResponseSchema), | ||
| }, | ||
| }, | ||
| } as const; | ||
|
Comment on lines
+23
to
+41
|
||
|
|
||
| constructor( | ||
| @inject(Request) | ||
| private readonly request: KibanaRequest<unknown, unknown, BulkGetAlertActionsBody>, | ||
| @inject(Response) private readonly response: KibanaResponseFactory, | ||
| @inject(AlertActionsClient) private readonly alertActionsClient: AlertActionsClient | ||
| ) {} | ||
|
|
||
| async handle() { | ||
| try { | ||
| const results = await this.alertActionsClient.bulkGet(this.request.body.episode_ids); | ||
|
|
||
| return this.response.ok({ body: results }); | ||
| } catch (e) { | ||
| const boom = Boom.isBoom(e) ? e : Boom.boomify(e); | ||
| return this.response.customError({ | ||
| statusCode: boom.output.statusCode, | ||
| body: boom.output.payload, | ||
adcoelho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.