🌊 Волна 8: backend качество (4 бага)
- admin_level_counts_n_plus_1: один GROUP BY вместо 6 запросов (admin.py) - duplicate_index_typing_progress: удалён дубль индекса (database.py) - teacher_create_table_on_every_request: DDL в init_db (teacher.py + database.py) - on_event_deprecated: lifespan-контекст (main.py) drop_matview_on_startup + leaderboard_refresh_30s — уже в Волне 1. Аудит: Issue #4, backend 4/20 задач
This commit is contained in:
@@ -4,6 +4,7 @@ import os
|
||||
import re
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
@@ -47,8 +48,6 @@ if SENTRY_DSN:
|
||||
else:
|
||||
logger.warning("SENTRY_DSN not set — Sentry disabled")
|
||||
|
||||
app = FastAPI(title="Filwords PL API")
|
||||
|
||||
|
||||
# ============ Rate Limiting ============
|
||||
class RateLimiter:
|
||||
@@ -117,6 +116,105 @@ class RateLimiter:
|
||||
rate_limiter = RateLimiter()
|
||||
|
||||
|
||||
# ============ Фоновое обновление кэша рейтинга ============
|
||||
async def refresh_leaderboard_cache_loop():
|
||||
await asyncio.sleep(10)
|
||||
while True:
|
||||
try:
|
||||
from database import get_pool
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY leaderboard_cache")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to refresh leaderboard cache: {e}")
|
||||
# 🔧 ФИКС (23.09.2026): leaderboard_refresh_30s — refresh каждые 30с
|
||||
# нагружал БД ради рейтинга, который меняется редко. Теперь 5 минут.
|
||||
await asyncio.sleep(300)
|
||||
|
||||
|
||||
# 🔧 ФИКС (23.09.2026): on_event_deprecated — переходим на lifespan-контекст.
|
||||
# @app.on_event("startup") устарел и будет удалён в новых версиях FastAPI.
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# ============ Startup ============
|
||||
await init_db()
|
||||
await init_sentences()
|
||||
await init_achievements()
|
||||
await init_learning_data()
|
||||
|
||||
asyncio.create_task(rate_limiter.start_cleanup())
|
||||
|
||||
from database import get_pool
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
# 🔧 ФИКС (23.09.2026): drop_matview_on_startup — раньше DROP + CREATE
|
||||
# на каждом рестарте. Теперь только CREATE IF NOT EXISTS.
|
||||
# При изменении определения — отдельная миграция.
|
||||
await conn.execute("""
|
||||
CREATE MATERIALIZED VIEW IF NOT EXISTS leaderboard_cache AS
|
||||
SELECT
|
||||
u.telegram_id,
|
||||
u.username,
|
||||
u.current_level,
|
||||
COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) as total_score,
|
||||
COALESCE(ws.total, 0) as wordsearch_score,
|
||||
COALESCE(s.total, 0) as sentence_score,
|
||||
COALESCE(wq.total, 0) as wordquiz_score,
|
||||
COALESCE(wq.words_guessed, 0) as words_guessed,
|
||||
COALESCE(bw.total, 0) as bubblewords_score,
|
||||
COALESCE(bw.levels_completed, 0) as bubble_levels_completed
|
||||
FROM users u
|
||||
LEFT JOIN (
|
||||
SELECT user_id, SUM(score) as total
|
||||
FROM user_progress
|
||||
WHERE completed_at IS NOT NULL
|
||||
GROUP BY user_id
|
||||
) ws ON u.telegram_id = ws.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, SUM(score) as total
|
||||
FROM sentence_progress
|
||||
WHERE completed_at IS NOT NULL
|
||||
GROUP BY user_id
|
||||
) s ON u.telegram_id = s.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, total_score as total, words_guessed
|
||||
FROM word_quiz_progress
|
||||
) wq ON u.telegram_id = wq.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, total_score as total, levels_completed
|
||||
FROM bubble_words_progress
|
||||
) bw ON u.telegram_id = bw.user_id
|
||||
WHERE COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) > 0
|
||||
ORDER BY total_score DESC
|
||||
""")
|
||||
|
||||
await conn.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_leaderboard_cache_user
|
||||
ON leaderboard_cache (telegram_id)
|
||||
""")
|
||||
|
||||
logger.info("Leaderboard cache created")
|
||||
|
||||
refresh_task = asyncio.create_task(refresh_leaderboard_cache_loop())
|
||||
|
||||
logger.info(f"Filwords API v{APP_VERSION} started")
|
||||
|
||||
yield
|
||||
|
||||
# ============ Shutdown ============
|
||||
# 🔧 ФИКС (23.09.2026): отменяем фоновые задачи при остановке.
|
||||
refresh_task.cancel()
|
||||
try:
|
||||
await refresh_task
|
||||
except asyncio.CancelledError:
|
||||
logger.info("Leaderboard refresh task cancelled")
|
||||
|
||||
logger.info(f"Filwords API v{APP_VERSION} stopped")
|
||||
|
||||
|
||||
app = FastAPI(title="Filwords PL API", lifespan=lifespan)
|
||||
|
||||
|
||||
# ============ Rate Limiting Middleware ============
|
||||
@app.middleware("http")
|
||||
async def rate_limit_middleware(request: Request, call_next):
|
||||
@@ -198,88 +296,6 @@ app.include_router(feed.router)
|
||||
app.include_router(admin_content.router)
|
||||
|
||||
|
||||
# ============ Фоновое обновление кэша рейтинга ============
|
||||
async def refresh_leaderboard_cache_loop():
|
||||
await asyncio.sleep(10)
|
||||
while True:
|
||||
try:
|
||||
from database import get_pool
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY leaderboard_cache")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to refresh leaderboard cache: {e}")
|
||||
# 🔧 ФИКС (23.09.2026): leaderboard_refresh_30s — refresh каждые 30с
|
||||
# нагружал БД ради рейтинга, который меняется редко. Теперь 5 минут.
|
||||
await asyncio.sleep(300)
|
||||
|
||||
|
||||
# ============ Startup ============
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await init_db()
|
||||
await init_sentences()
|
||||
await init_achievements()
|
||||
await init_learning_data()
|
||||
|
||||
asyncio.create_task(rate_limiter.start_cleanup())
|
||||
|
||||
from database import get_pool
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
# 🔧 ФИКС (23.09.2026): drop_matview_on_startup — раньше DROP + CREATE
|
||||
# на каждом рестарте. Теперь только CREATE IF NOT EXISTS.
|
||||
# При изменении определения — отдельная миграция.
|
||||
await conn.execute("""
|
||||
CREATE MATERIALIZED VIEW IF NOT EXISTS leaderboard_cache AS
|
||||
SELECT
|
||||
u.telegram_id,
|
||||
u.username,
|
||||
u.current_level,
|
||||
COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) as total_score,
|
||||
COALESCE(ws.total, 0) as wordsearch_score,
|
||||
COALESCE(s.total, 0) as sentence_score,
|
||||
COALESCE(wq.total, 0) as wordquiz_score,
|
||||
COALESCE(wq.words_guessed, 0) as words_guessed,
|
||||
COALESCE(bw.total, 0) as bubblewords_score,
|
||||
COALESCE(bw.levels_completed, 0) as bubble_levels_completed
|
||||
FROM users u
|
||||
LEFT JOIN (
|
||||
SELECT user_id, SUM(score) as total
|
||||
FROM user_progress
|
||||
WHERE completed_at IS NOT NULL
|
||||
GROUP BY user_id
|
||||
) ws ON u.telegram_id = ws.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, SUM(score) as total
|
||||
FROM sentence_progress
|
||||
WHERE completed_at IS NOT NULL
|
||||
GROUP BY user_id
|
||||
) s ON u.telegram_id = s.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, total_score as total, words_guessed
|
||||
FROM word_quiz_progress
|
||||
) wq ON u.telegram_id = wq.user_id
|
||||
LEFT JOIN (
|
||||
SELECT user_id, total_score as total, levels_completed
|
||||
FROM bubble_words_progress
|
||||
) bw ON u.telegram_id = bw.user_id
|
||||
WHERE COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) > 0
|
||||
ORDER BY total_score DESC
|
||||
""")
|
||||
|
||||
await conn.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_leaderboard_cache_user
|
||||
ON leaderboard_cache (telegram_id)
|
||||
""")
|
||||
|
||||
logger.info("Leaderboard cache created")
|
||||
|
||||
asyncio.create_task(refresh_leaderboard_cache_loop())
|
||||
|
||||
logger.info(f"Filwords API v{APP_VERSION} started")
|
||||
|
||||
|
||||
# ============ Базовые эндпоинты ============
|
||||
@app.get("/")
|
||||
async def root():
|
||||
|
||||
Reference in New Issue
Block a user