-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Link to Anomaly detection job creation from the alerting rule fo…
…rm (#174016) ## Summary Closes #173654 Adds an option to create a new anomaly detection job from the job selector control. <img width="614" alt="image" src="https://github.com/elastic/kibana/assets/5236598/1dc19cb9-b7e5-471c-9dec-3018103efb69"> ### Checklist - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [x] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [x] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers)
- Loading branch information
Showing
11 changed files
with
278 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/ml/public/alerting/anomaly_detection_rule/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { registerAnomalyDetectionRule } from './register_anomaly_detection_rule'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
x-pack/plugins/ml/public/alerting/anomaly_detection_rule/register_anomaly_detection_rule.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* 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 { | ||
type RuleTypeParamsExpressionProps, | ||
type TriggersAndActionsUIPublicPluginSetup, | ||
} from '@kbn/triggers-actions-ui-plugin/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import React, { lazy } from 'react'; | ||
import type { MlCapabilities } from '../../../common/types/capabilities'; | ||
import type { MlCoreSetup } from '../../plugin'; | ||
import { ML_ALERT_TYPES } from '../../../common'; | ||
import type { MlAnomalyDetectionAlertParams } from '../../../common/types/alerts'; | ||
import { validateLookbackInterval, validateTopNBucket } from '../validators'; | ||
|
||
export function registerAnomalyDetectionRule( | ||
triggersActionsUi: TriggersAndActionsUIPublicPluginSetup, | ||
getStartServices: MlCoreSetup['getStartServices'], | ||
mlCapabilities: MlCapabilities | ||
) { | ||
triggersActionsUi.ruleTypeRegistry.register({ | ||
id: ML_ALERT_TYPES.ANOMALY_DETECTION, | ||
description: i18n.translate('xpack.ml.alertTypes.anomalyDetection.description', { | ||
defaultMessage: 'Alert when anomaly detection jobs results match the condition.', | ||
}), | ||
iconClass: 'bell', | ||
documentationUrl(docLinks) { | ||
return docLinks.links.ml.alertingRules; | ||
}, | ||
ruleParamsExpression: (props: RuleTypeParamsExpressionProps<MlAnomalyDetectionAlertParams>) => { | ||
const MlAlertTrigger = lazy(() => import('./ml_anomaly_alert_trigger')); | ||
return ( | ||
<MlAlertTrigger | ||
{...props} | ||
getStartServices={getStartServices} | ||
mlCapabilities={mlCapabilities} | ||
/> | ||
); | ||
}, | ||
validate: (ruleParams: MlAnomalyDetectionAlertParams) => { | ||
const validationResult = { | ||
errors: { | ||
jobSelection: new Array<string>(), | ||
severity: new Array<string>(), | ||
resultType: new Array<string>(), | ||
topNBuckets: new Array<string>(), | ||
lookbackInterval: new Array<string>(), | ||
} as Record<keyof MlAnomalyDetectionAlertParams, string[]>, | ||
}; | ||
|
||
if (!ruleParams.jobSelection?.jobIds?.length && !ruleParams.jobSelection?.groupIds?.length) { | ||
validationResult.errors.jobSelection.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.jobSelection.errorMessage', { | ||
defaultMessage: 'Job selection is required', | ||
}) | ||
); | ||
} | ||
|
||
// Since 7.13 we support single job selection only | ||
if ( | ||
(Array.isArray(ruleParams.jobSelection?.groupIds) && | ||
ruleParams.jobSelection?.groupIds.length > 0) || | ||
(Array.isArray(ruleParams.jobSelection?.jobIds) && | ||
ruleParams.jobSelection?.jobIds.length > 1) | ||
) { | ||
validationResult.errors.jobSelection.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.singleJobSelection.errorMessage', { | ||
defaultMessage: 'Only one job per rule is allowed', | ||
}) | ||
); | ||
} | ||
|
||
if (ruleParams.severity === undefined) { | ||
validationResult.errors.severity.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.severity.errorMessage', { | ||
defaultMessage: 'Anomaly severity is required', | ||
}) | ||
); | ||
} | ||
|
||
if (ruleParams.resultType === undefined) { | ||
validationResult.errors.resultType.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.resultType.errorMessage', { | ||
defaultMessage: 'Result type is required', | ||
}) | ||
); | ||
} | ||
|
||
if (!!ruleParams.lookbackInterval && validateLookbackInterval(ruleParams.lookbackInterval)) { | ||
validationResult.errors.lookbackInterval.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.lookbackInterval.errorMessage', { | ||
defaultMessage: 'Lookback interval is invalid', | ||
}) | ||
); | ||
} | ||
|
||
if ( | ||
typeof ruleParams.topNBuckets === 'number' && | ||
validateTopNBucket(ruleParams.topNBuckets) | ||
) { | ||
validationResult.errors.topNBuckets.push( | ||
i18n.translate('xpack.ml.alertTypes.anomalyDetection.topNBuckets.errorMessage', { | ||
defaultMessage: 'Number of buckets is invalid', | ||
}) | ||
); | ||
} | ||
|
||
return validationResult; | ||
}, | ||
requiresAppContext: false, | ||
defaultActionMessage: i18n.translate( | ||
'xpack.ml.alertTypes.anomalyDetection.defaultActionMessage', | ||
{ | ||
defaultMessage: `[\\{\\{rule.name\\}\\}] Elastic Stack Machine Learning Alert: | ||
- Job IDs: \\{\\{context.jobIds\\}\\} | ||
- Time: \\{\\{context.timestampIso8601\\}\\} | ||
- Anomaly score: \\{\\{context.score\\}\\} | ||
\\{\\{context.message\\}\\} | ||
\\{\\{#context.topInfluencers.length\\}\\} | ||
Top influencers: | ||
\\{\\{#context.topInfluencers\\}\\} | ||
\\{\\{influencer_field_name\\}\\} = \\{\\{influencer_field_value\\}\\} [\\{\\{score\\}\\}] | ||
\\{\\{/context.topInfluencers\\}\\} | ||
\\{\\{/context.topInfluencers.length\\}\\} | ||
\\{\\{#context.topRecords.length\\}\\} | ||
Top records: | ||
\\{\\{#context.topRecords\\}\\} | ||
\\{\\{function\\}\\}(\\{\\{field_name\\}\\}) \\{\\{by_field_value\\}\\}\\{\\{over_field_value\\}\\}\\{\\{partition_field_value\\}\\} [\\{\\{score\\}\\}]. Typical: \\{\\{typical\\}\\}, Actual: \\{\\{actual\\}\\} | ||
\\{\\{/context.topRecords\\}\\} | ||
\\{\\{/context.topRecords.length\\}\\} | ||
\\{\\{! Replace kibanaBaseUrl if not configured in Kibana \\}\\} | ||
[Open in Anomaly Explorer](\\{\\{\\{kibanaBaseUrl\\}\\}\\}\\{\\{\\{context.anomalyExplorerUrl\\}\\}\\}) | ||
`, | ||
} | ||
), | ||
}); | ||
} |
File renamed without changes.
Oops, something went wrong.