From cf50d4ae19634f04c4484aed1eb2815c40d907d4 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Fri, 4 Oct 2024 16:57:55 -0700 Subject: [PATCH] AlertManager: reset duration when re-adding an event (#33478) * fix alert duration when re-adding an alert before it's ended (personality) * clean up * messy test * clean up test * Revert "clean up test" This reverts commit e7c0f80cb3d0061558920cf2baf37c6ea6935c81. * better name * debug * Revert "debug" This reverts commit da8d561445f0f0d142cec99c583dda798973544c. * Reapply "clean up test" This reverts commit a7dba540f78d1c32317e5f89e83de5aac1136c38. * update refs --- selfdrive/selfdrived/alertmanager.py | 6 +++++- .../selfdrived/tests/test_alertmanager.py | 21 ++++++++++++++++++- selfdrive/test/process_replay/ref_commit | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/selfdrive/selfdrived/alertmanager.py b/selfdrive/selfdrived/alertmanager.py index 4a406d3962cbf4..ac1006ff258c01 100644 --- a/selfdrive/selfdrived/alertmanager.py +++ b/selfdrive/selfdrived/alertmanager.py @@ -27,10 +27,13 @@ class AlertEntry: alert: Alert | None = None start_frame: int = -1 end_frame: int = -1 + added_frame: int = -1 def active(self, frame: int) -> bool: return frame <= self.end_frame + def just_added(self, frame: int) -> bool: + return self.active(frame) and frame == (self.added_frame + 1) class AlertManager: def __init__(self): @@ -41,10 +44,11 @@ def add_many(self, frame: int, alerts: list[Alert]) -> None: for alert in alerts: entry = self.alerts[alert.alert_type] entry.alert = alert - if not entry.active(frame): + if not entry.just_added(frame): entry.start_frame = frame min_end_frame = entry.start_frame + alert.duration entry.end_frame = max(frame + 1, min_end_frame) + entry.added_frame = frame def process_alerts(self, frame: int, clear_event_types: set): ae = AlertEntry() diff --git a/selfdrive/selfdrived/tests/test_alertmanager.py b/selfdrive/selfdrived/tests/test_alertmanager.py index b75a4d1cbe3b3b..030b7d4515c2f2 100644 --- a/selfdrive/selfdrived/tests/test_alertmanager.py +++ b/selfdrive/selfdrived/tests/test_alertmanager.py @@ -32,8 +32,27 @@ def test_duration(self): for frame in range(duration+10): if frame < add_duration: AM.add_many(frame, [alert, ]) - AM.process_alerts(frame, {}) + AM.process_alerts(frame, set()) shown = AM.current_alert != EmptyAlert should_show = frame <= show_duration assert shown == should_show, f"{frame=} {add_duration=} {duration=}" + + # check one case: + # - if alert is re-added to AM before it ends the duration is extended + if duration > 1: + AM = AlertManager() + show_duration = duration * 2 + for frame in range(duration * 2 + 10): + if frame == 0: + AM.add_many(frame, [alert, ]) + + if frame == duration: + # add alert one frame before it ends + assert AM.current_alert == alert + AM.add_many(frame, [alert, ]) + AM.process_alerts(frame, set()) + + shown = AM.current_alert != EmptyAlert + should_show = frame <= show_duration + assert shown == should_show, f"{frame=} {duration=}" diff --git a/selfdrive/test/process_replay/ref_commit b/selfdrive/test/process_replay/ref_commit index 0ec17758ac914d..22cae4b8ccfd33 100644 --- a/selfdrive/test/process_replay/ref_commit +++ b/selfdrive/test/process_replay/ref_commit @@ -1 +1 @@ -bfbdd4706abcf5757790526d99d0000644017b1e \ No newline at end of file +36bb37715542677db4a42464128ca10968f2c589 \ No newline at end of file