perf(*): bound list queries, batch fetches, harden media downloads

This commit is contained in:
hh committed 2026-09-01 21:38:22 +02:00
1 parent 9c12628304
commit 1fa4f18a9b
35 files changed
+473 -138

No files matched your search

+13 -2
View File
@@ -8,6 +8,7 @@ from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
from utils.jobs import enqueue
from utils.read.models import DEFAULT_LIMIT, Page
router = APIRouter(prefix="/api", tags=["backfill"], route_class=DishkaRoute)
@@ -130,16 +131,26 @@ async def list_jobs(
pool: FromDishka[asyncpg.Pool],
account_id: Annotated[int, Query()],
status: Annotated[str | None, Query()] = None,
limit: Annotated[int, Query()] = DEFAULT_LIMIT,
offset: Annotated[int, Query()] = 0,
) -> list[JobView]:
page = Page(limit=limit, offset=offset)
if status is None:
rows = await pool.fetch(
"SELECT * FROM jobs WHERE account_id = $1 ORDER BY id DESC", account_id
"SELECT * FROM jobs WHERE account_id = $1 "
"ORDER BY id DESC LIMIT $2 OFFSET $3",
account_id,
page.capped_limit,
page.offset,
)
else:
rows = await pool.fetch(
"SELECT * FROM jobs WHERE account_id = $1 AND status = $2 ORDER BY id DESC",
"SELECT * FROM jobs WHERE account_id = $1 AND status = $2 "
"ORDER BY id DESC LIMIT $3 OFFSET $4",
account_id,
status,
page.capped_limit,
page.offset,
)
return [_to_view(row) for row in rows]
+9
View File
@@ -10,6 +10,7 @@ from utils.policy import repository
from utils.read import chats
from utils.read.models import (
DEFAULT_LIMIT,
MAX_LIMIT,
ChatListItem,
MessageVersionView,
MessageView,
@@ -49,6 +50,14 @@ async def list_chats(
)
@router.get("/chats/batch")
async def get_chats_batch(
pool: FromDishka[asyncpg.Pool], account_id: AccountId, ids: Annotated[str, Query()]
) -> list[ChatListItem]:
parsed = [int(part) for part in ids.split(",") if part.strip()]
return await chats.get_chats(pool, account_id, parsed[:MAX_LIMIT])
@router.get("/chats/{chat_id}")
async def get_chat(
pool: FromDishka[asyncpg.Pool], chat_id: int, account_id: AccountId
+2 -1
View File
@@ -13,7 +13,7 @@ from utils.policy import repository as policy_repository
from utils.policy.defaults import TRACKING
from utils.policy.models import ScopeType
from utils.read import discover
from utils.read.models import DiscoverItem
from utils.read.models import MAX_LIMIT, DiscoverItem
router = APIRouter(prefix="/api", tags=["discover"], route_class=DishkaRoute)
@@ -68,6 +68,7 @@ async def discover_peers(
) -> list[DiscoverItem]:
if not query.strip():
return []
limit = min(limit, MAX_LIMIT)
items = await discover.search(pool, account_id, query, limit)
if not remote:
return items
+8 -2
View File
@@ -52,9 +52,15 @@ async def chat_links(
@router.get("/calendar")
async def chat_calendar(
pool: FromDishka[asyncpg.Pool], chat_id: int, account_id: AccountId
pool: FromDishka[asyncpg.Pool],
chat_id: int,
account_id: AccountId,
date_from: Annotated[datetime | None, Query()] = None,
date_to: Annotated[datetime | None, Query()] = None,
) -> list[DayCount]:
return await profile.daily_counts(pool, account_id, chat_id)
return await profile.daily_counts(
pool, account_id, chat_id, date_from=date_from, date_to=date_to
)
@router.get("/message-at")