|
5 | 5 | from implicitdict import ImplicitDict, Optional |
6 | 6 | from uas_standards.astm.f3548.v21.api import OperationalIntent |
7 | 7 |
|
| 8 | +from monitoring.mock_uss.app import webapp |
8 | 9 | from monitoring.mock_uss.user_interactions.notifications import UserNotification |
9 | 10 | from monitoring.monitorlib.clients.flight_planning.flight_info import FlightInfo |
10 | 11 | from monitoring.monitorlib.clients.mock_uss.mock_uss_scd_injection_api import ( |
|
14 | 15 |
|
15 | 16 | DEADLOCK_TIMEOUT = timedelta(seconds=5) |
16 | 17 | NOTIFICATIONS_LIMIT = timedelta(hours=1) |
| 18 | +DB_CLEANUP_INTERVAL = timedelta(hours=1) |
| 19 | +FLIGHTS_LIMIT = timedelta(hours=1) |
| 20 | +OPERATIONAL_INTENTS_LIMIT = timedelta(hours=1) |
17 | 21 |
|
18 | 22 |
|
19 | 23 | class FlightRecord(ImplicitDict): |
@@ -42,8 +46,49 @@ def cleanup_notifications(self): |
42 | 46 | > arrow.utcnow().datetime |
43 | 47 | ] |
44 | 48 |
|
| 49 | + def cleanup_flights(self): |
| 50 | + to_cleanup = [] |
| 51 | + |
| 52 | + for flight_id, flight in self.flights.items(): |
| 53 | + if ( |
| 54 | + flight |
| 55 | + and not flight.locked |
| 56 | + and flight.op_intent.reference.time_end.value.datetime + FLIGHTS_LIMIT |
| 57 | + < arrow.utcnow().datetime |
| 58 | + ): |
| 59 | + to_cleanup.append(flight_id) |
| 60 | + |
| 61 | + for flight_id in to_cleanup: |
| 62 | + del self.flights[flight_id] |
| 63 | + |
| 64 | + def cleanup_operational_intents(self): |
| 65 | + to_cleanup = [] |
| 66 | + |
| 67 | + for op_id, op_intent in self.cached_operations.items(): |
| 68 | + if ( |
| 69 | + op_intent.reference.time_end.value.datetime + FLIGHTS_LIMIT |
| 70 | + < arrow.utcnow().datetime |
| 71 | + ): |
| 72 | + to_cleanup.append(op_id) |
| 73 | + |
| 74 | + for op_id in to_cleanup: |
| 75 | + del self.cached_operations[op_id] |
| 76 | + |
45 | 77 |
|
46 | 78 | db = SynchronizedValue[Database]( |
47 | 79 | Database(), |
48 | 80 | decoder=lambda b: ImplicitDict.parse(json.loads(b.decode("utf-8")), Database), |
49 | 81 | ) |
| 82 | + |
| 83 | +TASK_DATABASE_CLEANUP = "flights database cleanup" |
| 84 | + |
| 85 | + |
| 86 | +@webapp.periodic_task(TASK_DATABASE_CLEANUP) |
| 87 | +def database_cleanup() -> None: |
| 88 | + with db.transact() as tx: |
| 89 | + tx.value.cleanup_notifications() |
| 90 | + tx.value.cleanup_flights() |
| 91 | + tx.value.cleanup_operational_intents() |
| 92 | + |
| 93 | + |
| 94 | +webapp.set_task_period(TASK_DATABASE_CLEANUP, DB_CLEANUP_INTERVAL) |
0 commit comments