Squelch ongoing alerts so message is only sent once

This commit is contained in:
Emily Doherty 2024-11-30 04:45:00 -08:00
parent aa955816f9
commit cddc49bbfe
2 changed files with 7 additions and 5 deletions

View File

@ -15,8 +15,6 @@ class History:
self._last_price_movement : int = 0
self._latest_price_datum : Tuple[datetime.datetime, int] | None = None
self._update_triggers : List[UpdateTrigger] = []
self._squelched_alerts : List[Alert] = []
for alert_type in AlertType:
self._update_triggers.append(UpdateTrigger(Alert(alert_type, flavor, self._region)))
@ -39,9 +37,8 @@ class History:
async def _process_update_triggers(self) -> List[Alert]:
alerts = []
for trigger in self._update_triggers:
if trigger.check_and_update(self._latest_price_datum, self._history) and trigger.alert not in self._squelched_alerts:
if trigger.check_and_update(self._latest_price_datum, self._history):
alerts.append(trigger.alert)
return alerts
async def add_price(self, datum: Tuple[datetime.datetime, int]) -> List[Alert]:

View File

@ -9,6 +9,7 @@ class UpdateTrigger:
def __init__(self, alert: Alert):
self._alert : Alert = alert
self._last_trigger : Tuple[datetime.datetime, int] | None = None
self._squelched : bool = False
@property
def alert(self) -> Alert:
@ -75,5 +76,9 @@ class UpdateTrigger:
if comparison_operator(new_datum[1], self._last_trigger[1]):
self._last_trigger = new_datum
return True
was_squelched = self._squelched
self._squelched = True
return not was_squelched # Returns True if squelched otherwise True
elif self._squelched:
self._squelched = False
return False