Initial implementation of custom price triggers

- likely to have some bugs, but this is good enough for a preview release.
This commit is contained in:
2025-11-06 02:46:03 -08:00
parent 19eb0a4e24
commit ed79f4b65c
8 changed files with 197 additions and 58 deletions

View File

@@ -15,10 +15,13 @@ class History:
self._last_price_movement: int = 0
self._latest_price_datum: Tuple[datetime.datetime, int] | None = None
self._update_triggers: List[UpdateTrigger] = []
# Create triggers for all non-custom alert types
for alert_type in AlertType:
self._update_triggers.append(
UpdateTrigger(Alert(alert_type, flavor, self._region))
)
if alert_type != AlertType.SPECIFIC_PRICE:
self._update_triggers.append(
UpdateTrigger(Alert(alert_type, flavor, self._region))
)
# SPECIFIC_PRICE triggers are created on-demand as they have unique prices
@property
def flavor(self) -> Flavor:
@@ -55,8 +58,21 @@ class History:
self._history.append(datum)
return await self._process_update_triggers()
async def find_update_trigger_from_alert(self, alert: Alert) -> UpdateTrigger:
async def find_update_trigger_from_alert(self, alert: Alert, initial_import: bool = False) -> UpdateTrigger:
for trigger in self._update_triggers:
if trigger.alert == alert:
return trigger
# If not found and it's a SPECIFIC_PRICE alert, create it on-demand
if alert.alert_type == AlertType.SPECIFIC_PRICE:
new_trigger = UpdateTrigger(alert)
if initial_import:
new_trigger.squelched = True
self._update_triggers.append(new_trigger)
# Initialize the trigger with current history
if self._latest_price_datum is not None:
new_trigger.check_and_update(self._latest_price_datum, self._history)
new_trigger.squelched = False
return new_trigger
raise ValueError

View File

@@ -57,6 +57,16 @@ class HistoryManager:
await history.add_price(item)
self._history[flavor][region] = history
async def load_custom_alerts(self, custom_alerts: List[Alert]):
"""Load custom price alerts and initialize their triggers with historical data."""
for alert in custom_alerts:
history = self._history[alert.flavor][alert.region]
# This will create the trigger on-demand via find_update_trigger_from_alert
trigger = await history.find_update_trigger_from_alert(alert)
# Process all historical data through this trigger to initialize its state
for datum in history.history:
trigger.check_and_update(datum, history.history)
async def update_data(self, flavor: Flavor, region: Region) -> List[Alert]:
history = self._history[flavor][region]
current_price_data = await self._tdb.current(flavor)

View File

@@ -29,6 +29,10 @@ class UpdateTrigger:
def squelched(self):
return self._squelched
@squelched.setter
def squelched(self, value):
self._squelched = value
def _find_next_trigger(
self,
comparison_operator: Callable,
@@ -93,12 +97,47 @@ class UpdateTrigger:
case AlertType.ALL_TIME_HIGH:
time_range = now - start_time
comparison_operator = operator.gt
case AlertType.SPECIFIC_PRICE:
# For custom price alerts, check if the price crosses the threshold
# We alert when price moves from below to above (or vice versa)
target_price = self._alert.price
if self._last_trigger is None:
# First time - check if current price crossed the threshold
if new_datum[1] >= target_price:
self._last_trigger = new_datum
self._last_alerting = new_datum
return True
return False
else:
# Check if we crossed the threshold
old_price = self._last_trigger[1]
new_price = new_datum[1]
# Alert if we cross the threshold in either direction
crossed_up = old_price < target_price <= new_price
crossed_down = old_price >= target_price > new_price
if crossed_up or crossed_down:
self._last_trigger = new_datum
self._last_alerting = new_datum
was_squelched = self._squelched
self._squelched = True
return not was_squelched
elif self._squelched:
# Update last_trigger but don't alert (we're tracking current price)
self._last_trigger = new_datum
# If we moved away from threshold, allow next crossing to alert
if (crossed_up and new_price < target_price) or (crossed_down and new_price >= target_price):
self._squelched = False
else:
# Just update the last trigger for tracking
self._last_trigger = new_datum
return False
case _:
# TODO: The logic here is certainly wrong for Custom
time_range = datetime.timedelta(days=int(365.25 * 6))
comparison_operator = operator.eq
if new_datum[0] > now - time_range:
if new_datum[0] > now - time_range and self._alert.alert_type != AlertType.SPECIFIC_PRICE:
if self._last_trigger is None:
self._last_trigger = new_datum
self._last_alerting = new_datum