Skip to content

Commit

Permalink
[Uptime] Fix ML job/rule edit error (#155212)
Browse files Browse the repository at this point in the history
## Summary

This PR attempts to fix an error encountered when editing an existing ML
rule in Uptime UI.


## Testing

You can reproduce this issue in latest `main` by creating a monitor (use
synthetics or uptime, doesn't matter) and open the legacy Uptime UI.

- Navigate to the monitor detail view, and create an ML alert (you might
need to engage trial license or other means to activate ML before you
can do this.)
- Create your alert
- Edit the alert (the screen should crash)
- Checkout this branch
- Try to edit again. You should be able to edit and update the rule
object multiple times without a crash
- See GIF below for further illustration on repro

**NOTE:** This GIF doesn't correspond exactly to what you can expect to
see on `main`, as I recorded it while I was investigating the problem.
You should expect the edit flyout not to load at all on `main`, rather
than seeing an error toast after attempting to POST like this GIF shows.
But the actual flow is exactly the same up to the point of editing the
detail flyout.


![20230418185724](https://user-images.githubusercontent.com/18429259/233210656-0a10fa93-f612-4764-afc0-028bc4289741.gif)


### Checklist

Delete any items that are not applicable to this PR.

- [ ] 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
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] 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))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] 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))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
  • Loading branch information
justinkambic and shahzad31 authored Apr 25, 2023
1 parent 283f285 commit 6e85cd2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export const ManageMLJobComponent = ({ hasMLJob, onEnableJob, onJobDelete }: Pro
onConfirm={() => {
deleteAnomalyAlert();
setIsConfirmAlertDeleteOpen(false);
setIsPopOverOpen(false);
}}
onCancel={() => {
setIsConfirmAlertDeleteOpen(false);
Expand Down
22 changes: 11 additions & 11 deletions x-pack/plugins/synthetics/public/legacy_uptime/state/api/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* 2.0.
*/

import type { ActionType, AsApiContract, Rule } from '@kbn/triggers-actions-ui-plugin/public';
import {
ActionType,
AsApiContract,
Rule,
transformRule,
} from '@kbn/triggers-actions-ui-plugin/public';
import { RuleTypeParams } from '@kbn/alerting-plugin/common';
import { MonitorStatusTranslations } from '../../../../common/rules/legacy_uptime/translations';
import { ActionConnector } from '../../../../common/rules/types';
Expand Down Expand Up @@ -143,17 +148,12 @@ export const fetchAnomalyAlertRecords = async ({
sort_order: 'asc',
};
const rawRules = await apiService.get<{
data: Array<Rule<NewAlertParams> & { rule_type_id: string }>;
data: Array<AsApiContract<Rule>>;
}>(API_URLS.RULES_FIND, data);
const monitorRule = rawRules.data.find(
(rule) => rule.params.monitorId === monitorId
) as Rule<NewAlertParams> & { rule_type_id: string };
if (monitorRule) {
return {
...monitorRule,
ruleTypeId: monitorRule.rule_type_id,
};
}
const monitorRule = rawRules.data.find((rule) => rule.params.monitorId === monitorId);
if (!monitorRule) return undefined;

return transformRule(monitorRule) as Rule<NewAlertParams>;
};

export const disableAlertById = async ({ alertId }: { alertId: string }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { RuleExecutionStatus } from '@kbn/alerting-plugin/common';
import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common';
import { Rule, RuleAction, ResolvedRule, RuleLastRun } from '../../../types';
import type { Rule, RuleAction, ResolvedRule, RuleLastRun } from '../../../types';

const transformAction: RewriteRequestCase<RuleAction> = ({
uuid,
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/triggers_actions_ui/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ export const getNotifyWhenOptions = async () => {
const { NOTIFY_WHEN_OPTIONS } = await import('./application/sections/rule_form/rule_notify_when');
return NOTIFY_WHEN_OPTIONS;
};

export { transformRule } from './application/lib/rule_api/common_transformations';

0 comments on commit 6e85cd2

Please sign in to comment.