[ML] Add new custom rule action to force time shift (#188710)

This PR follows up https://github.com/elastic/elasticsearch/pull/110974
and extends the schema for custom rules.

A valid schema looks like this:
```http
POST _ml/anomaly_detectors/my_job/_update
{
	"detectors": {
		"detector_index": 0,
		"custom_rules": [
			// update the detector with a custom rule that forces a time shift of 1 hour back starting now
			{
				"actions": [
					"force_time_shift"
				],
				"params": {
					"force_time_shift": {
"time_shift_amount": 3600}
					}
				},
				"conditions": [{
					"applies_to": "time",
					"operator": "gt",
					"value": "now"
				},
{
					"applies_to": "time",
					"operator": "lt",
					"value": "now+bucket_span"
				},

]
			},
			...
```

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Valeriy Khakhutskyy 2024-07-26 09:04:43 +02:00 committed by GitHub
parent 64b45e0884
commit bd843dda3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 1 deletions

View file

@ -15,6 +15,7 @@
export enum ML_DETECTOR_RULE_ACTION {
SKIP_MODEL_UPDATE = 'skip_model_update',
SKIP_RESULT = 'skip_result',
FORCE_TIME_SHIFT = 'force_time_shift',
}
/**
@ -44,6 +45,20 @@ export enum ML_DETECTOR_RULE_OPERATOR {
GREATER_THAN_OR_EQUAL = 'gte',
}
/**
* Enum ML_DETECTOR_RULE_PARAMS
*/
export enum ML_DETECTOR_RULE_PARAMS {
FORCE_TIME_SHIFT = 'force_time_shift',
}
/**
* Enum ML_DETECTOR_RULE_PARAMS_FORCE_TIME_SHIFT
*/
export enum ML_DETECTOR_RULE_PARAMS_FORCE_TIME_SHIFT {
TIME_SHIFT_AMOUNT = 'time_shift_amount',
}
/**
* List of detector functions which don't support rules with numeric conditions.
*/

View file

@ -58,6 +58,8 @@ export {
ML_DETECTOR_RULE_CONDITIONS_NOT_SUPPORTED_FUNCTIONS,
ML_DETECTOR_RULE_FILTER_TYPE,
ML_DETECTOR_RULE_OPERATOR,
ML_DETECTOR_RULE_PARAMS,
ML_DETECTOR_RULE_PARAMS_FORCE_TIME_SHIFT,
} from './detector_rule';
export type { InfluencersFilterQuery } from './es_client';

View file

@ -25,8 +25,17 @@ export interface DetectorRuleCondition {
value: number;
}
export interface DetectorRuleParamsForceTimeShift {
time_shift_amount: number;
}
export interface DetectorRuleParams {
force_time_shift?: DetectorRuleParamsForceTimeShift;
}
export interface DetectorRule {
actions: ML_DETECTOR_RULE_ACTION[];
scope?: DetectorRuleScope;
conditions?: DetectorRuleCondition[];
params?: DetectorRuleParams[];
}

View file

@ -24,6 +24,7 @@ import {
import { ML_DETECTOR_RULE_ACTION } from '@kbn/ml-anomaly-utils';
import { FormattedMessage } from '@kbn/i18n-react';
// TODO: add onForceTimeShift action
export function ActionsSection({ actions, onSkipResultChange, onSkipModelUpdateChange }) {
return (
<React.Fragment>

View file

@ -12,10 +12,15 @@ const customRulesSchema = schema.maybe(
schema.arrayOf(
schema.object({
actions: schema.arrayOf(
schema.oneOf([schema.literal('skip_result'), schema.literal('skip_model_update')])
schema.oneOf([
schema.literal('skip_result'),
schema.literal('skip_model_update'),
schema.literal('force_time_shift'),
])
),
conditions: schema.maybe(schema.arrayOf(schema.any())),
scope: schema.maybe(schema.any()),
params: schema.maybe(schema.any()),
})
)
);