Core: listening stats over an explicit date window (phase 3, step 1)
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m1s
CI / test (push) Successful in 2m5s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s

scrobbler.services.stats gains scrobble_count_between, top_artists_between
and top_tracks_between, over [start, end) (start inclusive, end
exclusive) rather than a rolling period. The existing period-based
functions are re-expressed on the same _between() primitive instead of
duplicating the windowing logic.

These are for federation's weekly summaries (phase 3), which need a
fixed Monday-to-Sunday window rather than "N days back". No public API
changes; the Last.fm and /api/v1/me endpoints are unaffected (pinned by
a regression test).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Sonnet 5 committed 2026-09-26 11:34:08 +01:00
1 parent bc717c2a06
commit ea6b10637e
2 files changed
+139 -2

No files matched your search

+57 -2
View File
@@ -47,13 +47,20 @@ def since(period: str, now: datetime | None = None) -> datetime | None:
return None if days is None else (now or datetime.now(UTC)) - timedelta(days=days)
def _user_scrobbles(user: User, period: str = "overall"):
def _between(user: User, start: datetime | None, end: datetime | None):
"""user_id plus an optional window: start inclusive, end exclusive."""
conditions = [Scrobble.user_id == user.id]
if (start := since(period)) is not None:
if start is not None:
conditions.append(Scrobble.played_at >= start)
if end is not None:
conditions.append(Scrobble.played_at < end)
return conditions
def _user_scrobbles(user: User, period: str = "overall"):
return _between(user, since(period), None)
def _display(column):
"""Most common spelling within a case-insensitive group. Ties go to the capitalised
spelling: the C collation sorts uppercase first, regardless of the server locale."""
@@ -162,6 +169,54 @@ def listen_counts(user: User, period: str = "1month", bucket: str = "day") -> li
return [{"start": day, "count": count} for day, count in counts.items()]
def scrobble_count_between(user: User, start: datetime, end: datetime) -> int:
"""Total plays in [start, end)."""
return db.session.scalar(select(func.count()).where(*_between(user, start, end))) or 0
def top_artists_between(
user: User, start: datetime, end: datetime, limit: int = 10
) -> list[TopItem]:
"""Top artists in [start, end), unpaginated (callers like weekly summaries want a
handful, not a full listing)."""
key = func.lower(Scrobble.artist)
plays = func.count().label("plays")
query = (
select(_display(Scrobble.artist), plays)
.where(*_between(user, start, end))
.group_by(key)
.order_by(plays.desc(), key)
.limit(limit)
)
rows = db.session.execute(query).all()
return [TopItem(i + 1, name, count) for i, (name, count) in enumerate(rows)]
def top_tracks_between(
user: User, start: datetime, end: datetime, limit: int = 10
) -> list[TopItem]:
"""Top tracks in [start, end), unpaginated."""
keys = (func.lower(Scrobble.track), func.lower(Scrobble.artist))
plays = func.count().label("plays")
query = (
select(
_display(Scrobble.track),
_display(Scrobble.artist),
func.max(Scrobble.duration),
plays,
)
.where(*_between(user, start, end))
.group_by(*keys)
.order_by(plays.desc(), *keys)
.limit(limit)
)
rows = db.session.execute(query).all()
return [
TopItem(i + 1, name, count, artist=artist, duration=duration)
for i, (name, artist, duration, count) in enumerate(rows)
]
def summary(user: User) -> dict:
row = db.session.execute(
select(