Further alert implementation #17

This commit is contained in:
Ian Renton
2025-10-06 17:14:52 +01:00
parent fdb2a445b8
commit e0675a01f0
7 changed files with 280 additions and 22 deletions

View File

@@ -17,10 +17,11 @@ from data.spot import Spot
class WebServer:
# Constructor
def __init__(self, spots, status_data, port):
def __init__(self, spots, alerts, status_data, port):
self.last_page_access_time = None
self.last_api_access_time = None
self.spots = spots
self.alerts = alerts
self.status_data = status_data
self.port = port
self.thread = Thread(target=self.run)
@@ -175,17 +176,21 @@ class WebServer:
# Get the query (and the right one, with Bottle magic. This is a MultiDict object)
query = bottle.request.query
# Create a shallow copy of the alert list, ordered by alert time. We'll then filter it accordingly.
# Create a shallow copy of the alert list, ordered by start time. We'll then filter it accordingly.
# We can filter by received time with "received_since", which take a UNIX timestamp in seconds UTC.
# We can also filter by source, sig, and dx_continent. Each of these accepts a single
# value or a comma-separated list.
# We can provide a "limit" number as well. Alerts are always returned newest-first; "limit" limits to only the
# most recent X alerts.
alert_ids = list(self.spots.iterkeys())
alert_ids = list(self.alerts.iterkeys())
alerts = []
for k in alert_ids:
alerts.append(self.spots.get(k))
alerts = sorted(alerts, key=lambda spot: spot.time, reverse=True)
# While we persist old spots in the system for a while to produce a useful list, any alert that has already
# passed its end time can be explicitly removed from the list to return.
# TODO deal with there being no end time
if self.alerts.get(k).end_time > datetime.now(pytz.UTC).timestamp():
alerts.append(self.alerts.get(k))
alerts = sorted(alerts, key=lambda alert: alert.start_time)
for k in query.keys():
match k:
case "received_since":