fix(subsonic): one listen is one play, and a daily mix played in a Subsonic app is credited to it
release / web (push) Successful in 1m23s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m50s
release / Build signed APK (push) Successful in 5m29s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m23s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m50s
release / Build signed APK (push) Successful in 5m29s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
A Subsonic app reports "now playing" when a song starts and "played" when it ends. The played report used to close the now-playing row as a play of its own and write a second one, so every listen counted twice, and as a skip when the app sent the song's start time. The report now completes the app's own row for that song. A late report for an earlier song no longer cuts short the song playing now. Subsonic's scrobble doesn't say where a song was played from, so Minstrel remembers, per user and app, the daily mix the app last opened (for six hours). A play of a song in that mix is credited to it: the mix's metrics count it and its rotation moves on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+106
-80
@@ -57,8 +57,8 @@ type StartedResult struct {
|
||||
|
||||
// RecordPlayStarted: in one transaction, auto-close any prior open row for
|
||||
// the user, FindOrCreate the play_session, insert the new play_event,
|
||||
// return its id and session id. Source-agnostic — kept for the Subsonic
|
||||
// shim (frozen API), which never originates from a system playlist.
|
||||
// return its id and session id. The play has no source; a caller that knows
|
||||
// which surface the play came from uses RecordPlayStartedWithSource.
|
||||
func (w *Writer) RecordPlayStarted(
|
||||
ctx context.Context,
|
||||
userID, trackID pgtype.UUID,
|
||||
@@ -144,60 +144,63 @@ func (w *Writer) RecordPlayStartedWithSource(
|
||||
if err := w.autoClosePriorOpen(ctx, q, userID, at); err != nil {
|
||||
return err
|
||||
}
|
||||
sessionID, err := playsessions.FindOrCreate(ctx, q, userID, at, clientID, w.sessionTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var clientIDPtr *string
|
||||
if clientID != "" {
|
||||
clientIDPtr = &clientID
|
||||
}
|
||||
var sourcePtr *string
|
||||
if source != "" {
|
||||
sourcePtr = &source
|
||||
}
|
||||
var pickKind *string
|
||||
if systemPlaylistSources[source] {
|
||||
pickKind, err = lookupSystemPickKind(ctx, q, userID, trackID, source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ev, err := q.InsertPlayEvent(ctx, dbq.InsertPlayEventParams{
|
||||
UserID: userID,
|
||||
TrackID: trackID,
|
||||
SessionID: sessionID,
|
||||
StartedAt: pgtype.Timestamptz{Time: at, Valid: true},
|
||||
ClientID: clientIDPtr,
|
||||
Source: sourcePtr,
|
||||
PickKind: pickKind,
|
||||
DeviceClass: strPtrOrNil(deviceClass),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out.PlayEventID = ev.ID
|
||||
out.SessionID = sessionID
|
||||
|
||||
// Capture session vector for the just-inserted row.
|
||||
if err := w.captureSessionVector(ctx, q, ev.ID, sessionID, at); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if systemPlaylistSources[source] {
|
||||
if err := q.AppendRotationPlayed(ctx, dbq.AppendRotationPlayedParams{
|
||||
UserID: userID,
|
||||
PlaylistKind: source,
|
||||
TrackID: trackID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
var err error
|
||||
out, err = w.openPlay(ctx, q, userID, trackID, clientID, source, deviceClass, at)
|
||||
return err
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// openPlay inserts a started play_event inside the caller's transaction. It
|
||||
// finds or creates the listening session, stamps a system-playlist play's
|
||||
// pick_kind, captures the session vector, and adds the track to that
|
||||
// playlist's rotation so its shuffle prefers what is still unplayed.
|
||||
func (w *Writer) openPlay(
|
||||
ctx context.Context,
|
||||
q *dbq.Queries,
|
||||
userID, trackID pgtype.UUID,
|
||||
clientID, source, deviceClass string,
|
||||
at time.Time,
|
||||
) (StartedResult, error) {
|
||||
sessionID, err := playsessions.FindOrCreate(ctx, q, userID, at, clientID, w.sessionTimeout)
|
||||
if err != nil {
|
||||
return StartedResult{}, err
|
||||
}
|
||||
var pickKind *string
|
||||
if systemPlaylistSources[source] {
|
||||
pickKind, err = lookupSystemPickKind(ctx, q, userID, trackID, source)
|
||||
if err != nil {
|
||||
return StartedResult{}, err
|
||||
}
|
||||
}
|
||||
ev, err := q.InsertPlayEvent(ctx, dbq.InsertPlayEventParams{
|
||||
UserID: userID,
|
||||
TrackID: trackID,
|
||||
SessionID: sessionID,
|
||||
StartedAt: pgtype.Timestamptz{Time: at, Valid: true},
|
||||
ClientID: strPtrOrNil(clientID),
|
||||
Source: strPtrOrNil(source),
|
||||
PickKind: pickKind,
|
||||
DeviceClass: strPtrOrNil(deviceClass),
|
||||
})
|
||||
if err != nil {
|
||||
return StartedResult{}, err
|
||||
}
|
||||
if err := w.captureSessionVector(ctx, q, ev.ID, sessionID, at); err != nil {
|
||||
return StartedResult{}, err
|
||||
}
|
||||
if systemPlaylistSources[source] {
|
||||
if err := q.AppendRotationPlayed(ctx, dbq.AppendRotationPlayedParams{
|
||||
UserID: userID,
|
||||
PlaylistKind: source,
|
||||
TrackID: trackID,
|
||||
}); err != nil {
|
||||
return StartedResult{}, err
|
||||
}
|
||||
}
|
||||
return StartedResult{PlayEventID: ev.ID, SessionID: sessionID}, nil
|
||||
}
|
||||
|
||||
// RecordPlayEnded closes a play_event with duration + computed completion
|
||||
// ratio. Applies the skip rule (AND of completion < threshold AND
|
||||
// duration_played < threshold) — both conditions must hold for the row to
|
||||
@@ -308,13 +311,39 @@ func (w *Writer) RecordPlaySkipped(
|
||||
})
|
||||
}
|
||||
|
||||
// RecordSyntheticCompletedPlay writes a start+end pair for a Subsonic
|
||||
// scrobble?submission=true call. ended_at = at + track.duration_ms,
|
||||
// duration_played_ms = track.duration_ms, was_skipped = false.
|
||||
// sameListenWindow is how far apart a Subsonic app's "now playing" and its
|
||||
// "played" report for the same song may be and still be one listen. The
|
||||
// report's time is the song's start in some apps and its end (or omitted,
|
||||
// so the server's clock) in others, and a song can sit paused, so the
|
||||
// window is wide. A second listen of the same song always opens its own
|
||||
// row first, since the app reports "now playing" again, so a wide window
|
||||
// can't merge two listens.
|
||||
const sameListenWindow = 6 * time.Hour
|
||||
|
||||
// within reports whether a and b are no more than d apart, in either order.
|
||||
func within(a, b time.Time, d time.Duration) bool {
|
||||
gap := a.Sub(b)
|
||||
return gap <= d && gap >= -d
|
||||
}
|
||||
|
||||
// RecordSyntheticCompletedPlay records a Subsonic scrobble?submission=true
|
||||
// call: one completed listen, duration_played_ms = track.duration_ms,
|
||||
// was_skipped = false.
|
||||
//
|
||||
// Subsonic apps report "now playing" (submission=false, which opened a row
|
||||
// through RecordPlayStarted) when a song starts and "played" when it ends.
|
||||
// When the user's open row is that same song, that row is the listen and is
|
||||
// completed in place. Otherwise a completed row is written from at. An open
|
||||
// row for a different song is closed first only when it started before at:
|
||||
// one that started after it is the song playing now, which an app reporting
|
||||
// this one late must not cut short.
|
||||
//
|
||||
// source names the system playlist the listen came from, or is "" when
|
||||
// unknown; it only applies when a new row is written.
|
||||
func (w *Writer) RecordSyntheticCompletedPlay(
|
||||
ctx context.Context,
|
||||
userID, trackID pgtype.UUID,
|
||||
clientID string,
|
||||
clientID, source string,
|
||||
at time.Time,
|
||||
) error {
|
||||
var playEventID pgtype.UUID
|
||||
@@ -324,36 +353,33 @@ func (w *Writer) RecordSyntheticCompletedPlay(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.autoClosePriorOpen(ctx, q, userID, at); err != nil {
|
||||
open, err := q.GetOpenPlayEventForUser(ctx, userID)
|
||||
hasOpen := err == nil
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
sessionID, err := playsessions.FindOrCreate(ctx, q, userID, at, clientID, w.sessionTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var clientIDPtr *string
|
||||
if clientID != "" {
|
||||
clientIDPtr = &clientID
|
||||
}
|
||||
ev, err := q.InsertPlayEvent(ctx, dbq.InsertPlayEventParams{
|
||||
UserID: userID,
|
||||
TrackID: trackID,
|
||||
SessionID: sessionID,
|
||||
StartedAt: pgtype.Timestamptz{Time: at, Valid: true},
|
||||
ClientID: clientIDPtr,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
playEventID = ev.ID
|
||||
if err := w.captureSessionVector(ctx, q, ev.ID, sessionID, at); err != nil {
|
||||
return err
|
||||
startedAt := at
|
||||
switch {
|
||||
case hasOpen && open.TrackID == trackID && within(open.StartedAt.Time, at, sameListenWindow):
|
||||
playEventID = open.ID
|
||||
startedAt = open.StartedAt.Time
|
||||
default:
|
||||
if hasOpen && !open.StartedAt.Time.After(at) {
|
||||
if err := w.autoClosePriorOpen(ctx, q, userID, at); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
started, err := w.openPlay(ctx, q, userID, trackID, clientID, source, "", at)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
playEventID = started.PlayEventID
|
||||
}
|
||||
ratio := 1.0
|
||||
dur := track.DurationMs
|
||||
_, err = q.UpdatePlayEventEnded(ctx, dbq.UpdatePlayEventEndedParams{
|
||||
ID: ev.ID,
|
||||
EndedAt: pgtype.Timestamptz{Time: at.Add(time.Duration(track.DurationMs) * time.Millisecond), Valid: true},
|
||||
ID: playEventID,
|
||||
EndedAt: pgtype.Timestamptz{Time: startedAt.Add(time.Duration(track.DurationMs) * time.Millisecond), Valid: true},
|
||||
DurationPlayedMs: &dur,
|
||||
CompletionRatio: &ratio,
|
||||
WasSkipped: false,
|
||||
|
||||
Reference in New Issue
Block a user