Skip to content

Commit

Permalink
[ResponseOps][Rules] Moved params of anomaly detection rules to a sep…
Browse files Browse the repository at this point in the history
…arate package (elastic#205497)

Connected with elastic#195189

## Summary

- Moved params of anomaly detection rule type to
`/response-ops/rule_params/anomaly_detection/`
- Moved params of anomaly detection jobs health rule type to
`/response-ops/rule_params/anomaly_detection_jobs_health/`

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
2 people authored and Zacqary committed Jan 9, 2025
1 parent c83cfab commit b2828c1
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 @@ -136,5 +136,6 @@
"@kbn/core-ui-settings-server",
"@kbn/core-security-server",
"@kbn/charts-theme",
"@kbn/response-ops-rule-params",
]
}

0 comments on commit b2828c1

Please sign in to comment.