From 0b4632375a2aafe0e6e831d15a65627eb2cf6ec3 Mon Sep 17 00:00:00 2001 From: Ying Date: Mon, 14 Oct 2024 11:56:43 -0400 Subject: [PATCH 1/6] Only matching strings --- .../server/integration_tests/removed_types.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts b/x-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts index aeb182c4794e6..835cf799b60f2 100644 --- a/x-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts +++ b/x-pack/plugins/task_manager/server/integration_tests/removed_types.test.ts @@ -127,7 +127,9 @@ describe('unrecognized task types', () => { if (errorLogCalls) { // should be no workload aggregator errors for (const elog of errorLogCalls) { - expect(elog).not.toMatch(/^\[WorkloadAggregator\]: Error: Unsupported task type/i); + if (typeof elog === 'string') { + expect(elog).not.toMatch(/^\[WorkloadAggregator\]: Error: Unsupported task type/i); + } } } }); From 2134407b484ddc8d4c2893528f82c769f00eabc0 Mon Sep 17 00:00:00 2001 From: Ying Date: Fri, 25 Oct 2024 07:01:08 -0400 Subject: [PATCH 2/6] wip --- .../backfill/apis/schedule/schemas/v1.ts | 1 + .../schedule_backfill_params_schema.ts | 1 + .../transform_backfill_param_to_ad_hoc_run.ts | 1 + .../server/backfill_client/backfill_client.ts | 2 +- .../data/ad_hoc_run/types/ad_hoc_run.ts | 1 + .../transforms/transform_request/v1.ts | 7 +- .../ad_hoc_run_params_model_versions.ts | 7 ++ .../schemas/raw_ad_hoc_run_params/index.ts | 1 + .../schemas/raw_ad_hoc_run_params/v1.ts | 2 +- .../schemas/raw_ad_hoc_run_params/v2.ts | 93 +++++++++++++++++++ 10 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts diff --git a/x-pack/plugins/alerting/common/routes/backfill/apis/schedule/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/backfill/apis/schedule/schemas/v1.ts index 527134a2b5138..7906d6d4fcb97 100644 --- a/x-pack/plugins/alerting/common/routes/backfill/apis/schedule/schemas/v1.ts +++ b/x-pack/plugins/alerting/common/routes/backfill/apis/schedule/schemas/v1.ts @@ -15,6 +15,7 @@ export const scheduleBodySchema = schema.arrayOf( rule_id: schema.string(), start: schema.string(), end: schema.maybe(schema.string()), + run_actions: schema.boolean({ defaultValue: true }), }, { validate({ start, end }) { diff --git a/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schemas/schedule_backfill_params_schema.ts b/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schemas/schedule_backfill_params_schema.ts index c4a469da1b5db..82fd3ed069716 100644 --- a/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schemas/schedule_backfill_params_schema.ts +++ b/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schemas/schedule_backfill_params_schema.ts @@ -14,6 +14,7 @@ export const scheduleBackfillParamSchema = schema.object( ruleId: schema.string(), start: schema.string(), end: schema.maybe(schema.string()), + runActions: schema.boolean({ defaultValue: true }), }, { validate({ start, end }) { diff --git a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts index 4dc01a6c8939e..9bb485d7868e2 100644 --- a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts @@ -32,6 +32,7 @@ export const transformBackfillParamToAdHocRun = ( params: rule.params, apiKeyOwner: rule.apiKeyOwner, apiKeyCreatedByUser: rule.apiKeyCreatedByUser, + actions: param.runActions ? rule.actions : [], consumer: rule.consumer, enabled: rule.enabled, schedule: rule.schedule, diff --git a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts index 48b5e49c428c0..1a601ab1b3c31 100644 --- a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts +++ b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts @@ -132,7 +132,7 @@ export class BackfillClient { adHocSOsToCreate.push({ type: AD_HOC_RUN_SAVED_OBJECT_TYPE, attributes: transformBackfillParamToAdHocRun(param, rule, spaceId), - references: [reference], + references: [reference], // TODO need to extract action references here }); } else if (error) { // keep track of the error encountered for this request by index so diff --git a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts index be03f45749c5d..cfe21050c0363 100644 --- a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts @@ -27,6 +27,7 @@ type AdHocRunSORule = Pick< | 'name' | 'tags' | 'alertTypeId' + | 'actions' | 'params' | 'apiKeyOwner' | 'apiKeyCreatedByUser' diff --git a/x-pack/plugins/alerting/server/routes/backfill/apis/schedule/transforms/transform_request/v1.ts b/x-pack/plugins/alerting/server/routes/backfill/apis/schedule/transforms/transform_request/v1.ts index 170d85c4f862b..83e8a6e82c5ca 100644 --- a/x-pack/plugins/alerting/server/routes/backfill/apis/schedule/transforms/transform_request/v1.ts +++ b/x-pack/plugins/alerting/server/routes/backfill/apis/schedule/transforms/transform_request/v1.ts @@ -10,4 +10,9 @@ import { ScheduleBackfillRequestBodyV1 } from '../../../../../../../common/route import { ScheduleBackfillParams } from '../../../../../../application/backfill/methods/schedule/types'; export const transformRequest = (request: ScheduleBackfillRequestBodyV1): ScheduleBackfillParams => - request.map(({ rule_id, start, end }) => ({ ruleId: rule_id, start, end })); + request.map(({ rule_id, start, end, run_actions }) => ({ + ruleId: rule_id, + start, + end, + runActions: run_actions, + })); diff --git a/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts b/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts index 95f544be5c8e2..d8c15c3d5cb27 100644 --- a/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts +++ b/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts @@ -16,4 +16,11 @@ export const adHocRunParamsModelVersions: SavedObjectsModelVersionMap = { create: rawAdHocRunParamsSchemaV1, }, }, + // '2': { + // changes: [], + // schemas: { + // forwardCompatibility: rawAdHocRunParamsSchemaV2.extends({}, { unknowns: 'ignore' }), + // create: rawAdHocRunParamsSchemaV2, + // }, + // }, }; diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/index.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/index.ts index 977a13f3a7e4b..17907c6405830 100644 --- a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/index.ts +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/index.ts @@ -6,3 +6,4 @@ */ export { rawAdHocRunParamsSchema as rawAdHocRunParamsSchemaV1 } from './v1'; +export { rawAdHocRunParamsSchema as rawAdHocRunParamsSchemaV2 } from './v2'; diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v1.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v1.ts index 8676c2c606912..5970599492422 100644 --- a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v1.ts +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v1.ts @@ -21,7 +21,7 @@ const rawAdHocRunSchedule = schema.object({ runAt: schema.string(), }); -const rawAdHocRunParamsRuleSchema = schema.object({ +export const rawAdHocRunParamsRuleSchema = schema.object({ name: schema.string(), tags: schema.arrayOf(schema.string()), alertTypeId: schema.string(), diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts new file mode 100644 index 0000000000000..00b7807b35acc --- /dev/null +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts @@ -0,0 +1,93 @@ +/* + * 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 { schema } from '@kbn/config-schema'; +import { + rawAdHocRunParamsSchema as rawAdHocRunParamsSchemaV1, + rawAdHocRunParamsRuleSchema as rawAdHocRunParamsRuleSchemaV1, +} from './v1'; + +const ISOWeekdaysSchema = schema.oneOf([ + schema.literal(1), + schema.literal(2), + schema.literal(3), + schema.literal(4), + schema.literal(5), + schema.literal(6), + schema.literal(7), +]); + +const rawRuleAlertsFilterSchema = schema.object({ + query: schema.maybe( + schema.object({ + kql: schema.string(), + filters: schema.arrayOf( + schema.object({ + query: schema.maybe(schema.recordOf(schema.string(), schema.any())), + meta: schema.object({ + alias: schema.maybe(schema.nullable(schema.string())), + disabled: schema.maybe(schema.boolean()), + negate: schema.maybe(schema.boolean()), + controlledBy: schema.maybe(schema.string()), + group: schema.maybe(schema.string()), + index: schema.maybe(schema.string()), + isMultiIndex: schema.maybe(schema.boolean()), + type: schema.maybe(schema.string()), + key: schema.maybe(schema.string()), + params: schema.maybe(schema.any()), + value: schema.maybe(schema.string()), + field: schema.maybe(schema.string()), + relation: schema.maybe(schema.oneOf([schema.literal('OR'), schema.literal('AND')])), + }), + $state: schema.maybe( + schema.object({ + store: schema.oneOf([schema.literal('appState'), schema.literal('globalState')]), + }) + ), + }) + ), + dsl: schema.maybe(schema.string()), + }) + ), + timeframe: schema.maybe( + schema.object({ + days: schema.arrayOf(ISOWeekdaysSchema), + hours: schema.object({ + start: schema.string(), + end: schema.string(), + }), + timezone: schema.string(), + }) + ), +}); + +const rawAdHocRunParamsRuleActionSchema = schema.object({ + uuid: schema.maybe(schema.string()), + group: schema.maybe(schema.string()), + actionRef: schema.string(), + actionTypeId: schema.string(), + params: schema.recordOf(schema.string(), schema.any()), + frequency: schema.maybe( + schema.object({ + summary: schema.boolean(), + notifyWhen: schema.oneOf([ + schema.literal('onActionGroupChange'), + schema.literal('onActiveAlert'), + schema.literal('onThrottleInterval'), + ]), + throttle: schema.nullable(schema.string()), + }) + ), + alertsFilter: schema.maybe(rawRuleAlertsFilterSchema), + useAlertDataForTemplate: schema.maybe(schema.boolean()), +}); + +const rawAdHocRunParamsRuleSchema = rawAdHocRunParamsRuleSchemaV1.extends({ + actions: schema.arrayOf(rawAdHocRunParamsRuleActionSchema), +}); + +export const rawAdHocRunParamsSchema = rawAdHocRunParamsSchemaV1.extends({}); From fcfaec0ad3ece49722413afe75133af9ad3111b4 Mon Sep 17 00:00:00 2001 From: Ying Date: Mon, 11 Nov 2024 12:27:49 -0500 Subject: [PATCH 3/6] Updating backfill scheduling to accept actions --- .../methods/schedule/schedule_backfill.ts | 1 + .../backfill/result/schemas/index.ts | 2 ++ ...transform_ad_hoc_run_to_backfill_result.ts | 10 ++++++ .../transform_backfill_param_to_ad_hoc_run.ts | 4 ++- .../server/backfill_client/backfill_client.ts | 33 +++++++++++++++---- .../data/ad_hoc_run/types/ad_hoc_run.ts | 3 +- .../rules_client/lib/denormalize_actions.ts | 11 +++---- .../rules_client/lib/extract_references.ts | 5 ++- .../ad_hoc_run_params_model_versions.ts | 19 ++++++----- .../schemas/raw_ad_hoc_run_params/v2.ts | 4 ++- 10 files changed, 66 insertions(+), 26 deletions(-) diff --git a/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schedule_backfill.ts b/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schedule_backfill.ts index 534262aa31c31..2f2816bf95867 100644 --- a/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schedule_backfill.ts +++ b/x-pack/plugins/alerting/server/application/backfill/methods/schedule/schedule_backfill.ts @@ -145,6 +145,7 @@ export async function scheduleBackfill( const actionsClient = await context.getActionsClient(); return await context.backfillClient.bulkQueue({ + actionsClient, auditLogger: context.auditLogger, params, rules: rulesToSchedule.map(({ id, attributes, references }) => { diff --git a/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts b/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts index b454d41dd40ca..8735cf43fd176 100644 --- a/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts +++ b/x-pack/plugins/alerting/server/application/backfill/result/schemas/index.ts @@ -8,6 +8,7 @@ import { schema } from '@kbn/config-schema'; import { ruleParamsSchema } from '@kbn/response-ops-rule-params'; import { adHocRunStatus } from '../../../../../common/constants'; +import { actionSchema as ruleActionSchema } from '../../../rule/schemas/action_schemas'; export const statusSchema = schema.oneOf([ schema.literal(adHocRunStatus.COMPLETE), @@ -32,6 +33,7 @@ export const backfillSchema = schema.object({ id: schema.string(), name: schema.string(), tags: schema.arrayOf(schema.string()), + actions: schema.arrayOf(ruleActionSchema), alertTypeId: schema.string(), params: ruleParamsSchema, apiKeyOwner: schema.nullable(schema.string()), diff --git a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_ad_hoc_run_to_backfill_result.ts b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_ad_hoc_run_to_backfill_result.ts index 13257742b7005..0e84e4ea7e69f 100644 --- a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_ad_hoc_run_to_backfill_result.ts +++ b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_ad_hoc_run_to_backfill_result.ts @@ -6,11 +6,14 @@ */ import { SavedObject, SavedObjectsBulkCreateObject } from '@kbn/core/server'; +import { ActionsClient } from '@kbn/actions-plugin/server'; import { AdHocRunSO } from '../../../data/ad_hoc_run/types'; import { createBackfillError } from '../../../backfill_client/lib'; import { ScheduleBackfillResult } from '../methods/schedule/types'; +import { transformRawActionsToDomainActions } from '../../rule/transforms'; export const transformAdHocRunToBackfillResult = ( + actionsClient: ActionsClient, { id, attributes, references, error }: SavedObject, originalSO?: SavedObjectsBulkCreateObject ): ScheduleBackfillResult => { @@ -55,6 +58,13 @@ export const transformAdHocRunToBackfillResult = ( rule: { ...attributes.rule, id: references[0].id, + actions: transformRawActionsToDomainActions({ + ruleId: id, + actions: attributes.rule.actions, + references, + isSystemAction: (connectorId: string) => actionsClient.isSystemAction(connectorId), + omitGeneratedValues: true, + }), }, spaceId: attributes.spaceId, start: attributes.start, diff --git a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts index 9bb485d7868e2..2618cce31b810 100644 --- a/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/application/backfill/transforms/transform_backfill_param_to_ad_hoc_run.ts @@ -6,6 +6,7 @@ */ import { isString } from 'lodash'; +import { DenormalizedAction } from '../../../rules_client'; import { AdHocRunSO } from '../../../data/ad_hoc_run/types'; import { calculateSchedule } from '../../../backfill_client/lib'; import { adHocRunStatus } from '../../../../common/constants'; @@ -15,6 +16,7 @@ import { ScheduleBackfillParam } from '../methods/schedule/types'; export const transformBackfillParamToAdHocRun = ( param: ScheduleBackfillParam, rule: RuleDomain, + actions: DenormalizedAction[], spaceId: string ): AdHocRunSO => { const schedule = calculateSchedule(param.start, rule.schedule.interval, param.end); @@ -32,7 +34,7 @@ export const transformBackfillParamToAdHocRun = ( params: rule.params, apiKeyOwner: rule.apiKeyOwner, apiKeyCreatedByUser: rule.apiKeyCreatedByUser, - actions: param.runActions ? rule.actions : [], + actions: param.runActions ? (actions as unknown as AdHocRunSO['rule']['actions']) : [], consumer: rule.consumer, enabled: rule.enabled, schedule: rule.schedule, diff --git a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts index 1a601ab1b3c31..0c9c4d965827c 100644 --- a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts +++ b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts @@ -23,6 +23,8 @@ import { TaskPriority, } from '@kbn/task-manager-plugin/server'; import { isNumber } from 'lodash'; +import { ActionsClient } from '@kbn/actions-plugin/server'; +import { asyncForEach } from '@kbn/std'; import { ScheduleBackfillError, ScheduleBackfillParam, @@ -42,6 +44,8 @@ import { AD_HOC_RUN_SAVED_OBJECT_TYPE, RULE_SAVED_OBJECT_TYPE } from '../saved_o import { TaskRunnerFactory } from '../task_runner'; import { RuleTypeRegistry } from '../types'; import { createBackfillError } from './lib'; +import { denormalizeActions } from '../rules_client/lib/denormalize_actions'; +import { NormalizedAlertActionWithGeneratedValues } from '../rules_client'; export const BACKFILL_TASK_TYPE = 'ad_hoc_run-backfill'; @@ -53,6 +57,7 @@ interface ConstructorOpts { } interface BulkQueueOpts { + actionsClient: ActionsClient; auditLogger?: AuditLogger; params: ScheduleBackfillParams; rules: RuleDomain[]; @@ -86,6 +91,7 @@ export class BackfillClient { } public async bulkQueue({ + actionsClient, auditLogger, params, rules, @@ -118,7 +124,7 @@ export class BackfillClient { const soToCreateIndexOrErrorMap: Map = new Map(); - params.forEach((param: ScheduleBackfillParam, ndx: number) => { + await asyncForEach(params, async (param: ScheduleBackfillParam, ndx: number) => { // For this schedule request, look up the rule or return error const { rule, error } = getRuleOrError(param.ruleId, rules, ruleTypeRegistry); if (rule) { @@ -129,19 +135,21 @@ export class BackfillClient { name: `rule`, type: RULE_SAVED_OBJECT_TYPE, }; + const { references: actionReferences, actions } = await denormalizeActions( + () => Promise.resolve(actionsClient), + rule.actions as NormalizedAlertActionWithGeneratedValues[] + ); adHocSOsToCreate.push({ type: AD_HOC_RUN_SAVED_OBJECT_TYPE, - attributes: transformBackfillParamToAdHocRun(param, rule, spaceId), - references: [reference], // TODO need to extract action references here + attributes: transformBackfillParamToAdHocRun(param, rule, actions, spaceId), + references: [reference, ...actionReferences], }); } else if (error) { // keep track of the error encountered for this request by index so // we can return it in order soToCreateIndexOrErrorMap.set(ndx, error); this.logger.warn( - `No rule found for ruleId ${param.ruleId} - not scheduling backfill for ${JSON.stringify( - param - )}` + `Error for ruleId ${param.ruleId} - not scheduling backfill for ${JSON.stringify(param)}` ); } }); @@ -175,7 +183,7 @@ export class BackfillClient { }) ); } - return transformAdHocRunToBackfillResult(so, adHocSOsToCreate?.[index]); + return transformAdHocRunToBackfillResult(actionsClient, so, adHocSOsToCreate?.[index]); } ); @@ -343,5 +351,16 @@ function getRuleOrError( }; } + // validate that we can run all the actions that are configured + const hasUnsupportedActions = rule.actions.some( + (action) => action.frequency?.notifyWhen !== 'onActiveAlert' + ); + + if (hasUnsupportedActions) { + return { + error: createBackfillError(`Rule ${ruleId} has unsupported actions`, ruleId, rule.name), + }; + } + return { rule }; } diff --git a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts index cfe21050c0363..06227f24af6fd 100644 --- a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { RawRule } from '../../../types'; import { RuleDomain } from '../../../application/rule/types'; import { AdHocRunStatus } from '../../../../common/constants'; @@ -23,7 +24,7 @@ export interface AdHocRunSchedule extends Record { // the backfill job was scheduled. if there are updates to the rule configuration // after the backfill is scheduled, they will not be reflected during the backfill run. type AdHocRunSORule = Pick< - RuleDomain, + RawRule, | 'name' | 'tags' | 'alertTypeId' diff --git a/x-pack/plugins/alerting/server/rules_client/lib/denormalize_actions.ts b/x-pack/plugins/alerting/server/rules_client/lib/denormalize_actions.ts index 3cd1113a13628..e7303888ca8b8 100644 --- a/x-pack/plugins/alerting/server/rules_client/lib/denormalize_actions.ts +++ b/x-pack/plugins/alerting/server/rules_client/lib/denormalize_actions.ts @@ -5,25 +5,22 @@ * 2.0. */ import { SavedObjectReference } from '@kbn/core/server'; +import { ActionsClient } from '@kbn/actions-plugin/server'; import { preconfiguredConnectorActionRefPrefix, systemConnectorActionRefPrefix, } from '../common/constants'; -import { - DenormalizedAction, - NormalizedAlertActionWithGeneratedValues, - RulesClientContext, -} from '../types'; +import { DenormalizedAction, NormalizedAlertActionWithGeneratedValues } from '../types'; export async function denormalizeActions( - context: RulesClientContext, + getActionsClient: () => Promise, alertActions: NormalizedAlertActionWithGeneratedValues[] ): Promise<{ actions: DenormalizedAction[]; references: SavedObjectReference[] }> { const references: SavedObjectReference[] = []; const actions: DenormalizedAction[] = []; if (alertActions.length) { - const actionsClient = await context.getActionsClient(); + const actionsClient = await getActionsClient(); const actionIds = [...new Set(alertActions.map((alertAction) => alertAction.id))]; const actionResults = await actionsClient.getBulk({ diff --git a/x-pack/plugins/alerting/server/rules_client/lib/extract_references.ts b/x-pack/plugins/alerting/server/rules_client/lib/extract_references.ts index 9dfca0897ca08..167fee5a46c52 100644 --- a/x-pack/plugins/alerting/server/rules_client/lib/extract_references.ts +++ b/x-pack/plugins/alerting/server/rules_client/lib/extract_references.ts @@ -26,7 +26,10 @@ export async function extractReferences< params: ExtractedParams; references: SavedObjectReference[]; }> { - const { references: actionReferences, actions } = await denormalizeActions(context, ruleActions); + const { references: actionReferences, actions } = await denormalizeActions( + context.getActionsClient, + ruleActions + ); // Extracts any references using configured reference extractor if available const extractedRefsAndParams = ruleType?.useSavedObjectReferences?.extractReferences diff --git a/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts b/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts index d8c15c3d5cb27..91a8418a42a37 100644 --- a/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts +++ b/x-pack/plugins/alerting/server/saved_objects/model_versions/ad_hoc_run_params_model_versions.ts @@ -6,7 +6,10 @@ */ import { SavedObjectsModelVersionMap } from '@kbn/core-saved-objects-server'; -import { rawAdHocRunParamsSchemaV1 } from '../schemas/raw_ad_hoc_run_params'; +import { + rawAdHocRunParamsSchemaV1, + rawAdHocRunParamsSchemaV2, +} from '../schemas/raw_ad_hoc_run_params'; export const adHocRunParamsModelVersions: SavedObjectsModelVersionMap = { '1': { @@ -16,11 +19,11 @@ export const adHocRunParamsModelVersions: SavedObjectsModelVersionMap = { create: rawAdHocRunParamsSchemaV1, }, }, - // '2': { - // changes: [], - // schemas: { - // forwardCompatibility: rawAdHocRunParamsSchemaV2.extends({}, { unknowns: 'ignore' }), - // create: rawAdHocRunParamsSchemaV2, - // }, - // }, + '2': { + changes: [], + schemas: { + forwardCompatibility: rawAdHocRunParamsSchemaV2.extends({}, { unknowns: 'ignore' }), + create: rawAdHocRunParamsSchemaV2, + }, + }, }; diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts index 00b7807b35acc..374f3ed942e8c 100644 --- a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts @@ -90,4 +90,6 @@ const rawAdHocRunParamsRuleSchema = rawAdHocRunParamsRuleSchemaV1.extends({ actions: schema.arrayOf(rawAdHocRunParamsRuleActionSchema), }); -export const rawAdHocRunParamsSchema = rawAdHocRunParamsSchemaV1.extends({}); +export const rawAdHocRunParamsSchema = rawAdHocRunParamsSchemaV1.extends({ + rule: rawAdHocRunParamsRuleSchema, +}); From b27940b4a1f53556f78eff8b4366b57c1dafcdaf Mon Sep 17 00:00:00 2001 From: Ying Date: Tue, 12 Nov 2024 14:54:24 -0500 Subject: [PATCH 4/6] Running actions in ad hoc task runner --- .../actions/server/create_execute_function.ts | 10 +++- .../lib/get_summarized_alerts_query.ts | 3 ++ .../backfill/methods/find/find_backfill.ts | 4 +- .../backfill/methods/get/get_backfill.ts | 3 +- .../data/ad_hoc_run/types/ad_hoc_run.ts | 3 +- .../schemas/raw_ad_hoc_run_params/latest.ts | 11 ++++ .../schemas/raw_ad_hoc_run_params/v2.ts | 10 ++-- .../lib/format_action_to_enqueue.ts | 5 +- .../schedulers/per_alert_action_scheduler.ts | 1 + .../schedulers/summary_action_scheduler.ts | 1 + .../schedulers/system_action_scheduler.ts | 1 + .../task_runner/action_scheduler/types.ts | 4 +- .../server/task_runner/ad_hoc_task_runner.ts | 51 +++++++++++++++++-- .../mark_available_tasks_as_claimed.ts | 4 +- .../model_versions/task_model_versions.ts | 16 +++++- .../server/saved_objects/schemas/task.ts | 4 ++ x-pack/plugins/task_manager/server/task.ts | 5 ++ 17 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/latest.ts diff --git a/x-pack/plugins/actions/server/create_execute_function.ts b/x-pack/plugins/actions/server/create_execute_function.ts index a92bff9719559..f4eb894239a0f 100644 --- a/x-pack/plugins/actions/server/create_execute_function.ts +++ b/x-pack/plugins/actions/server/create_execute_function.ts @@ -6,7 +6,11 @@ */ import { SavedObjectsBulkResponse, SavedObjectsClientContract, Logger } from '@kbn/core/server'; -import { RunNowResult, TaskManagerStartContract } from '@kbn/task-manager-plugin/server'; +import { + RunNowResult, + TaskManagerStartContract, + TaskPriority, +} from '@kbn/task-manager-plugin/server'; import { RawAction, ActionTypeRegistryContract, @@ -36,6 +40,7 @@ export interface ExecuteOptions apiKey: string | null; executionId: string; actionTypeId: string; + priority?: TaskPriority; } interface ActionTaskParams @@ -179,7 +184,7 @@ export function createBulkExecutionEnqueuerFunction({ const actionTaskParamsRecords: SavedObjectsBulkResponse = await unsecuredSavedObjectsClient.bulkCreate(actions, { refresh: false }); - const taskInstances = actionTaskParamsRecords.saved_objects.map((so) => { + const taskInstances = actionTaskParamsRecords.saved_objects.map((so, index) => { const actionId = so.attributes.actionId; return { taskType: `actions:${actionTypeIds[actionId]}`, @@ -189,6 +194,7 @@ export function createBulkExecutionEnqueuerFunction({ }, state: {}, scope: ['actions'], + ...(runnableActions[index].priority ? { priority: runnableActions[index].priority } : {}), }; }); diff --git a/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts b/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts index ab3edece0becc..fa115975948d6 100644 --- a/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts +++ b/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts @@ -165,7 +165,10 @@ const getQueryByExecutionUuid = ({ }); } + console.log(`getQueryByExecutionUuid ${JSON.stringify(filter)}`); + if (alertsFilter) { + console.log(`alertsFilter ${JSON.stringify(alertsFilter)}`); filter.push(...generateAlertsFilterDSL(alertsFilter)); } diff --git a/x-pack/plugins/alerting/server/application/backfill/methods/find/find_backfill.ts b/x-pack/plugins/alerting/server/application/backfill/methods/find/find_backfill.ts index 522128d0385f8..b52394c07f2ea 100644 --- a/x-pack/plugins/alerting/server/application/backfill/methods/find/find_backfill.ts +++ b/x-pack/plugins/alerting/server/application/backfill/methods/find/find_backfill.ts @@ -98,6 +98,8 @@ export async function findBackfill( ...(params.sortOrder ? { sortOrder: params.sortOrder } : {}), }); + const actionsClient = await context.getActionsClient(); + const transformedData: Backfill[] = data.map((so: SavedObject) => { context.auditLogger?.log( adHocRunAuditEvent({ @@ -110,7 +112,7 @@ export async function findBackfill( }) ); - return transformAdHocRunToBackfillResult(so) as Backfill; + return transformAdHocRunToBackfillResult(actionsClient, so) as Backfill; }); return { diff --git a/x-pack/plugins/alerting/server/application/backfill/methods/get/get_backfill.ts b/x-pack/plugins/alerting/server/application/backfill/methods/get/get_backfill.ts index 6f14dba88684c..152025bcd16dd 100644 --- a/x-pack/plugins/alerting/server/application/backfill/methods/get/get_backfill.ts +++ b/x-pack/plugins/alerting/server/application/backfill/methods/get/get_backfill.ts @@ -73,7 +73,8 @@ export async function getBackfill(context: RulesClientContext, id: string): Prom }) ); - return transformAdHocRunToBackfillResult(result) as Backfill; + const actionsClient = await context.getActionsClient(); + return transformAdHocRunToBackfillResult(actionsClient, result) as Backfill; } catch (err) { const errorMessage = `Failed to get backfill by id: ${id}`; context.logger.error(`${errorMessage} - ${err}`); diff --git a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts index 06227f24af6fd..cfe21050c0363 100644 --- a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { RawRule } from '../../../types'; import { RuleDomain } from '../../../application/rule/types'; import { AdHocRunStatus } from '../../../../common/constants'; @@ -24,7 +23,7 @@ export interface AdHocRunSchedule extends Record { // the backfill job was scheduled. if there are updates to the rule configuration // after the backfill is scheduled, they will not be reflected during the backfill run. type AdHocRunSORule = Pick< - RawRule, + RuleDomain, | 'name' | 'tags' | 'alertTypeId' diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/latest.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/latest.ts new file mode 100644 index 0000000000000..03c55b706231d --- /dev/null +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/latest.ts @@ -0,0 +1,11 @@ +/* + * 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 { TypeOf } from '@kbn/config-schema'; +import { rawAdHocRunParamsSchema } from './v2'; + +export type RawAdHocRunParams = TypeOf; diff --git a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts index 374f3ed942e8c..d0597786ff6ed 100644 --- a/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts +++ b/x-pack/plugins/alerting/server/saved_objects/schemas/raw_ad_hoc_run_params/v2.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { FilterStateStore } from '@kbn/es-query'; import { rawAdHocRunParamsSchema as rawAdHocRunParamsSchemaV1, rawAdHocRunParamsRuleSchema as rawAdHocRunParamsRuleSchemaV1, @@ -45,12 +46,15 @@ const rawRuleAlertsFilterSchema = schema.object({ }), $state: schema.maybe( schema.object({ - store: schema.oneOf([schema.literal('appState'), schema.literal('globalState')]), + store: schema.oneOf([ + schema.literal(FilterStateStore.APP_STATE), // change + schema.literal(FilterStateStore.GLOBAL_STATE), // change + ]), }) ), }) ), - dsl: schema.maybe(schema.string()), + dsl: schema.string(), // change }) ), timeframe: schema.maybe( @@ -66,7 +70,7 @@ const rawRuleAlertsFilterSchema = schema.object({ }); const rawAdHocRunParamsRuleActionSchema = schema.object({ - uuid: schema.maybe(schema.string()), + uuid: schema.string(), group: schema.maybe(schema.string()), actionRef: schema.string(), actionTypeId: schema.string(), diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/format_action_to_enqueue.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/format_action_to_enqueue.ts index af560a19ab9be..ce08d7f17667b 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/format_action_to_enqueue.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/format_action_to_enqueue.ts @@ -7,12 +7,14 @@ import { RuleAction, RuleSystemAction } from '@kbn/alerting-types'; import { asSavedObjectExecutionSource } from '@kbn/actions-plugin/server'; +import { TaskPriority } from '@kbn/task-manager-plugin/server'; import { RULE_SAVED_OBJECT_TYPE } from '../../..'; interface FormatActionToEnqueueOpts { action: RuleAction | RuleSystemAction; apiKey: string | null; executionId: string; + priority?: TaskPriority; ruleConsumer: string; ruleId: string; ruleTypeId: string; @@ -20,7 +22,7 @@ interface FormatActionToEnqueueOpts { } export const formatActionToEnqueue = (opts: FormatActionToEnqueueOpts) => { - const { action, apiKey, executionId, ruleConsumer, ruleId, ruleTypeId, spaceId } = opts; + const { action, apiKey, executionId, priority, ruleConsumer, ruleId, ruleTypeId, spaceId } = opts; const namespace = spaceId === 'default' ? {} : { namespace: spaceId }; return { @@ -44,5 +46,6 @@ export const formatActionToEnqueue = (opts: FormatActionToEnqueueOpts) => { }, ], actionTypeId: action.actionTypeId, + priority, }; }; diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/per_alert_action_scheduler.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/per_alert_action_scheduler.ts index 28b35d885b3d2..d6bdef51d8438 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/per_alert_action_scheduler.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/per_alert_action_scheduler.ts @@ -241,6 +241,7 @@ export class PerAlertActionScheduler< action: actionToRun, apiKey: this.context.apiKey, executionId: this.context.executionId, + priority: this.context.priority, ruleConsumer: this.context.ruleConsumer, ruleId: this.context.rule.id, ruleTypeId: this.context.ruleType.id, diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/summary_action_scheduler.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/summary_action_scheduler.ts index db53f15be2180..f6bdb2472f44e 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/summary_action_scheduler.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/summary_action_scheduler.ts @@ -207,6 +207,7 @@ export class SummaryActionScheduler< action: actionToRun, apiKey: this.context.apiKey, executionId: this.context.executionId, + priority: this.context.priority, ruleConsumer: this.context.ruleConsumer, ruleId: this.context.rule.id, ruleTypeId: this.context.ruleType.id, diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/system_action_scheduler.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/system_action_scheduler.ts index 0c5cceb0f0a52..f3aba6a17b0db 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/system_action_scheduler.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/schedulers/system_action_scheduler.ts @@ -157,6 +157,7 @@ export class SystemActionScheduler< action: actionToRun, apiKey: this.context.apiKey, executionId: this.context.executionId, + priority: this.context.priority, ruleConsumer: this.context.ruleConsumer, ruleId: this.context.rule.id, ruleTypeId: this.context.ruleType.id, diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts index 02b9647f91866..f1618db04e8f2 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts @@ -9,6 +9,7 @@ import type { Logger } from '@kbn/core/server'; import { PublicMethodsOf } from '@kbn/utility-types'; import { ActionsClient } from '@kbn/actions-plugin/server/actions_client'; import { ExecuteOptions as EnqueueExecutionOptions } from '@kbn/actions-plugin/server/create_execute_function'; +import { TaskPriority } from '@kbn/task-manager-plugin/server'; import { IAlertsClient } from '../../alerts_client/types'; import { Alert } from '../../alert'; import { @@ -53,7 +54,7 @@ export interface ActionSchedulerOptions< >; logger: Logger; alertingEventLogger: PublicMethodsOf; - rule: SanitizedRule; + rule: Omit, 'executionStatus'>; taskRunnerContext: TaskRunnerContext; taskInstance: RuleTaskInstance; ruleRunMetricsStore: RuleRunMetricsStore; @@ -64,6 +65,7 @@ export interface ActionSchedulerOptions< previousStartedAt: Date | null; actionsClient: PublicMethodsOf; alertsClient: IAlertsClient; + priority?: TaskPriority; } export type Executable< diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts index d126151030672..1e914d53ff13e 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts @@ -20,11 +20,12 @@ import { TaskErrorSource, } from '@kbn/task-manager-plugin/server'; import { nanosToMillis } from '@kbn/event-log-plugin/common'; -import { CancellableTask, RunResult } from '@kbn/task-manager-plugin/server/task'; +import { CancellableTask, RunResult, TaskPriority } from '@kbn/task-manager-plugin/server/task'; import { AdHocRunStatus, adHocRunStatus } from '../../common/constants'; import { RuleRunnerErrorStackTraceLog, RuleTaskStateAndMetrics, TaskRunnerContext } from './types'; import { getExecutorServices } from './get_executor_services'; import { ErrorWithReason, validateRuleTypeParams } from '../lib'; +import { transformRawActionsToDomainActions } from '../application/rule/transforms'; import { AlertInstanceContext, AlertInstanceState, @@ -35,7 +36,7 @@ import { RuleTypeState, } from '../types'; import { TaskRunnerTimer, TaskRunnerTimerSpan } from './task_runner_timer'; -import { AdHocRun, AdHocRunSchedule, AdHocRunSO } from '../data/ad_hoc_run/types'; +import { AdHocRun, AdHocRunSchedule } from '../data/ad_hoc_run/types'; import { AD_HOC_RUN_SAVED_OBJECT_TYPE } from '../saved_objects'; import { RuleMonitoringService } from '../monitoring/rule_monitoring_service'; import { AdHocTaskRunningHandler } from './ad_hoc_task_running_handler'; @@ -52,6 +53,8 @@ import { import { RuleRunMetrics, RuleRunMetricsStore } from '../lib/rule_run_metrics_store'; import { getEsErrorMessage } from '../lib/errors'; import { Result, isOk, asOk, asErr } from '../lib/result_type'; +import { RawAdHocRunParams } from '../saved_objects/schemas/raw_ad_hoc_run_params/latest'; +import { ActionScheduler } from './action_scheduler'; interface ConstructorParams { context: TaskRunnerContext; @@ -173,7 +176,7 @@ export class AdHocTaskRunner implements CancellableTask { return ruleRunMetricsStore.getMetrics(); } - const { rule } = adHocRunData; + const { rule, apiKeyToUse } = adHocRunData; const ruleType = this.ruleTypeRegistry.get(rule.alertTypeId); const ruleLabel = `${ruleType.id}:${rule.id}: '${rule.name}'`; @@ -253,6 +256,35 @@ export class AdHocTaskRunner implements CancellableTask { throw error; } + const actionScheduler = new ActionScheduler({ + rule: { + ...rule, + muteAll: false, + mutedInstanceIds: [], + createdAt: new Date(rule.createdAt), + updatedAt: new Date(rule.updatedAt), + }, + ruleType, + logger: this.logger, + taskRunnerContext: this.context, + taskInstance: this.taskInstance, + ruleRunMetricsStore, + apiKey: apiKeyToUse, + ruleConsumer: rule.consumer, + executionId: this.executionId, + ruleLabel, + previousStartedAt: null, + alertingEventLogger: this.alertingEventLogger, + actionsClient: await this.context.actionsPlugin.getActionsClientWithRequest(fakeRequest), + alertsClient, + priority: TaskPriority.Low, + }); + + await actionScheduler.run({ + activeCurrentAlerts: alertsClient.getProcessedAlerts('activeCurrent'), + recoveredCurrentAlerts: alertsClient.getProcessedAlerts('recoveredCurrent'), + }); + return ruleRunMetricsStore.getMetrics(); } @@ -291,8 +323,8 @@ export class AdHocTaskRunner implements CancellableTask { let adHocRunData: AdHocRun; try { - const adHocRunSO: SavedObject = - await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( + const adHocRunSO: SavedObject = + await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( AD_HOC_RUN_SAVED_OBJECT_TYPE, adHocRunParamsId, { namespace } @@ -304,6 +336,15 @@ export class AdHocTaskRunner implements CancellableTask { rule: { ...adHocRunSO.attributes.rule, id: adHocRunSO.references[0].id, + params: adHocRunSO.attributes.rule.params as unknown as never, + actions: transformRawActionsToDomainActions({ + ruleId: adHocRunSO.references[0].id, + actions: adHocRunSO.attributes.rule.actions, + references: adHocRunSO.references, + isSystemAction: (connectorId: string) => + this.context.actionsPlugin.isSystemActionConnector(connectorId), + omitGeneratedValues: false, + }), }, }; } catch (err) { diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts index 4e138545aec25..e6c5feb28b5ac 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts @@ -128,7 +128,9 @@ function getSortByPriority(definitions: TaskTypeDictionary): estypes.SortCombina // TODO: we could do this locally as well, but they may starve source: ` String taskType = doc['task.taskType'].value; - if (params.priority_map.containsKey(taskType)) { + if (doc['task.priority'].value != null) { + return doc['task.priority'].value; + } else if (params.priority_map.containsKey(taskType)) { return params.priority_map[taskType]; } else { return ${TaskPriority.Normal}; diff --git a/x-pack/plugins/task_manager/server/saved_objects/model_versions/task_model_versions.ts b/x-pack/plugins/task_manager/server/saved_objects/model_versions/task_model_versions.ts index 775b3ea2f8cad..4c7ae514f0bb7 100644 --- a/x-pack/plugins/task_manager/server/saved_objects/model_versions/task_model_versions.ts +++ b/x-pack/plugins/task_manager/server/saved_objects/model_versions/task_model_versions.ts @@ -6,7 +6,7 @@ */ import { SavedObjectsModelVersionMap } from '@kbn/core-saved-objects-server'; -import { taskSchemaV1, taskSchemaV2 } from '../schemas/task'; +import { taskSchemaV1, taskSchemaV2, taskSchemaV3 } from '../schemas/task'; export const taskModelVersions: SavedObjectsModelVersionMap = { '1': { @@ -35,4 +35,18 @@ export const taskModelVersions: SavedObjectsModelVersionMap = { create: taskSchemaV2, }, }, + '3': { + changes: [ + { + type: 'mappings_addition', + addedMappings: { + priority: { type: 'integer' }, + }, + }, + ], + schemas: { + forwardCompatibility: taskSchemaV3.extends({}, { unknowns: 'ignore' }), + create: taskSchemaV3, + }, + }, }; diff --git a/x-pack/plugins/task_manager/server/saved_objects/schemas/task.ts b/x-pack/plugins/task_manager/server/saved_objects/schemas/task.ts index 2a6ee5c92198c..b6e724099912d 100644 --- a/x-pack/plugins/task_manager/server/saved_objects/schemas/task.ts +++ b/x-pack/plugins/task_manager/server/saved_objects/schemas/task.ts @@ -42,3 +42,7 @@ export const taskSchemaV1 = schema.object({ export const taskSchemaV2 = taskSchemaV1.extends({ partition: schema.maybe(schema.number()), }); + +export const taskSchemaV3 = taskSchemaV2.extends({ + priority: schema.maybe(schema.number()), +}); diff --git a/x-pack/plugins/task_manager/server/task.ts b/x-pack/plugins/task_manager/server/task.ts index bbe2935bdfc6d..23fcc2aa4bac7 100644 --- a/x-pack/plugins/task_manager/server/task.ts +++ b/x-pack/plugins/task_manager/server/task.ts @@ -351,6 +351,11 @@ export interface TaskInstance { * Used to break up tasks so each Kibana node can claim tasks on a subset of the partitions */ partition?: number; + + /* + * Optionally override the priority defined in the task type for this specific task instance + */ + priority?: TaskPriority; } /** From c02249e2c22dfa7fa3085fd63a42838d6fd2748b Mon Sep 17 00:00:00 2001 From: Ying Date: Wed, 13 Nov 2024 08:29:07 -0500 Subject: [PATCH 5/6] Cleanup --- .../alerts_client/lib/get_summarized_alerts_query.ts | 3 --- .../alerting/server/backfill_client/backfill_client.ts | 6 +++++- .../alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts | 5 +++-- .../task_runner/action_scheduler/lib/build_rule_url.ts | 5 +++-- .../alerting/server/task_runner/action_scheduler/types.ts | 6 +++++- .../alerting/server/task_runner/ad_hoc_task_runner.ts | 7 +++---- .../alerting/server/task_runner/transform_action_params.ts | 4 ++-- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts b/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts index fa115975948d6..ab3edece0becc 100644 --- a/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts +++ b/x-pack/plugins/alerting/server/alerts_client/lib/get_summarized_alerts_query.ts @@ -165,10 +165,7 @@ const getQueryByExecutionUuid = ({ }); } - console.log(`getQueryByExecutionUuid ${JSON.stringify(filter)}`); - if (alertsFilter) { - console.log(`alertsFilter ${JSON.stringify(alertsFilter)}`); filter.push(...generateAlertsFilterDSL(alertsFilter)); } diff --git a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts index 0c9c4d965827c..e3b7e3cdbce86 100644 --- a/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts +++ b/x-pack/plugins/alerting/server/backfill_client/backfill_client.ts @@ -135,9 +135,13 @@ export class BackfillClient { name: `rule`, type: RULE_SAVED_OBJECT_TYPE, }; + const allActions = [ + ...rule.actions, + ...(rule.systemActions ?? []), + ] as NormalizedAlertActionWithGeneratedValues[]; const { references: actionReferences, actions } = await denormalizeActions( () => Promise.resolve(actionsClient), - rule.actions as NormalizedAlertActionWithGeneratedValues[] + allActions ); adHocSOsToCreate.push({ type: AD_HOC_RUN_SAVED_OBJECT_TYPE, diff --git a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts index cfe21050c0363..ca4bbe91cd41f 100644 --- a/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts +++ b/x-pack/plugins/alerting/server/data/ad_hoc_run/types/ad_hoc_run.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { RawRule } from '../../../types'; import { RuleDomain } from '../../../application/rule/types'; import { AdHocRunStatus } from '../../../../common/constants'; @@ -23,7 +24,7 @@ export interface AdHocRunSchedule extends Record { // the backfill job was scheduled. if there are updates to the rule configuration // after the backfill is scheduled, they will not be reflected during the backfill run. type AdHocRunSORule = Pick< - RuleDomain, + RawRule, | 'name' | 'tags' | 'alertTypeId' @@ -44,7 +45,7 @@ type AdHocRunSORule = Pick< // This is the rule information after loaded from persistence with the // rule ID injected from the SO references array -type AdHocRunRule = AdHocRunSORule & Pick; +type AdHocRunRule = Omit & Pick; export interface AdHocRunSO extends Record { apiKeyId: string; diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/build_rule_url.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/build_rule_url.ts index 3df27a512c7f9..3f828709ee940 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/build_rule_url.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/lib/build_rule_url.ts @@ -6,16 +6,17 @@ */ import { Logger } from '@kbn/logging'; -import { RuleTypeParams, SanitizedRule } from '@kbn/alerting-types'; +import { RuleTypeParams } from '@kbn/alerting-types'; import { getRuleDetailsRoute, triggersActionsRoute } from '@kbn/rule-data-utils'; import { GetViewInAppRelativeUrlFn } from '../../../types'; +import { ActionSchedulerRule } from '../types'; interface BuildRuleUrlOpts { end?: number; getViewInAppRelativeUrl?: GetViewInAppRelativeUrlFn; kibanaBaseUrl: string | undefined; logger: Logger; - rule: SanitizedRule; + rule: ActionSchedulerRule; spaceId: string; start?: number; } diff --git a/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts b/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts index f1618db04e8f2..65d98a82261e9 100644 --- a/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts +++ b/x-pack/plugins/alerting/server/task_runner/action_scheduler/types.ts @@ -32,6 +32,10 @@ import { } from '../../lib/alerting_event_logger/alerting_event_logger'; import { RuleTaskInstance, TaskRunnerContext } from '../types'; +export type ActionSchedulerRule = Omit< + SanitizedRule, + 'executionStatus' +>; export interface ActionSchedulerOptions< Params extends RuleTypeParams, ExtractedParams extends RuleTypeParams, @@ -54,7 +58,7 @@ export interface ActionSchedulerOptions< >; logger: Logger; alertingEventLogger: PublicMethodsOf; - rule: Omit, 'executionStatus'>; + rule: ActionSchedulerRule; taskRunnerContext: TaskRunnerContext; taskInstance: RuleTaskInstance; ruleRunMetricsStore: RuleRunMetricsStore; diff --git a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts index 1e914d53ff13e..13c0edfec8707 100644 --- a/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/ad_hoc_task_runner.ts @@ -36,7 +36,7 @@ import { RuleTypeState, } from '../types'; import { TaskRunnerTimer, TaskRunnerTimerSpan } from './task_runner_timer'; -import { AdHocRun, AdHocRunSchedule } from '../data/ad_hoc_run/types'; +import { AdHocRun, AdHocRunSO, AdHocRunSchedule } from '../data/ad_hoc_run/types'; import { AD_HOC_RUN_SAVED_OBJECT_TYPE } from '../saved_objects'; import { RuleMonitoringService } from '../monitoring/rule_monitoring_service'; import { AdHocTaskRunningHandler } from './ad_hoc_task_running_handler'; @@ -53,7 +53,6 @@ import { import { RuleRunMetrics, RuleRunMetricsStore } from '../lib/rule_run_metrics_store'; import { getEsErrorMessage } from '../lib/errors'; import { Result, isOk, asOk, asErr } from '../lib/result_type'; -import { RawAdHocRunParams } from '../saved_objects/schemas/raw_ad_hoc_run_params/latest'; import { ActionScheduler } from './action_scheduler'; interface ConstructorParams { @@ -323,8 +322,8 @@ export class AdHocTaskRunner implements CancellableTask { let adHocRunData: AdHocRun; try { - const adHocRunSO: SavedObject = - await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( + const adHocRunSO: SavedObject = + await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( AD_HOC_RUN_SAVED_OBJECT_TYPE, adHocRunParamsId, { namespace } diff --git a/x-pack/plugins/alerting/server/task_runner/transform_action_params.ts b/x-pack/plugins/alerting/server/task_runner/transform_action_params.ts index fb934db0ffb7d..d4fc0907f8f9a 100644 --- a/x-pack/plugins/alerting/server/task_runner/transform_action_params.ts +++ b/x-pack/plugins/alerting/server/task_runner/transform_action_params.ts @@ -14,8 +14,8 @@ import { AlertInstanceState, AlertInstanceContext, RuleTypeParams, - SanitizedRule, } from '../types'; +import { ActionSchedulerRule } from './action_scheduler/types'; export interface TransformActionParamsOptions { actionsPlugin: ActionsPluginStartContract; @@ -146,7 +146,7 @@ export function transformSummaryActionParams({ kibanaBaseUrl, }: { alerts: SummarizedAlertsWithAll; - rule: SanitizedRule; + rule: ActionSchedulerRule; ruleTypeId: string; actionsPlugin: ActionsPluginStartContract; actionId: string; From e2169ce78f193fec3f1a28a1257770aa989bbf2a Mon Sep 17 00:00:00 2001 From: Ying Date: Thu, 14 Nov 2024 15:37:42 -0500 Subject: [PATCH 6/6] Fixes --- .../server/queries/mark_available_tasks_as_claimed.ts | 2 +- x-pack/plugins/task_manager/server/saved_objects/mappings.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts index e6c5feb28b5ac..e8ff3b5ae6c10 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts @@ -128,7 +128,7 @@ function getSortByPriority(definitions: TaskTypeDictionary): estypes.SortCombina // TODO: we could do this locally as well, but they may starve source: ` String taskType = doc['task.taskType'].value; - if (doc['task.priority'].value != null) { + if (doc['task.priority'].size() != 0) { return doc['task.priority'].value; } else if (params.priority_map.containsKey(taskType)) { return params.priority_map[taskType]; diff --git a/x-pack/plugins/task_manager/server/saved_objects/mappings.ts b/x-pack/plugins/task_manager/server/saved_objects/mappings.ts index 8ad641b56a58f..7ebd5091c7d47 100644 --- a/x-pack/plugins/task_manager/server/saved_objects/mappings.ts +++ b/x-pack/plugins/task_manager/server/saved_objects/mappings.ts @@ -65,6 +65,9 @@ export const taskMappings: SavedObjectsTypeMappingDefinition = { partition: { type: 'integer', }, + priority: { + type: 'integer', + }, }, };