This commit is contained in:
gr
2026-07-31 16:37:23 +08:00
parent 3fc94c4a89
commit 5db9674490
3 changed files with 54 additions and 11 deletions
+30 -6
View File
@@ -2567,14 +2567,25 @@ async def get_logs(
if account_id is not None:
await get_owned_account(db, user, account_id)
limit = max(1, min(int(limit or 50), 500))
stmt = (
# Fetch only primary keys while MySQL performs the cross-account sort.
# Selecting the full ORM row here includes MEDIUMTEXT/TEXT columns, which
# makes MySQL 5.7 materialize a huge on-disk temporary table for non-admin
# users. Under polling load that grew ibtmp1 by tens of gigabytes.
id_stmt = (
logs_for_user(user, account_id)
.with_only_columns(MessageLog.id)
.order_by(MessageLog.created_at.desc())
.offset(max(0, int(offset or 0)))
.limit(limit)
)
result = await db.execute(stmt)
return result.scalars().all()
ordered_ids = list((await db.execute(id_stmt)).scalars().all())
if not ordered_ids:
return []
rows = (
await db.execute(select(MessageLog).where(MessageLog.id.in_(ordered_ids)))
).scalars().all()
rows_by_id = {row.id: row for row in rows}
return [rows_by_id[row_id] for row_id in ordered_ids if row_id in rows_by_id]
@app.get("/api/received-messages", response_model=List[ReceivedMessageLogResponse])
@@ -2588,13 +2599,26 @@ async def get_received_messages(
if account_id is not None:
await get_owned_account(db, user, account_id)
limit = max(1, min(int(limit or 100), 500))
stmt = (
# Keep the global sort narrow for the same reason as /api/logs. raw_content
# can be large and must only be loaded after LIMIT has selected the IDs.
id_stmt = (
received_logs_for_user(user, account_id)
.with_only_columns(ReceivedMessageLog.id)
.order_by(ReceivedMessageLog.created_at.desc())
.limit(limit)
)
result = await db.execute(stmt)
return result.scalars().all()
ordered_ids = list((await db.execute(id_stmt)).scalars().all())
if not ordered_ids:
return []
rows = (
await db.execute(
select(ReceivedMessageLog).where(
ReceivedMessageLog.id.in_(ordered_ids)
)
)
).scalars().all()
rows_by_id = {row.id: row for row in rows}
return [rows_by_id[row_id] for row_id in ordered_ids if row_id in rows_by_id]
@app.get("/api/system-logs", response_model=List[SystemLogResponse])