Skip to content

Commit 3caff53

Browse files
authored
fix: Noisy presets do not play sound (#4979)
1 parent d3be911 commit 3caff53

4 files changed

Lines changed: 127 additions & 15 deletions

File tree

keep-ui/features/presets/create-or-update-preset/ui/preset-controls.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const PresetControls: React.FC<PresetControlsProps> = ({
4242

4343
<div className="flex items-center gap-2">
4444
<Switch
45+
data-testid="is-noisy-switch"
4546
id="noisy"
4647
checked={isNoisy}
4748
onChange={() => setIsNoisy(!isNoisy)}

keep-ui/features/presets/custom-preset-links/ui/CustomPresetAlertLinks.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import {
1616
import { SortableContext, useSortable } from "@dnd-kit/sortable";
1717
import { CSS } from "@dnd-kit/utilities";
1818
import { AiOutlineSound } from "react-icons/ai";
19-
// Using dynamic import to avoid hydration issues with react-player
20-
import dynamic from "next/dynamic";
21-
const ReactPlayer = dynamic(() => import("react-player"), { ssr: false });
2219
// import css
2320
import "./CustomPresetAlertLink.css";
2421
import clsx from "clsx";
@@ -27,6 +24,7 @@ import { usePresetActions } from "@/entities/presets/model/usePresetActions";
2724
import { usePresetPolling } from "@/entities/presets/model/usePresetPolling";
2825
import { usePresetAlertsCount } from "../model/usePresetAlertsCount";
2926
import { FireIcon } from "@heroicons/react/24/outline";
27+
import { PresetsNoise } from "./PresetsNoise";
3028

3129
type AlertPresetLinkProps = {
3230
preset: Preset;
@@ -214,18 +212,7 @@ export const CustomPresetAlertLinks = ({
214212
/>
215213
))}
216214
</SortableContext>
217-
{/* React Player for playing alert sound */}
218-
<ReactPlayer
219-
// TODO: cache the audio file fiercely
220-
url="/music/alert.mp3"
221-
playing={anyNoisyNow}
222-
volume={0.5}
223-
loop={true}
224-
width="0"
225-
height="0"
226-
playsinline
227-
className="absolute -z-10"
228-
/>
215+
<PresetsNoise presets={presets}></PresetsNoise>
229216
</DndContext>
230217
);
231218
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Preset } from "@/entities/presets/model";
2+
import { useMemo } from "react";
3+
import { useApi } from "@/shared/lib/hooks/useApi";
4+
import useSWR from "swr";
5+
import { AlertsQuery } from "@/entities/alerts/model";
6+
// Using dynamic import to avoid hydration issues with react-player
7+
import dynamic from "next/dynamic";
8+
import clsx from "clsx";
9+
const ReactPlayer = dynamic(() => import("react-player"), { ssr: false });
10+
11+
interface PresetsNoiseProps {
12+
presets: Preset[];
13+
}
14+
15+
export const PresetsNoise = ({ presets }: PresetsNoiseProps) => {
16+
const api = useApi();
17+
const noisyPresets = useMemo(
18+
() => presets?.filter((preset) => preset.is_noisy),
19+
[presets]
20+
);
21+
22+
const { data: shouldDoNoise } = useSWR(
23+
() =>
24+
api.isReady() && noisyPresets
25+
? noisyPresets.map((noisyPreset) => noisyPreset.id)
26+
: null,
27+
async () => {
28+
let shouldDoNoise = false;
29+
30+
// Iterate through noisy presets and find first that has an Alert that should trigger noise
31+
for (let noisyPreset of noisyPresets) {
32+
const noisyAlertsCelRules = [
33+
"status == 'firing' && deleted == false && dismissed == false",
34+
noisyPreset.options.find((opt) => opt.label == "CEL")?.value,
35+
];
36+
const query: AlertsQuery = {
37+
cel: noisyAlertsCelRules.map((cel) => `(${cel})`).join(" && "),
38+
limit: 0,
39+
offset: 0,
40+
};
41+
42+
const { count: matchingAlertsCount } = await api.post(
43+
"/alerts/query",
44+
query
45+
);
46+
shouldDoNoise = !!matchingAlertsCount;
47+
48+
if (shouldDoNoise) {
49+
break;
50+
}
51+
}
52+
53+
return shouldDoNoise;
54+
}
55+
);
56+
57+
/* React Player for playing alert sound */
58+
return (
59+
<div
60+
data-testid="noisy-presets-audio-player"
61+
className={clsx("absolute -z-10", {
62+
playing: shouldDoNoise,
63+
})}
64+
>
65+
<ReactPlayer
66+
// TODO: cache the audio file fiercely
67+
url="/music/alert.mp3"
68+
playing={shouldDoNoise}
69+
volume={0.5}
70+
loop={true}
71+
width="0"
72+
height="0"
73+
playsinline
74+
className="absolute -z-10"
75+
/>
76+
</div>
77+
);
78+
};

tests/e2e_tests/incidents_alerts_tests/test_filtering_sort_search_on_alerts.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,52 @@ def filter_lambda(alert):
753753
alert_property_name,
754754
None,
755755
)
756+
# check that alerts noise is not playing
757+
expect(
758+
browser.locator("[data-testid='noisy-presets-audio-player'].playing")
759+
).to_have_count(0)
760+
except Exception:
761+
save_failure_artifacts(browser, log_entries=[])
762+
raise
763+
764+
765+
def test_adding_new_noisy_preset(
766+
browser: Page,
767+
setup_test_data,
768+
setup_page_logging,
769+
failure_artifacts,
770+
):
771+
try:
772+
current_alerts = query_alerts(cell_query="", limit=1000)["results"]
773+
init_test(browser, current_alerts, max_retries=3)
774+
775+
# Give the page a moment to process redirects
776+
browser.wait_for_timeout(500)
777+
778+
# Wait for navigation to complete to either signin or providers page
779+
# (since we might get redirected automatically)
780+
browser.wait_for_load_state("networkidle")
781+
cel_input_locator = browser.locator(".alerts-cel-input")
782+
cel_input_locator.click()
783+
browser.keyboard.type("name.contains('high')")
784+
browser.keyboard.press("Enter")
785+
browser.wait_for_timeout(500)
786+
browser.locator("[data-testid='save-preset-button']").click()
787+
preset_form_locator = browser.locator("[data-testid='preset-form']")
788+
preset_form_locator.locator("[data-testid='preset-name-input']").fill(
789+
"Test noisy preset"
790+
)
791+
preset_form_locator.locator("[data-testid='is-noisy-switch']").click()
792+
preset_form_locator.locator("[data-testid='save-preset-button']").click()
793+
expect(
794+
browser.locator("[data-testid='noisy-presets-audio-player'].playing")
795+
).to_have_count(1)
796+
browser.reload()
797+
798+
# check that it's still playing after reloading
799+
expect(
800+
browser.locator("[data-testid='noisy-presets-audio-player'].playing")
801+
).to_have_count(1)
756802
except Exception:
757803
save_failure_artifacts(browser, log_entries=[])
758804
raise

0 commit comments

Comments
 (0)