From 1747d635395ae467d8f80035a0725121837e662d Mon Sep 17 00:00:00 2001 From: savsis Date: Sun, 13 Sep 2026 22:10:12 +0500 Subject: [PATCH] perf: two more N+1s in bot.py, found by scanning the whole codebase for the pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same shape as the admin-endpoint and build_subscription_text fixes from earlier tonight, just two spots that scan hadn't covered: cb_mysub (per-node db.get_node() in the loop building the 'my subscriptions' bot message - the more user-facing of the two, fires on every tap of that button) and reconcile_pending_payments (same pattern in the 90s background reconciler, lower-impact since it only reaches the lookup for payments that just turned paid, but same fix either way for consistency). Ran a small script over every db.py call site in api.py/bot.py/ links.py to confirm these were the only two still inside a loop — everything else already resolved to a single-row lookup outside any loop. Co-Authored-By: Claude Sonnet 5 --- bot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 84084f2..86efcd4 100644 --- a/bot.py +++ b/bot.py @@ -227,9 +227,10 @@ async def cb_mysub(cb: CallbackQuery): await cb.message.edit_text("У тебя пока нет активных подписок.", reply_markup=kb) return await cb.answer() lines = ["Твои подписки\n"] + nodes_by_code = {n["code"]: n for n in db.list_nodes()} for s in subs: plan = PLANS_BY_CODE.get(s["plan"], {}).get("label", s["plan"]) - node_info = db.get_node(s["node"]) + node_info = nodes_by_code.get(s["node"]) node = node_info["label"] if node_info else s["node"] lines.append(f"{node} — {plan}, до {s['expires_at'][:10]}") lines.append(f"\n{DIVIDER}\nСсылка-подписка:\n{sub_url_for(user['token'])}") @@ -326,6 +327,7 @@ async def cb_admin_sync(cb: CallbackQuery): async def reconcile_pending_payments(): if not PAYMENTS_ENABLED: return + nodes_by_code = {n["code"]: n for n in db.list_nodes()} for payment in db.list_payments(): if payment["status"] != "pending" or not payment.get("external_id"): continue @@ -335,7 +337,7 @@ async def reconcile_pending_payments(): continue if status in payments.PAID_STATUSES: plan = PLANS_BY_CODE.get(payment["plan"]) - node_row = db.get_node(payment["node"]) + node_row = nodes_by_code.get(payment["node"]) if not plan or not node_row: continue granted = db.mark_payment_paid(payment["id"])