diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/index.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/index.ts new file mode 100644 index 0000000000000..d5348359b22a4 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/index.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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { mlAnomalyDetectionAlertParamsSchema } from './latest'; +export { mlAnomalyDetectionAlertParamsSchema as mlAnomalyDetectionAlertParamsSchemaV1 } from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/latest.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/latest.ts new file mode 100644 index 0000000000000..f278309c22b03 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/latest.ts @@ -0,0 +1,10 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/v1.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/v1.ts new file mode 100644 index 0000000000000..982f730fc2525 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection/v1.ts @@ -0,0 +1,30 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema } from '@kbn/config-schema'; +import { ML_ANOMALY_RESULT_TYPE } from '@kbn/ml-anomaly-utils'; + +import { jobsSelectionSchema } from '../common/utils'; + +export const mlAnomalyDetectionAlertParamsSchema = schema.object({ + jobSelection: jobsSelectionSchema, + /** Anomaly score threshold */ + severity: schema.number({ min: 0, max: 100 }), + /** Result type to alert upon */ + resultType: schema.oneOf([ + schema.literal(ML_ANOMALY_RESULT_TYPE.RECORD), + schema.literal(ML_ANOMALY_RESULT_TYPE.BUCKET), + schema.literal(ML_ANOMALY_RESULT_TYPE.INFLUENCER), + ]), + includeInterim: schema.boolean({ defaultValue: true }), + /** User's override for the lookback interval */ + lookbackInterval: schema.nullable(schema.string()), + /** User's override for the top N buckets */ + topNBuckets: schema.nullable(schema.number({ min: 1 })), +}); diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/index.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/index.ts new file mode 100644 index 0000000000000..7ea30e9ae8385 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/index.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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { anomalyDetectionJobsHealthRuleParamsSchema } from './latest'; +export { anomalyDetectionJobsHealthRuleParamsSchema as anomalyDetectionJobsHealthRuleParamsSchemaV1 } from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/latest.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/latest.ts new file mode 100644 index 0000000000000..f278309c22b03 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/latest.ts @@ -0,0 +1,10 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export * from './v1'; diff --git a/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/v1.ts b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/v1.ts new file mode 100644 index 0000000000000..b3e4f0e7fbbc6 --- /dev/null +++ b/src/platform/packages/shared/response-ops/rule_params/anomaly_detection_jobs_health/v1.ts @@ -0,0 +1,48 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema } from '@kbn/config-schema'; +import { jobsSelectionSchema } from '../common/utils'; + +export const anomalyDetectionJobsHealthRuleParamsSchema = schema.object({ + includeJobs: jobsSelectionSchema, + excludeJobs: schema.nullable(jobsSelectionSchema), + testsConfig: schema.nullable( + schema.object({ + datafeed: schema.nullable( + schema.object({ + enabled: schema.boolean({ defaultValue: true }), + }) + ), + mml: schema.nullable( + schema.object({ + enabled: schema.boolean({ defaultValue: true }), + }) + ), + delayedData: schema.nullable( + schema.object({ + enabled: schema.boolean({ defaultValue: true }), + docsCount: schema.nullable(schema.number({ min: 1 })), + timeInterval: schema.nullable(schema.string()), + }) + ), + behindRealtime: schema.nullable( + schema.object({ + enabled: schema.boolean({ defaultValue: true }), + timeInterval: schema.nullable(schema.string()), + }) + ), + errorMessages: schema.nullable( + schema.object({ + enabled: schema.boolean({ defaultValue: true }), + }) + ), + }) + ), +}); diff --git a/src/platform/packages/shared/response-ops/rule_params/common/utils.ts b/src/platform/packages/shared/response-ops/rule_params/common/utils.ts index 521621620dc29..074085c61969b 100644 --- a/src/platform/packages/shared/response-ops/rule_params/common/utils.ts +++ b/src/platform/packages/shared/response-ops/rule_params/common/utils.ts @@ -8,8 +8,25 @@ */ import { schema } from '@kbn/config-schema'; +import { i18n } from '@kbn/i18n'; import { isEmpty } from 'lodash'; +export const jobsSelectionSchema = schema.object( + { + jobIds: schema.arrayOf(schema.string(), { defaultValue: [] }), + groupIds: schema.arrayOf(schema.string(), { defaultValue: [] }), + }, + { + validate: (v) => { + if (!v.jobIds?.length && !v.groupIds?.length) { + return i18n.translate('xpack.ml.alertTypes.anomalyDetection.jobSelection.errorMessage', { + defaultMessage: 'Job selection is required', + }); + } + }, + } +); + export const oneOfLiterals = (arrayOfLiterals: Readonly) => schema.string({ validate: (value) => diff --git a/src/platform/packages/shared/response-ops/rule_params/tsconfig.json b/src/platform/packages/shared/response-ops/rule_params/tsconfig.json index c8de98d29c211..4d996cbca3a7a 100644 --- a/src/platform/packages/shared/response-ops/rule_params/tsconfig.json +++ b/src/platform/packages/shared/response-ops/rule_params/tsconfig.json @@ -9,6 +9,7 @@ "kbn_references": [ "@kbn/config-schema", "@kbn/ml-anomaly-utils", + "@kbn/i18n", "@kbn/alerting-comparators", ] } diff --git a/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_anomaly_detection_alert_type.ts b/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_anomaly_detection_alert_type.ts index deb7c9d2cc657..46ab36f5a1d42 100644 --- a/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_anomaly_detection_alert_type.ts +++ b/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_anomaly_detection_alert_type.ts @@ -22,6 +22,7 @@ import { AlertsClientError } from '@kbn/alerting-plugin/server'; import { ALERT_REASON, ALERT_URL } from '@kbn/rule-data-utils'; import type { MlAnomalyDetectionAlert } from '@kbn/alerts-as-data-utils'; import { ES_FIELD_TYPES } from '@kbn/field-types'; +import { mlAnomalyDetectionAlertParamsSchema } from '@kbn/response-ops-rule-params/anomaly_detection'; import { ALERT_ANOMALY_DETECTION_JOB_ID, ALERT_ANOMALY_IS_INTERIM, @@ -33,10 +34,7 @@ import { } from '../../../common/constants/alerts'; import { PLUGIN_ID } from '../../../common/constants/app'; import { MINIMUM_FULL_LICENSE } from '../../../common/license'; -import { - type MlAnomalyDetectionAlertParams, - mlAnomalyDetectionAlertParams, -} from '../../routes/schemas/alerting_schema'; +import type { MlAnomalyDetectionAlertParams } from '../../routes/schemas/alerting_schema'; import type { RegisterAlertParams } from './register_ml_alerts'; import type { InfluencerAnomalyAlertDoc } from '../../../common/types/alerts'; import { type RecordAnomalyAlertDoc } from '../../../common/types/alerts'; @@ -177,12 +175,12 @@ export function registerAnomalyDetectionAlertType({ actionGroups: [THRESHOLD_MET_GROUP], defaultActionGroupId: ANOMALY_SCORE_MATCH_GROUP_ID, validate: { - params: mlAnomalyDetectionAlertParams, + params: mlAnomalyDetectionAlertParamsSchema, }, schemas: { params: { type: 'config-schema', - schema: mlAnomalyDetectionAlertParams, + schema: mlAnomalyDetectionAlertParamsSchema, }, }, actionVariables: { diff --git a/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_jobs_monitoring_rule_type.ts b/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_jobs_monitoring_rule_type.ts index 7d50f99a42128..f499774945876 100644 --- a/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_jobs_monitoring_rule_type.ts +++ b/x-pack/platform/plugins/shared/ml/server/lib/alerts/register_jobs_monitoring_rule_type.ts @@ -27,6 +27,8 @@ import { AlertsClientError } from '@kbn/alerting-plugin/server'; import type { MlAnomalyDetectionHealthAlert } from '@kbn/alerts-as-data-utils'; import type { ALERT_REASON } from '@kbn/rule-data-utils'; import { ES_FIELD_TYPES } from '@kbn/field-types'; +import { anomalyDetectionJobsHealthRuleParamsSchema } from '@kbn/response-ops-rule-params/anomaly_detection_jobs_health'; + import { ALERT_DATAFEED_RESULTS, ALERT_DELAYED_DATA_RESULTS, @@ -36,10 +38,7 @@ import { } from '../../../common/constants/alerts'; import { PLUGIN_ID } from '../../../common/constants/app'; import { MINIMUM_FULL_LICENSE } from '../../../common/license'; -import { - anomalyDetectionJobsHealthRuleParams, - type AnomalyDetectionJobsHealthRuleParams, -} from '../../routes/schemas/alerting_schema'; +import type { AnomalyDetectionJobsHealthRuleParams } from '../../routes/schemas/alerting_schema'; import type { RegisterAlertParams } from './register_ml_alerts'; import type { JobMessage } from '../../../common/types/audit_message'; @@ -218,12 +217,12 @@ export function registerJobsMonitoringRuleType({ actionGroups: [REALTIME_ISSUE_DETECTED], defaultActionGroupId: ANOMALY_DETECTION_JOB_REALTIME_ISSUE, validate: { - params: anomalyDetectionJobsHealthRuleParams, + params: anomalyDetectionJobsHealthRuleParamsSchema, }, schemas: { params: { type: 'config-schema', - schema: anomalyDetectionJobsHealthRuleParams, + schema: anomalyDetectionJobsHealthRuleParamsSchema, }, }, actionVariables: { diff --git a/x-pack/platform/plugins/shared/ml/server/routes/schemas/alerting_schema.ts b/x-pack/platform/plugins/shared/ml/server/routes/schemas/alerting_schema.ts index 3137f8227ed5c..53ffdba0a64a0 100644 --- a/x-pack/platform/plugins/shared/ml/server/routes/schemas/alerting_schema.ts +++ b/x-pack/platform/plugins/shared/ml/server/routes/schemas/alerting_schema.ts @@ -7,45 +7,13 @@ import type { TypeOf } from '@kbn/config-schema'; import { schema } from '@kbn/config-schema'; -import { i18n } from '@kbn/i18n'; -import { ML_ANOMALY_RESULT_TYPE } from '@kbn/ml-anomaly-utils'; -import { ALERT_PREVIEW_SAMPLE_SIZE } from '../../../common/constants/alerts'; - -const jobsSelectionSchema = schema.object( - { - jobIds: schema.arrayOf(schema.string(), { defaultValue: [] }), - groupIds: schema.arrayOf(schema.string(), { defaultValue: [] }), - }, - { - validate: (v) => { - if (!v.jobIds?.length && !v.groupIds?.length) { - return i18n.translate('xpack.ml.alertTypes.anomalyDetection.jobSelection.errorMessage', { - defaultMessage: 'Job selection is required', - }); - } - }, - } -); +import { mlAnomalyDetectionAlertParamsSchema } from '@kbn/response-ops-rule-params/anomaly_detection'; +import type { anomalyDetectionJobsHealthRuleParamsSchema } from '@kbn/response-ops-rule-params/anomaly_detection_jobs_health'; -export const mlAnomalyDetectionAlertParams = schema.object({ - jobSelection: jobsSelectionSchema, - /** Anomaly score threshold */ - severity: schema.number({ min: 0, max: 100 }), - /** Result type to alert upon */ - resultType: schema.oneOf([ - schema.literal(ML_ANOMALY_RESULT_TYPE.RECORD), - schema.literal(ML_ANOMALY_RESULT_TYPE.BUCKET), - schema.literal(ML_ANOMALY_RESULT_TYPE.INFLUENCER), - ]), - includeInterim: schema.boolean({ defaultValue: true }), - /** User's override for the lookback interval */ - lookbackInterval: schema.nullable(schema.string()), - /** User's override for the top N buckets */ - topNBuckets: schema.nullable(schema.number({ min: 1 })), -}); +import { ALERT_PREVIEW_SAMPLE_SIZE } from '../../../common/constants/alerts'; export const mlAnomalyDetectionAlertPreviewRequest = schema.object({ - alertParams: mlAnomalyDetectionAlertParams, + alertParams: mlAnomalyDetectionAlertParamsSchema, /** * Relative time range to look back from now, e.g. 1y, 8m, 15d */ @@ -56,51 +24,14 @@ export const mlAnomalyDetectionAlertPreviewRequest = schema.object({ sampleSize: schema.number({ defaultValue: ALERT_PREVIEW_SAMPLE_SIZE, min: 0 }), }); -export type MlAnomalyDetectionAlertParams = TypeOf; +export type MlAnomalyDetectionAlertParams = TypeOf; export type MlAnomalyDetectionAlertPreviewRequest = TypeOf< typeof mlAnomalyDetectionAlertPreviewRequest >; -export const anomalyDetectionJobsHealthRuleParams = schema.object({ - includeJobs: jobsSelectionSchema, - excludeJobs: schema.nullable(jobsSelectionSchema), - testsConfig: schema.nullable( - schema.object({ - datafeed: schema.nullable( - schema.object({ - enabled: schema.boolean({ defaultValue: true }), - }) - ), - mml: schema.nullable( - schema.object({ - enabled: schema.boolean({ defaultValue: true }), - }) - ), - delayedData: schema.nullable( - schema.object({ - enabled: schema.boolean({ defaultValue: true }), - docsCount: schema.nullable(schema.number({ min: 1 })), - timeInterval: schema.nullable(schema.string()), - }) - ), - behindRealtime: schema.nullable( - schema.object({ - enabled: schema.boolean({ defaultValue: true }), - timeInterval: schema.nullable(schema.string()), - }) - ), - errorMessages: schema.nullable( - schema.object({ - enabled: schema.boolean({ defaultValue: true }), - }) - ), - }) - ), -}); - export type AnomalyDetectionJobsHealthRuleParams = TypeOf< - typeof anomalyDetectionJobsHealthRuleParams + typeof anomalyDetectionJobsHealthRuleParamsSchema >; export type TestsConfig = AnomalyDetectionJobsHealthRuleParams['testsConfig']; diff --git a/x-pack/platform/plugins/shared/ml/tsconfig.json b/x-pack/platform/plugins/shared/ml/tsconfig.json index b65618569ec71..3ac0057d62fa5 100644 --- a/x-pack/platform/plugins/shared/ml/tsconfig.json +++ b/x-pack/platform/plugins/shared/ml/tsconfig.json @@ -137,5 +137,6 @@ "@kbn/core-saved-objects-api-server", "@kbn/core-ui-settings-server", "@kbn/core-security-server", + "@kbn/response-ops-rule-params", ] }