diff --git a/tasks/flightradar24/ingest/preprocess/main.py b/tasks/flightradar24/ingest/preprocess/main.py index 79805ca..03ee4a9 100644 --- a/tasks/flightradar24/ingest/preprocess/main.py +++ b/tasks/flightradar24/ingest/preprocess/main.py @@ -309,6 +309,7 @@ def process_batch(conn, packets: list, aircraft_state: dict) -> int: aircraft_state[icao24]["vrate"] = float(parsed["vrate"]) if onground is not None: aircraft_state[icao24]["onground"] = onground + aircraft_state[icao24]["velocity_ts"] = observed_at # update onground from any message if onground is not None and icao24 in aircraft_state: @@ -317,11 +318,17 @@ def process_batch(conn, packets: list, aircraft_state: dict) -> int: # for MSG3: enrich with cached velocity if msg_type == "3": cached = aircraft_state.get(icao24, {}) - if speed is None: + velocity_ts = cached.get("velocity_ts") + # use cached velocity only if it's fresh (< 30 sec old) + velocity_fresh = ( + velocity_ts is not None and + (observed_at - velocity_ts).total_seconds() < 30 + ) + if speed is None and velocity_fresh: speed = cached.get("speed") - if heading is None: + if heading is None and velocity_fresh: heading = cached.get("heading") - if vrate is None: + if vrate is None and velocity_fresh: vrate = cached.get("vrate") if onground is None: onground = cached.get("onground")