P1-b: don't let LBS/WIFI fixes overwrite a fresh GPS fix in live view
Some checks are pending
build / build-push (push) Blocked by required conditions
build / lint-test (push) Waiting to run

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
kianiadee 2026-05-29 00:13:55 +03:00
parent 6eb6b4716c
commit 9c7b69e395

View file

@ -24,6 +24,14 @@ log = structlog.get_logger("projector.live_positions")
DRAIN_BATCH = 500 DRAIN_BATCH = 500
PROJECTED_FLAG_KEY = "live_positions_projected_at" PROJECTED_FLAG_KEY = "live_positions_projected_at"
# Low-accuracy positioning modes (cell-tower / wifi) reported in Tracksolid's
# `posType`. These can be kilometres off a real GPS fix, so we don't let one
# overwrite a *fresh* GPS fix in the live view (it would teleport the marker).
# A GPS fix older than this window does yield to LBS/WIFI, so a genuinely
# GPS-dark vehicle still updates rather than freezing forever.
LOW_ACCURACY_POS_TYPES = ("LBS", "WIFI")
GPS_PREFERENCE_WINDOW_SQL = "interval '10 minutes'"
_PLATE_FROM_DEVICE_NAME = re.compile(r"^.* - (.+?)(?:_cam|_CAM)?$") _PLATE_FROM_DEVICE_NAME = re.compile(r"^.* - (.+?)(?:_cam|_CAM)?$")
_CAMERA_NAME_RE = re.compile(r"_cam$", re.IGNORECASE) _CAMERA_NAME_RE = re.compile(r"_cam$", re.IGNORECASE)
@ -190,6 +198,13 @@ async def _project_one(
device_name = EXCLUDED.device_name, device_name = EXCLUDED.device_name,
pos_type = EXCLUDED.pos_type pos_type = EXCLUDED.pos_type
WHERE EXCLUDED.occurred_at > state.live_positions.occurred_at WHERE EXCLUDED.occurred_at > state.live_positions.occurred_at
AND NOT COALESCE(
EXCLUDED.pos_type = ANY (%s)
AND state.live_positions.pos_type = 'GPS'
AND state.live_positions.occurred_at
> now() - """ + GPS_PREFERENCE_WINDOW_SQL + """,
false
)
""", """,
( (
imei, vehicle_id, occurred_at, geom_wkt, imei, vehicle_id, occurred_at, geom_wkt,
@ -204,6 +219,7 @@ async def _project_one(
payload.get("satellites"), payload.get("satellites"),
payload.get("device_name"), payload.get("device_name"),
payload.get("pos_type"), payload.get("pos_type"),
list(LOW_ACCURACY_POS_TYPES),
), ),
) )