From 86bf9fd30226f2f5a352beed51466e115e9fbfd1 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 5 Oct 2026 15:33:06 +0100 Subject: [PATCH] Add daily play counts to the song detail endpoint Scopes listen_counts() to a single artist/track, the same way recent() was scoped for song history, so the song page can drive a per-track activity chart over the last 90 days. Co-Authored-By: Claude Sonnet 5 --- docs/openapi.json | 7 +++++++ src/scrobbler/api/tracks.py | 4 ++++ src/scrobbler/schemas/__init__.py | 3 +++ src/scrobbler/services/stats.py | 15 +++++++++++++-- tests/test_tracks.py | 10 ++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/docs/openapi.json b/docs/openapi.json index e5f4a02..1492953 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -3918,6 +3918,13 @@ "items": { "$ref": "#/components/schemas/Scrobble" } + }, + "counts": { + "type": "array", + "description": "Daily plays over the last 90 days", + "items": { + "$ref": "#/components/schemas/Count" + } } }, "additionalProperties": false diff --git a/src/scrobbler/api/tracks.py b/src/scrobbler/api/tracks.py index ba1981c..2863778 100644 --- a/src/scrobbler/api/tracks.py +++ b/src/scrobbler/api/tracks.py @@ -40,11 +40,15 @@ def detail(args): artist=args["artist"], track=args["track"], ) + counts = stats.listen_counts( + g.user, period="3month", bucket="day", artist=args["artist"], track=args["track"] + ) return { "artist": metadata.artist, "track": metadata.track, "metadata": metadata, "items": history.items, + "counts": counts, "page": history.page, "per_page": history.per_page, "total": history.total, diff --git a/src/scrobbler/schemas/__init__.py b/src/scrobbler/schemas/__init__.py index c0ff820..f999661 100644 --- a/src/scrobbler/schemas/__init__.py +++ b/src/scrobbler/schemas/__init__.py @@ -171,6 +171,9 @@ class TrackDetailSchema(PageMetaSchema): track = fields.String() metadata = fields.Nested(TrackMetadataSchema) items = fields.List(fields.Nested(ScrobbleSchema)) + counts = fields.List( + fields.Nested(CountSchema), metadata={"description": "Daily plays over the last 90 days"} + ) class SummarySchema(Schema): diff --git a/src/scrobbler/services/stats.py b/src/scrobbler/services/stats.py index 992faa9..c99fcab 100644 --- a/src/scrobbler/services/stats.py +++ b/src/scrobbler/services/stats.py @@ -163,14 +163,25 @@ def top_tracks(user: User, period: str = "overall", page: int = 1, per_page: int return Page(items, page, per_page, total) -def listen_counts(user: User, period: str = "1month", bucket: str = "day") -> list[dict]: +def listen_counts( + user: User, + period: str = "1month", + bucket: str = "day", + artist: str | None = None, + track: str | None = None, +) -> list[dict]: """Scrobbles per bucket (UTC). Empty buckets are included for day buckets.""" if bucket not in BUCKETS: # interpolated into SQL below, so allow-list it raise ValueError(f"bucket must be one of {BUCKETS}") + conditions = _user_scrobbles(user, period) + if artist: + conditions.append(func.lower(Scrobble.artist) == artist.lower()) + if track: + conditions.append(func.lower(Scrobble.track) == track.lower()) bucket_col = func.date_trunc(literal_column(f"'{bucket}'"), Scrobble.played_at, "UTC") query = ( select(bucket_col.label("bucket"), func.count()) - .where(*_user_scrobbles(user, period)) + .where(*conditions) .group_by("bucket") .order_by("bucket") ) diff --git a/tests/test_tracks.py b/tests/test_tracks.py index b37c551..b22adc0 100644 --- a/tests/test_tracks.py +++ b/tests/test_tracks.py @@ -95,6 +95,16 @@ def test_detail_requires_auth(client): assert client.get("/api/v1/me/tracks/search?q=a").status_code == 401 +def test_detail_counts_are_daily_and_scoped_to_the_track(client, auth, history): + body = client.get("/api/v1/me/tracks?artist=Radiohead&track=Reckoner", headers=auth).get_json() + counts = body["counts"] + assert len(counts) == 91 # 90 days back plus today + # Nude (100 days ago, outside the window) and Mallory's Reckoner plays don't count. + assert sum(c["count"] for c in counts) == 3 + assert counts[-1]["start"] == NOW.date().isoformat() + assert counts[-1]["count"] == 3 + + def test_detail_pagination(client, auth, history): first = client.get( "/api/v1/me/tracks?artist=Radiohead&track=Reckoner&limit=2&page=1", headers=auth