From 95a0f6e75a43a1fb78d85f123a95f0896befcd2b Mon Sep 17 00:00:00 2001 From: WildSoft Date: Wed, 14 Oct 2026 06:09:52 +0300 Subject: [PATCH] Update public source --- reports/access_points.py | 99 +++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/reports/access_points.py b/reports/access_points.py index b30f1b5..64bce14 100644 --- a/reports/access_points.py +++ b/reports/access_points.py @@ -124,30 +124,38 @@ def load_access_points(conn): if not rows: return [] - access_point_ids = [ - row["id"] - for row in rows - ] - - placeholders = ",".join( - "?" for _ in access_point_ids - ) - credentials = query_all( conn, - f""" + """ + WITH latest_observations AS ( + SELECT + obs.access_point_id, + ROW_NUMBER() OVER ( + PARTITION BY obs.access_point_id + ORDER BY + obs.observed_at DESC, + obs.id DESC + ) AS rn + FROM access_point_observations AS obs + WHERE + obs.latitude IS NOT NULL + AND obs.longitude IS NOT NULL + AND obs.latitude != 0 + AND obs.longitude != 0 + ) SELECT - access_point_id, - password, - source, - verified - FROM credentials - WHERE access_point_id IN ({placeholders}) + c.access_point_id, + c.password, + c.source, + c.verified + FROM credentials AS c + JOIN latest_observations AS obs + ON obs.access_point_id = c.access_point_id + AND obs.rn = 1 ORDER BY - created_at DESC, - id DESC + c.created_at DESC, + c.id DESC """, - access_point_ids, ) credentials_by_ap = {} @@ -165,35 +173,40 @@ def load_access_points(conn): "verified": credential["verified"], }) - session_conditions = [] - session_parameters = [] - - for row in rows: - - session_conditions.append( - "(access_point_id = ? AND session_id = ?)" - ) - - session_parameters.extend([ - row["id"], - row["session_id"], - ]) - handshakes = query_all( conn, - f""" + """ + WITH ranked_observations AS ( + SELECT + obs.access_point_id, + obs.session_id, + ROW_NUMBER() OVER ( + PARTITION BY obs.access_point_id + ORDER BY + obs.observed_at DESC, + obs.id DESC + ) AS rn + FROM access_point_observations AS obs + WHERE + obs.latitude IS NOT NULL + AND obs.longitude IS NOT NULL + AND obs.latitude != 0 + AND obs.longitude != 0 + ) SELECT - access_point_id, - session_id, - type, - captured_at - FROM handshakes - WHERE {" OR ".join(session_conditions)} + hs.access_point_id, + hs.session_id, + hs.type, + hs.captured_at + FROM handshakes AS hs + JOIN ranked_observations AS obs + ON obs.access_point_id = hs.access_point_id + AND obs.session_id = hs.session_id + AND obs.rn = 1 ORDER BY - captured_at DESC, - id DESC + hs.captured_at DESC, + hs.id DESC """, - session_parameters, ) handshakes_by_ap_session = {}