perf/ram: coalesce traffic commits, single-scan announcement counts, prune anomaly state#77
Merged
Merged
Conversation
…e anomaly state - traffic_stats: wrap per-batch refresh upserts in one transaction so the per-row commits coalesce into a single fsync instead of one per key - announcements: replace three per-status delivery scans with one grouped query, and collapse refresh_batch_counts into a single conditional-aggregate UPDATE...FROM scan - anomaly_detection: drop key entries from the observation maps once their samples age out of the window so the dicts don't grow unbounded - admin handlers: hoist InlineKeyboardButton to the module import and remove the redundant in-function re-imports https://claude.ai/code/session_01Rfqq8EzQ7s5cY9FVLewVdn
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Acts on the verified findings from a repo-wide optimization audit (performance / RAM / dead code). Five substantive, hand-verified fixes — false positives and cosmetic items from the audit were excluded.
services/traffic_stats.pyrefresh_viewsnow wraps the per-key upsert loop in onedb.transaction(). Each upsert's owncommit()is a no-op inside an explicit transaction, so a batch of N keys flushes once instead of issuing N fsyncs (synchronous=FULL).refresh_all_awgpaginates hundreds of keys per run — this collapses hundreds of disk flushes into one per batch. Repo methods unchanged (safe for any future standalone caller)._ledger_resultreplaced three per-statusSELECTs overannouncement_deliverieswith a single grouped query (delivery_user_ids_grouped); the now-unuseddelivery_user_ids_by_statuswas removed.refresh_batch_countscollapsed from three correlatedCOUNT(*)subqueries into oneUPDATE ... FROMconditional-aggregate scan.anomaly_detection._check_thresholdsnow drops a key from_observations/_last_alertedonce its samples age out of the window, so the maps no longer accumulate an entry per key ever observed.InlineKeyboardButtonto the module-level import inbot/handlers/admin.pyand removed two redundant in-function re-imports.DEFAULT_PROXY_*config keys were confirmed unused but intentionally kept (documented legacy env contract).Test plan
python -m pytest -q— full suite, 557 passedtest_traffic_stats_hardening,test_anomaly_detection,test_key_deletion_and_announcements,test_high_blockers_and_namespy_compileon all changed fileshttps://claude.ai/code/session_01Rfqq8EzQ7s5cY9FVLewVdn
Generated by Claude Code