From 8ae7aa70a4bcf8d3243431059515f2eaf5a6e6ee Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Tue, 24 Feb 2026 09:45:07 +0100 Subject: [PATCH 1/4] [mock_uss] Cleanup rid notificExtractTemplateScopeFieldsFromFlightPlanations --- monitoring/mock_uss/ridsp/user_notifications.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/monitoring/mock_uss/ridsp/user_notifications.py b/monitoring/mock_uss/ridsp/user_notifications.py index 28d4ac47d0..5100ab125f 100644 --- a/monitoring/mock_uss/ridsp/user_notifications.py +++ b/monitoring/mock_uss/ridsp/user_notifications.py @@ -17,6 +17,8 @@ from . import database +NOTIFICATIONS_LIMIT = datetime.timedelta(hours=1) + class ServiceProviderUserNotifications(ImplicitDict): user_notifications: list[UserNotification] = [] @@ -46,6 +48,15 @@ def create_notifications_if_needed(self, record: "database.TestRecord"): ): self.record_notification(message=notif_str, observed_at=notif_date) + self.cleanup() + + def cleanup(self): + self.user_notifications = [ + notif + for notif in self.user_notifications + if notif.observed_at + NOTIFICATIONS_LIMIT > arrow.utcnow().datetime + ] + def check_and_generate_missing_fields_notifications( injected_flights: list[TestFlight], From 20271a1b45a8750c9b36e8281e123db6eeaddf40 Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Wed, 4 Mar 2026 12:07:50 +0100 Subject: [PATCH 2/4] Cleanup --- monitoring/mock_uss/ridsp/database.py | 43 +++++++++++++++++++ .../mock_uss/ridsp/user_notifications.py | 8 +--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/monitoring/mock_uss/ridsp/database.py b/monitoring/mock_uss/ridsp/database.py index 3732c8b291..51ccd72424 100644 --- a/monitoring/mock_uss/ridsp/database.py +++ b/monitoring/mock_uss/ridsp/database.py @@ -1,13 +1,20 @@ import json +from datetime import timedelta +import arrow from implicitdict import ImplicitDict, Optional +from monitoring.mock_uss.app import webapp from monitoring.monitorlib.multiprocessing import SynchronizedValue from monitoring.monitorlib.rid_automated_testing import injection_api from .behavior import ServiceProviderBehavior from .user_notifications import ServiceProviderUserNotifications +DB_CLEANUP_INTERVAL = timedelta(hours=1) +FLIGHTS_LIMIT = timedelta(hours=1) +NOTIFICATIONS_LIMIT = timedelta(hours=1) + class TestRecord(ImplicitDict): """Representation of RID SP's record of a set of injected test flights""" @@ -25,6 +32,14 @@ def __init__(self, **kwargs): super().__init__(**kwargs) + def cleanup_flights(self): + self.flights = [ + flight + for flight in self.flights + if flight.get_span()[1] + and flight.get_span()[1] + FLIGHTS_LIMIT > arrow.utcnow().datetime # pyright: ignore[reportOptionalOperand] + ] + class Database(ImplicitDict): """Simple pseudo-database structure tracking the state of the mock system""" @@ -33,8 +48,36 @@ class Database(ImplicitDict): behavior: ServiceProviderBehavior = ServiceProviderBehavior() notifications: ServiceProviderUserNotifications = ServiceProviderUserNotifications() + def cleanup_notifications(self): + self.notifications.cleanup(NOTIFICATIONS_LIMIT) + + def cleanup_flights(self): + to_cleanup = [] + + for test_id, test in self.tests.items(): + if test.flights: + test.cleanup_flights() + + if not test.flights: + to_cleanup.append(test_id) + + for test_id in to_cleanup: + del self.tests[test_id] + db = SynchronizedValue[Database]( Database(), decoder=lambda b: ImplicitDict.parse(json.loads(b.decode("utf-8")), Database), ) + +TASK_DATABASE_CLEANUP = "ridsp database cleanup" + + +@webapp.periodic_task(TASK_DATABASE_CLEANUP) +def database_cleanup() -> None: + with db.transact() as tx: + tx.value.cleanup_notifications() + tx.value.cleanup_flights() + + +webapp.set_task_period(TASK_DATABASE_CLEANUP, DB_CLEANUP_INTERVAL) diff --git a/monitoring/mock_uss/ridsp/user_notifications.py b/monitoring/mock_uss/ridsp/user_notifications.py index 5100ab125f..db2e924d29 100644 --- a/monitoring/mock_uss/ridsp/user_notifications.py +++ b/monitoring/mock_uss/ridsp/user_notifications.py @@ -17,8 +17,6 @@ from . import database -NOTIFICATIONS_LIMIT = datetime.timedelta(hours=1) - class ServiceProviderUserNotifications(ImplicitDict): user_notifications: list[UserNotification] = [] @@ -48,13 +46,11 @@ def create_notifications_if_needed(self, record: "database.TestRecord"): ): self.record_notification(message=notif_str, observed_at=notif_date) - self.cleanup() - - def cleanup(self): + def cleanup(self, limit): self.user_notifications = [ notif for notif in self.user_notifications - if notif.observed_at + NOTIFICATIONS_LIMIT > arrow.utcnow().datetime + if notif.observed_at.value.datetime + limit > arrow.utcnow().datetime ] From 0115e96580e29c875e1a050bccc0e7a31e8a75ea Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Wed, 4 Mar 2026 17:04:53 +0100 Subject: [PATCH 3/4] Fix for comments --- monitoring/mock_uss/ridsp/database.py | 5 +++++ monitoring/mock_uss/ridsp/user_notifications.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/monitoring/mock_uss/ridsp/database.py b/monitoring/mock_uss/ridsp/database.py index 51ccd72424..52ba6a7c18 100644 --- a/monitoring/mock_uss/ridsp/database.py +++ b/monitoring/mock_uss/ridsp/database.py @@ -12,8 +12,13 @@ from .user_notifications import ServiceProviderUserNotifications DB_CLEANUP_INTERVAL = timedelta(hours=1) +"""Clean database this often.""" + FLIGHTS_LIMIT = timedelta(hours=1) +"""Automatically remove flights we manage after this long beyond their end time.""" + NOTIFICATIONS_LIMIT = timedelta(hours=1) +"""Automatically remove notifications after this long beyond their observation time.""" class TestRecord(ImplicitDict): diff --git a/monitoring/mock_uss/ridsp/user_notifications.py b/monitoring/mock_uss/ridsp/user_notifications.py index db2e924d29..09a91669be 100644 --- a/monitoring/mock_uss/ridsp/user_notifications.py +++ b/monitoring/mock_uss/ridsp/user_notifications.py @@ -46,7 +46,7 @@ def create_notifications_if_needed(self, record: "database.TestRecord"): ): self.record_notification(message=notif_str, observed_at=notif_date) - def cleanup(self, limit): + def cleanup(self, limit: datetime.timedelta): self.user_notifications = [ notif for notif in self.user_notifications From c3c329fa82f0902cf7d91ae8413bc7bb23bf51e7 Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Mon, 9 Mar 2026 08:52:20 +0100 Subject: [PATCH 4/4] Use := --- monitoring/mock_uss/ridsp/database.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/mock_uss/ridsp/database.py b/monitoring/mock_uss/ridsp/database.py index 52ba6a7c18..32388587f8 100644 --- a/monitoring/mock_uss/ridsp/database.py +++ b/monitoring/mock_uss/ridsp/database.py @@ -41,8 +41,8 @@ def cleanup_flights(self): self.flights = [ flight for flight in self.flights - if flight.get_span()[1] - and flight.get_span()[1] + FLIGHTS_LIMIT > arrow.utcnow().datetime # pyright: ignore[reportOptionalOperand] + if (end_time := flight.get_span()[1]) + and end_time + FLIGHTS_LIMIT > arrow.utcnow().datetime ]