feat(storage): library + disk statistics endpoint (§A6)
Implement `GET /api/v1/storage`, replacing the stub. Returns aggregate library facts (track/artist/album counts, total footprint, playtime, per-format / per-source / metadata-status breakdowns, top genres) plus the real capacity of the backing volume. - domain: `LibraryStats`, `FormatBreakdown`, `DiskUsage` value objects - ports: `FileStorage.disk_usage()` (local = shutil.disk_usage walking up to the nearest existing ancestor; S3 returns None — no fixed disk) - repo: `TrackRepository.library_stats()` (single set of GROUP BYs) - tests: storage stats API (auth, empty library, upload counting) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"""Storage / library statistics response schemas (§A6)."""
|
||||
|
||||
import datetime as dt
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class DiskUsageOut(BaseModel):
|
||||
total: int
|
||||
used: int
|
||||
free: int
|
||||
|
||||
|
||||
class FormatBreakdownOut(BaseModel):
|
||||
file_format: str
|
||||
track_count: int
|
||||
total_size: int
|
||||
|
||||
|
||||
class GenreCountOut(BaseModel):
|
||||
genre: str
|
||||
track_count: int
|
||||
|
||||
|
||||
class StorageStatsOut(BaseModel):
|
||||
"""Everything the Storage screen needs in a single call."""
|
||||
|
||||
# library catalogue
|
||||
total_tracks: int
|
||||
total_artists: int
|
||||
total_albums: int
|
||||
total_size: int
|
||||
total_duration_seconds: int
|
||||
largest_track_size: int
|
||||
earliest_added: dt.datetime | None
|
||||
latest_added: dt.datetime | None
|
||||
|
||||
# breakdowns
|
||||
by_format: list[FormatBreakdownOut]
|
||||
by_metadata_status: dict[str, int]
|
||||
by_source: dict[str, int]
|
||||
top_genres: list[GenreCountOut]
|
||||
|
||||
# backing volume (``None`` for object-store backends)
|
||||
disk: DiskUsageOut | None
|
||||
+60
-1
@@ -4,11 +4,70 @@ from typing import Any
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.deps import (
|
||||
AlbumRepoDep,
|
||||
ArtistRepoDep,
|
||||
CurrentUser,
|
||||
FileStorageDep,
|
||||
TrackRepoDep,
|
||||
)
|
||||
from app.api.schemas.storage import (
|
||||
DiskUsageOut,
|
||||
FormatBreakdownOut,
|
||||
GenreCountOut,
|
||||
StorageStatsOut,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/storage", tags=["storage"])
|
||||
|
||||
# How many of the most common genres the dashboard surfaces.
|
||||
_TOP_GENRES = 8
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def get_storage_stats() -> Any: ...
|
||||
async def get_storage_stats(
|
||||
track_repo: TrackRepoDep,
|
||||
artist_repo: ArtistRepoDep,
|
||||
album_repo: AlbumRepoDep,
|
||||
storage: FileStorageDep,
|
||||
_: CurrentUser,
|
||||
) -> StorageStatsOut:
|
||||
"""Library + disk statistics for the Storage dashboard (§A6).
|
||||
|
||||
Aggregates come from the catalogue (cheap GROUP BYs); ``disk`` reflects the
|
||||
real backing volume and is ``None`` for backends without a fixed-capacity
|
||||
disk (e.g. object stores)."""
|
||||
stats = await track_repo.library_stats()
|
||||
total_artists = await artist_repo.count(q=None)
|
||||
total_albums = await album_repo.count(artist_id=None, q=None)
|
||||
genres = await track_repo.genres()
|
||||
disk = await storage.disk_usage()
|
||||
|
||||
return StorageStatsOut(
|
||||
total_tracks=stats.total_tracks,
|
||||
total_artists=total_artists,
|
||||
total_albums=total_albums,
|
||||
total_size=stats.total_size,
|
||||
total_duration_seconds=stats.total_duration_seconds,
|
||||
largest_track_size=stats.largest_track_size,
|
||||
earliest_added=stats.earliest_added,
|
||||
latest_added=stats.latest_added,
|
||||
by_format=[
|
||||
FormatBreakdownOut(
|
||||
file_format=f.file_format,
|
||||
track_count=f.track_count,
|
||||
total_size=f.total_size,
|
||||
)
|
||||
for f in stats.by_format
|
||||
],
|
||||
by_metadata_status=stats.by_metadata_status,
|
||||
by_source=stats.by_source,
|
||||
top_genres=[
|
||||
GenreCountOut(genre=genre, track_count=count)
|
||||
for genre, count in genres[:_TOP_GENRES]
|
||||
],
|
||||
disk=DiskUsageOut(total=disk.total, used=disk.used, free=disk.free) if disk else None,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/duplicates")
|
||||
|
||||
Reference in New Issue
Block a user