feat: Add TTS statistics endpoint and service method for comprehensive TTS data
This commit is contained in:
@@ -41,6 +41,21 @@ async def get_track_statistics(
|
|||||||
) from e
|
) from e
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/tts-statistics")
|
||||||
|
async def get_tts_statistics(
|
||||||
|
_current_user: Annotated[User, Depends(get_current_user)],
|
||||||
|
dashboard_service: Annotated[DashboardService, Depends(get_dashboard_service)],
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Get TTS statistics."""
|
||||||
|
try:
|
||||||
|
return await dashboard_service.get_tts_statistics()
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch TTS statistics: {e!s}",
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
@router.get("/top-sounds")
|
@router.get("/top-sounds")
|
||||||
async def get_top_sounds(
|
async def get_top_sounds(
|
||||||
_current_user: Annotated[User, Depends(get_current_user)],
|
_current_user: Annotated[User, Depends(get_current_user)],
|
||||||
|
|||||||
@@ -201,8 +201,8 @@ class SoundRepository(BaseRepository[Sound]):
|
|||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def get_soundboard_statistics(self) -> dict[str, int | float]:
|
async def get_soundboard_statistics(self, sound_type: str = "SDB") -> dict[str, int | float]:
|
||||||
"""Get statistics for SDB type sounds."""
|
"""Get statistics for sounds of a specific type."""
|
||||||
try:
|
try:
|
||||||
statement = select(
|
statement = select(
|
||||||
func.count(Sound.id).label("count"),
|
func.count(Sound.id).label("count"),
|
||||||
@@ -211,7 +211,7 @@ class SoundRepository(BaseRepository[Sound]):
|
|||||||
func.sum(
|
func.sum(
|
||||||
Sound.size + func.coalesce(Sound.normalized_size, 0),
|
Sound.size + func.coalesce(Sound.normalized_size, 0),
|
||||||
).label("total_size"),
|
).label("total_size"),
|
||||||
).where(Sound.type == "SDB")
|
).where(Sound.type == sound_type)
|
||||||
|
|
||||||
result = await self.session.exec(statement)
|
result = await self.session.exec(statement)
|
||||||
row = result.first()
|
row = result.first()
|
||||||
|
|||||||
@@ -85,6 +85,21 @@ class DashboardService:
|
|||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
async def get_tts_statistics(self) -> dict[str, Any]:
|
||||||
|
"""Get comprehensive TTS statistics."""
|
||||||
|
try:
|
||||||
|
stats = await self.sound_repository.get_soundboard_statistics("TTS")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"sound_count": stats["count"],
|
||||||
|
"total_play_count": stats["total_plays"],
|
||||||
|
"total_duration": stats["total_duration"],
|
||||||
|
"total_size": stats["total_size"],
|
||||||
|
}
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Failed to get TTS statistics")
|
||||||
|
raise
|
||||||
|
|
||||||
def _get_date_filter(self, period: str) -> datetime | None: # noqa: PLR0911
|
def _get_date_filter(self, period: str) -> datetime | None: # noqa: PLR0911
|
||||||
"""Calculate the date filter based on the period."""
|
"""Calculate the date filter based on the period."""
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
|
|||||||
Reference in New Issue
Block a user