Files
agessaman b20e20925d refactor: one query for the unique recent repeater behind a path prefix
Seven places in message_handler and graph_trace_helper ran the same pair of queries, byte for byte: count the repeaters and room servers whose key starts with the prefix and were heard within the recency window, and if exactly one was, fetch its key (starred first, then most recent). contacts_repo.unique_recent_repeater_key now runs it and returns (count, key); each caller keeps its own logging, including the two that report the match count. Same queries, same order, same handling of execute_query's empty result on a database error.
2026-10-01 11:02:57 -07:00

44 lines
1.8 KiB
Python

"""Shared queries over complete_contact_tracking.
Rows only: selection and tie-breaking stay with each caller. Database errors
follow ``DBManager.execute_query``, which logs them and returns no rows.
"""
from __future__ import annotations
from typing import Any, Optional
def unique_recent_repeater_key(db_manager: Any, prefix: str, recency_days: int) -> tuple[int, Optional[str]]:
"""``(count, public_key)`` for repeaters/room servers whose key starts with *prefix*.
``count`` is how many distinct such nodes were heard within *recency_days*.
``public_key`` is set only when exactly one was, since only then is the
prefix known to name that node (starred first, then most recently heard).
"""
prefix_pattern = f"{prefix}%"
count_query = f"""
SELECT COUNT(DISTINCT public_key) as count
FROM complete_contact_tracking
WHERE public_key LIKE ?
AND role IN ('repeater', 'roomserver')
AND COALESCE(last_advert_timestamp, last_heard) >= datetime('now', '-{recency_days} days')
"""
count_results = db_manager.execute_query(count_query, (prefix_pattern,))
count = count_results[0].get("count", 0) if count_results else 0
if count != 1:
return count, None
query = f"""
SELECT public_key
FROM complete_contact_tracking
WHERE public_key LIKE ?
AND role IN ('repeater', 'roomserver')
AND COALESCE(last_advert_timestamp, last_heard) >= datetime('now', '-{recency_days} days')
ORDER BY is_starred DESC, COALESCE(last_advert_timestamp, last_heard) DESC
LIMIT 1
"""
results = db_manager.execute_query(query, (prefix_pattern,))
if results and results[0].get("public_key"):
return count, results[0]["public_key"]
return count, None