refactor(lounge): improve performance for list rooms

This commit is contained in:
MingxuanGame
2025-08-09 08:36:24 +00:00
parent e22c49d5db
commit 076b9d901b
2 changed files with 38 additions and 34 deletions

View File

@@ -10,7 +10,7 @@ class RoomCategory(str, Enum):
SPOTLIGHT = "spotlight" SPOTLIGHT = "spotlight"
FEATURED_ARTIST = "featured_artist" FEATURED_ARTIST = "featured_artist"
DAILY_CHALLENGE = "daily_challenge" DAILY_CHALLENGE = "daily_challenge"
REALTIME = "realtime" REALTIME = "realtime" # INTERNAL USE ONLY, DO NOT USE IN API
class MatchType(str, Enum): class MatchType(str, Enum):

View File

@@ -22,6 +22,7 @@ from .api_router import router
from fastapi import Depends, HTTPException, Query from fastapi import Depends, HTTPException, Query
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from redis.asyncio import Redis from redis.asyncio import Redis
from sqlalchemy.sql.elements import ColumnElement
from sqlmodel import col, exists, select from sqlmodel import col, exists, select
from sqlmodel.ext.asyncio.session import AsyncSession from sqlmodel.ext.asyncio.session import AsyncSession
@@ -37,48 +38,51 @@ async def get_all_rooms(
current_user: User = Depends(get_current_user), current_user: User = Depends(get_current_user),
): ):
resp_list: list[RoomResp] = [] resp_list: list[RoomResp] = []
db_rooms = (await db.exec(select(Room).where(True))).unique().all() where_clauses: list[ColumnElement[bool]] = [col(Room.category) == category]
now = datetime.now(UTC) now = datetime.now(UTC)
for room in db_rooms: if status is not None:
if category == RoomCategory.REALTIME and room.id not in MultiplayerHubs.rooms: where_clauses.append(col(Room.status) == status)
continue if mode == "open":
elif category != RoomCategory.REALTIME and category != room.category: where_clauses.append(
continue (col(Room.ends_at).is_(None))
| (col(Room.ends_at) > now.replace(tzinfo=UTC))
)
if category == RoomCategory.REALTIME:
where_clauses.append(col(Room.id).in_(MultiplayerHubs.rooms.keys()))
if mode == "participated":
where_clauses.append(
exists().where(
col(RoomParticipatedUser.room_id) == Room.id,
col(RoomParticipatedUser.user_id) == current_user.id,
)
)
if mode == "owned":
where_clauses.append(col(Room.host_id) == current_user.id)
if mode == "ended":
where_clauses.append(
(col(Room.ends_at).is_not(None))
& (col(Room.ends_at) < now.replace(tzinfo=UTC))
)
if status is not None and room.status != status: db_rooms = (
continue (
await db.exec(
if ( select(Room).where(
mode == "open" *where_clauses,
and room.ends_at is not None
and room.ends_at.replace(tzinfo=UTC) < now
):
continue
if (
mode == "participated"
and not (
await db.exec(
select(exists()).where(
RoomParticipatedUser.room_id == room.id,
RoomParticipatedUser.user_id == current_user.id,
)
) )
).first() )
): )
continue .unique()
if mode == "owned" and room.host_id != current_user.id: .all()
continue )
if mode == "ended" and (
room.ends_at is None
or room.ends_at.replace(tzinfo=UTC) < (now - timedelta(days=30))
):
continue
for room in db_rooms:
resp = await RoomResp.from_db(room, db) resp = await RoomResp.from_db(room, db)
if category == RoomCategory.REALTIME: if category == RoomCategory.REALTIME:
resp.has_password = bool( resp.has_password = bool(
MultiplayerHubs.rooms[room.id].room.settings.password.strip() MultiplayerHubs.rooms[room.id].room.settings.password.strip()
) )
resp.category = RoomCategory.NORMAL
resp_list.append(resp) resp_list.append(resp)
return resp_list return resp_list