Feature/song page design #2
No files matched your search
@@ -3918,6 +3918,13 @@
|
|||||||
"items": {
|
"items": {
|
||||||
"$ref": "#/components/schemas/Scrobble"
|
"$ref": "#/components/schemas/Scrobble"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"counts": {
|
||||||
|
"type": "array",
|
||||||
|
"description": "Daily plays over the last 90 days",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/Count"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
|
|||||||
@@ -40,11 +40,15 @@ def detail(args):
|
|||||||
artist=args["artist"],
|
artist=args["artist"],
|
||||||
track=args["track"],
|
track=args["track"],
|
||||||
)
|
)
|
||||||
|
counts = stats.listen_counts(
|
||||||
|
g.user, period="3month", bucket="day", artist=args["artist"], track=args["track"]
|
||||||
|
)
|
||||||
return {
|
return {
|
||||||
"artist": metadata.artist,
|
"artist": metadata.artist,
|
||||||
"track": metadata.track,
|
"track": metadata.track,
|
||||||
"metadata": metadata,
|
"metadata": metadata,
|
||||||
"items": history.items,
|
"items": history.items,
|
||||||
|
"counts": counts,
|
||||||
"page": history.page,
|
"page": history.page,
|
||||||
"per_page": history.per_page,
|
"per_page": history.per_page,
|
||||||
"total": history.total,
|
"total": history.total,
|
||||||
|
|||||||
@@ -171,6 +171,9 @@ class TrackDetailSchema(PageMetaSchema):
|
|||||||
track = fields.String()
|
track = fields.String()
|
||||||
metadata = fields.Nested(TrackMetadataSchema)
|
metadata = fields.Nested(TrackMetadataSchema)
|
||||||
items = fields.List(fields.Nested(ScrobbleSchema))
|
items = fields.List(fields.Nested(ScrobbleSchema))
|
||||||
|
counts = fields.List(
|
||||||
|
fields.Nested(CountSchema), metadata={"description": "Daily plays over the last 90 days"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SummarySchema(Schema):
|
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)
|
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."""
|
"""Scrobbles per bucket (UTC). Empty buckets are included for day buckets."""
|
||||||
if bucket not in BUCKETS: # interpolated into SQL below, so allow-list it
|
if bucket not in BUCKETS: # interpolated into SQL below, so allow-list it
|
||||||
raise ValueError(f"bucket must be one of {BUCKETS}")
|
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")
|
bucket_col = func.date_trunc(literal_column(f"'{bucket}'"), Scrobble.played_at, "UTC")
|
||||||
query = (
|
query = (
|
||||||
select(bucket_col.label("bucket"), func.count())
|
select(bucket_col.label("bucket"), func.count())
|
||||||
.where(*_user_scrobbles(user, period))
|
.where(*conditions)
|
||||||
.group_by("bucket")
|
.group_by("bucket")
|
||||||
.order_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
|
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):
|
def test_detail_pagination(client, auth, history):
|
||||||
first = client.get(
|
first = client.get(
|
||||||
"/api/v1/me/tracks?artist=Radiohead&track=Reckoner&limit=2&page=1", headers=auth
|
"/api/v1/me/tracks?artist=Radiohead&track=Reckoner&limit=2&page=1", headers=auth
|
||||||
|
|||||||
Reference in new issue
Block a user