Skip to content

Commit 6148705

Browse files
committed
Cleanup
1 parent ead12f6 commit 6148705

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

monitoring/mock_uss/ridsp/database.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import json
2+
from datetime import timedelta
23

4+
import arrow
35
from implicitdict import ImplicitDict, Optional
46

7+
from monitoring.mock_uss.app import webapp
58
from monitoring.monitorlib.multiprocessing import SynchronizedValue
69
from monitoring.monitorlib.rid_automated_testing import injection_api
710

811
from .behavior import ServiceProviderBehavior
912
from .user_notifications import ServiceProviderUserNotifications
1013

14+
DB_CLEANUP_INTERVAL = timedelta(hours=1)
15+
FLIGHTS_LIMIT = timedelta(hours=1)
16+
NOTIFICATIONS_LIMIT = timedelta(hours=1)
17+
1118

1219
class TestRecord(ImplicitDict):
1320
"""Representation of RID SP's record of a set of injected test flights"""
@@ -25,6 +32,14 @@ def __init__(self, **kwargs):
2532

2633
super().__init__(**kwargs)
2734

35+
def cleanup_flights(self):
36+
self.flights = [
37+
flight
38+
for flight in self.flights
39+
if flight.get_span()[1]
40+
and flight.get_span()[1] + FLIGHTS_LIMIT > arrow.utcnow().datetime # pyright: ignore[reportOptionalOperand]
41+
]
42+
2843

2944
class Database(ImplicitDict):
3045
"""Simple pseudo-database structure tracking the state of the mock system"""
@@ -33,8 +48,36 @@ class Database(ImplicitDict):
3348
behavior: ServiceProviderBehavior = ServiceProviderBehavior()
3449
notifications: ServiceProviderUserNotifications = ServiceProviderUserNotifications()
3550

51+
def cleanup_notifications(self):
52+
self.notifications.cleanup(NOTIFICATIONS_LIMIT)
53+
54+
def cleanup_flights(self):
55+
to_cleanup = []
56+
57+
for test_id, test in self.tests.items():
58+
if test.flights:
59+
test.cleanup_flights()
60+
61+
if not test.flights:
62+
to_cleanup.append(test_id)
63+
64+
for test_id in to_cleanup:
65+
del self.tests[test_id]
66+
3667

3768
db = SynchronizedValue[Database](
3869
Database(),
3970
decoder=lambda b: ImplicitDict.parse(json.loads(b.decode("utf-8")), Database),
4071
)
72+
73+
TASK_DATABASE_CLEANUP = "ridsp database cleanup"
74+
75+
76+
@webapp.periodic_task(TASK_DATABASE_CLEANUP)
77+
def database_cleanup() -> None:
78+
with db.transact() as tx:
79+
tx.value.cleanup_notifications()
80+
tx.value.cleanup_flights()
81+
82+
83+
webapp.set_task_period(TASK_DATABASE_CLEANUP, DB_CLEANUP_INTERVAL)

monitoring/mock_uss/ridsp/user_notifications.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
from . import database
1919

20-
NOTIFICATIONS_LIMIT = datetime.timedelta(hours=1)
21-
2220

2321
class ServiceProviderUserNotifications(ImplicitDict):
2422
user_notifications: list[UserNotification] = []
@@ -48,13 +46,11 @@ def create_notifications_if_needed(self, record: "database.TestRecord"):
4846
):
4947
self.record_notification(message=notif_str, observed_at=notif_date)
5048

51-
self.cleanup()
52-
53-
def cleanup(self):
49+
def cleanup(self, limit):
5450
self.user_notifications = [
5551
notif
5652
for notif in self.user_notifications
57-
if notif.observed_at + NOTIFICATIONS_LIMIT > arrow.utcnow().datetime
53+
if notif.observed_at.value.datetime + limit > arrow.utcnow().datetime
5854
]
5955

6056

monitoring/monitorlib/rid_automated_testing/injection_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def get_aircraft_type(self, rid_version: RIDVersion) -> UAType:
178178
def order_telemetry(self):
179179
self.telemetry = sorted(
180180
self.telemetry,
181-
key=lambda telemetry: telemetry.timestamp.datetime
182-
if telemetry.timestamp
183-
else 0,
181+
key=lambda telemetry: (
182+
telemetry.timestamp.datetime if telemetry.timestamp else 0
183+
),
184184
)
185185

186186
def select_relevant_states(

0 commit comments

Comments
 (0)