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 <noreply@anthropic.com>
This commit is contained in:
1 parent
bef41fc2cc
commit
86bf9fd302
5 files changed
+37
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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")
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user