refactor: Improve exception handling and logging in authentication and playlist services; enhance code readability and structure
This commit is contained in:
@@ -132,7 +132,7 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
logger.exception("Failed to get sounds by types: %s", sound_types)
|
||||
raise
|
||||
|
||||
async def search_and_sort(
|
||||
async def search_and_sort( # noqa: PLR0913
|
||||
self,
|
||||
search_query: str | None = None,
|
||||
sound_types: list[str] | None = None,
|
||||
@@ -177,8 +177,14 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
return list(result.all())
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to search and sort sounds: query=%s, types=%s, sort_by=%s, sort_order=%s",
|
||||
search_query, sound_types, sort_by, sort_order,
|
||||
(
|
||||
"Failed to search and sort sounds: "
|
||||
"query=%s, types=%s, sort_by=%s, sort_order=%s"
|
||||
),
|
||||
search_query,
|
||||
sound_types,
|
||||
sort_by,
|
||||
sort_order,
|
||||
)
|
||||
raise
|
||||
|
||||
@@ -189,21 +195,26 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
func.count(Sound.id).label("count"),
|
||||
func.sum(Sound.play_count).label("total_plays"),
|
||||
func.sum(Sound.duration).label("total_duration"),
|
||||
func.sum(Sound.size + func.coalesce(Sound.normalized_size, 0)).label("total_size"),
|
||||
func.sum(
|
||||
Sound.size + func.coalesce(Sound.normalized_size, 0),
|
||||
).label("total_size"),
|
||||
).where(Sound.type == "SDB")
|
||||
|
||||
result = await self.session.exec(statement)
|
||||
row = result.first()
|
||||
|
||||
return {
|
||||
"count": row.count if row.count is not None else 0,
|
||||
"total_plays": row.total_plays if row.total_plays is not None else 0,
|
||||
"total_duration": row.total_duration if row.total_duration is not None else 0,
|
||||
"total_size": row.total_size if row.total_size is not None else 0,
|
||||
}
|
||||
except Exception:
|
||||
logger.exception("Failed to get soundboard statistics")
|
||||
raise
|
||||
else:
|
||||
return {
|
||||
"count": row.count if row.count is not None else 0,
|
||||
"total_plays": row.total_plays if row.total_plays is not None else 0,
|
||||
"total_duration": (
|
||||
row.total_duration if row.total_duration is not None else 0
|
||||
),
|
||||
"total_size": row.total_size if row.total_size is not None else 0,
|
||||
}
|
||||
|
||||
async def get_track_statistics(self) -> dict[str, int | float]:
|
||||
"""Get statistics for EXT type sounds."""
|
||||
@@ -212,21 +223,26 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
func.count(Sound.id).label("count"),
|
||||
func.sum(Sound.play_count).label("total_plays"),
|
||||
func.sum(Sound.duration).label("total_duration"),
|
||||
func.sum(Sound.size + func.coalesce(Sound.normalized_size, 0)).label("total_size"),
|
||||
func.sum(
|
||||
Sound.size + func.coalesce(Sound.normalized_size, 0),
|
||||
).label("total_size"),
|
||||
).where(Sound.type == "EXT")
|
||||
|
||||
result = await self.session.exec(statement)
|
||||
row = result.first()
|
||||
|
||||
return {
|
||||
"count": row.count if row.count is not None else 0,
|
||||
"total_plays": row.total_plays if row.total_plays is not None else 0,
|
||||
"total_duration": row.total_duration if row.total_duration is not None else 0,
|
||||
"total_size": row.total_size if row.total_size is not None else 0,
|
||||
}
|
||||
except Exception:
|
||||
logger.exception("Failed to get track statistics")
|
||||
raise
|
||||
else:
|
||||
return {
|
||||
"count": row.count if row.count is not None else 0,
|
||||
"total_plays": row.total_plays if row.total_plays is not None else 0,
|
||||
"total_duration": (
|
||||
row.total_duration if row.total_duration is not None else 0
|
||||
),
|
||||
"total_size": row.total_size if row.total_size is not None else 0,
|
||||
}
|
||||
|
||||
async def get_top_sounds(
|
||||
self,
|
||||
@@ -234,7 +250,7 @@ class SoundRepository(BaseRepository[Sound]):
|
||||
date_filter: datetime | None = None,
|
||||
limit: int = 10,
|
||||
) -> list[dict]:
|
||||
"""Get top sounds by play count for a specific type and period using SoundPlayed records."""
|
||||
"""Get top sounds by play count for a specific type and period."""
|
||||
try:
|
||||
# Join SoundPlayed with Sound and count plays within the period
|
||||
statement = (
|
||||
|
||||
Reference in New Issue
Block a user