Skip to content

Commit

Permalink
[8.x] [ResponseOps][Rules] Moved params of anomaly detection rules to…
Browse files Browse the repository at this point in the history
… a separate package (#205497) (#206057)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ResponseOps][Rules] Moved params of anomaly detection rules to a
separate package
(#205497)](#205497)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Antonio","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-09T11:58:40Z","message":"[ResponseOps][Rules]
Moved params of anomaly detection rules to a separate package
(#205497)\n\nConnected with #195189\r\n\r\n## Summary\r\n\r\n- Moved
params of anomaly detection rule type
to\r\n`/response-ops/rule_params/anomaly_detection/`\r\n- Moved params
of anomaly detection jobs health rule type
to\r\n`/response-ops/rule_params/anomaly_detection_jobs_health/`\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"5235234640277d6f3a2f864f57aebb970190761a","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-minor"],"number":205497,"url":"https://github.com/elastic/kibana/pull/205497","mergeCommit":{"message":"[ResponseOps][Rules]
Moved params of anomaly detection rules to a separate package
(#205497)\n\nConnected with #195189\r\n\r\n## Summary\r\n\r\n- Moved
params of anomaly detection rule type
to\r\n`/response-ops/rule_params/anomaly_detection/`\r\n- Moved params
of anomaly detection jobs health rule type
to\r\n`/response-ops/rule_params/anomaly_detection_jobs_health/`\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"5235234640277d6f3a2f864f57aebb970190761a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/205497","number":205497,"mergeCommit":{"message":"[ResponseOps][Rules]
Moved params of anomaly detection rules to a separate package
(#205497)\n\nConnected with #195189\r\n\r\n## Summary\r\n\r\n- Moved
params of anomaly detection rule type
to\r\n`/response-ops/rule_params/anomaly_detection/`\r\n- Moved params
of anomaly detection jobs health rule type
to\r\n`/response-ops/rule_params/anomaly_detection_jobs_health/`\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"5235234640277d6f3a2f864f57aebb970190761a"}}]}]
BACKPORT-->
  • Loading branch information
adcoelho authored Jan 9, 2025
1 parent 8030f6a commit 7b12c92
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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 })),
});
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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 }),
})
),
})
),
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>) =>
schema.string({
validate: (value) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"kbn_references": [
"@kbn/config-schema",
"@kbn/ml-anomaly-utils",
"@kbn/i18n",
"@kbn/alerting-comparators",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -56,51 +24,14 @@ export const mlAnomalyDetectionAlertPreviewRequest = schema.object({
sampleSize: schema.number({ defaultValue: ALERT_PREVIEW_SAMPLE_SIZE, min: 0 }),
});

export type MlAnomalyDetectionAlertParams = TypeOf<typeof mlAnomalyDetectionAlertParams>;
export type MlAnomalyDetectionAlertParams = TypeOf<typeof mlAnomalyDetectionAlertParamsSchema>;

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'];
Expand Down
1 change: 1 addition & 0 deletions x-pack/platform/plugins/shared/ml/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,6 @@
"@kbn/core-saved-objects-api-server",
"@kbn/core-ui-settings-server",
"@kbn/core-security-server",
"@kbn/response-ops-rule-params",
]
}

0 comments on commit 7b12c92

Please sign in to comment.