The taste build graded every play in its window, and a play with no completion ratio and no played duration came out as completion 0: a song playing during the rebuild, or a row the sweeper closed without hearing how it ended, counted as abandoned and pushed its artist and tags negative. Only plays whose completion is known are graded now; an open play is graded once it ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.6 KiB
SQL
106 lines
3.6 KiB
SQL
-- Taste profile. The daily build computes signed artist/tag
|
|
-- weights in Go (graded engagement + decay + like bonus) and atomic-replaces
|
|
-- them here. Read queries serve the scorer's "top taste" lookups.
|
|
|
|
-- name: ListPlayEngagementInputsForUser :many
|
|
-- One row per play in the decay-relevant window. completion is the play's
|
|
-- completion ratio (precomputed column when present, else duration_played /
|
|
-- track duration, clamped to [0,1]); age_days drives the time-decay. Genre
|
|
-- is split into tags in Go; release_date derives the decade for the era
|
|
-- facet. Quarantined tracks are excluded, and so is a play whose completion
|
|
-- isn't known: one still open (playing now), or one the sweeper closed
|
|
-- without hearing how it ended. Graded as 0 they would read as skips.
|
|
SELECT
|
|
t.id AS track_id,
|
|
t.artist_id,
|
|
t.genre,
|
|
a.release_date,
|
|
LEAST(GREATEST(
|
|
COALESCE(pe.completion_ratio,
|
|
pe.duration_played_ms::float8 / NULLIF(t.duration_ms, 0),
|
|
0), 0), 1)::float8 AS completion,
|
|
(EXTRACT(epoch FROM now() - pe.started_at) / 86400.0)::float8 AS age_days
|
|
FROM play_events pe
|
|
JOIN tracks t ON t.id = pe.track_id
|
|
JOIN albums a ON a.id = t.album_id
|
|
WHERE pe.user_id = $1
|
|
AND pe.started_at > now() - ($2::float8 * INTERVAL '1 day')
|
|
AND (pe.completion_ratio IS NOT NULL OR pe.duration_played_ms IS NOT NULL)
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM quarantine q
|
|
WHERE q.user_id = $1 AND q.track_id = t.id
|
|
);
|
|
|
|
-- name: ListLikedTrackTasteInputsForUser :many
|
|
-- (track_id, artist_id, genre, release_date) for each track the user has
|
|
-- explicitly liked. Feeds the track-like bonus into the liked track's artist,
|
|
-- tags, and era; track_id keys the enriched track_tags lookup.
|
|
SELECT t.id AS track_id, t.artist_id, t.genre, a.release_date
|
|
FROM general_likes gl
|
|
JOIN tracks t ON t.id = gl.track_id
|
|
JOIN albums a ON a.id = t.album_id
|
|
WHERE gl.user_id = $1;
|
|
|
|
-- name: ListLikedArtistIDsForUser :many
|
|
-- Artists the user has explicitly liked; feeds the artist-like bonus.
|
|
SELECT artist_id FROM general_likes_artists WHERE user_id = $1;
|
|
|
|
-- name: DeleteTasteProfileArtistsForUser :exec
|
|
DELETE FROM taste_profile_artists WHERE user_id = $1;
|
|
|
|
-- name: InsertTasteProfileArtist :exec
|
|
INSERT INTO taste_profile_artists (user_id, artist_id, weight)
|
|
VALUES ($1, $2, $3);
|
|
|
|
-- name: DeleteTasteProfileTagsForUser :exec
|
|
DELETE FROM taste_profile_tags WHERE user_id = $1;
|
|
|
|
-- name: InsertTasteProfileTag :exec
|
|
INSERT INTO taste_profile_tags (user_id, tag, weight)
|
|
VALUES ($1, $2, $3);
|
|
|
|
-- name: ListTasteProfileArtistsForUser :many
|
|
-- Top-weighted taste artists, for the scorer and tests.
|
|
SELECT artist_id, weight
|
|
FROM taste_profile_artists
|
|
WHERE user_id = $1
|
|
ORDER BY weight DESC
|
|
LIMIT $2;
|
|
|
|
-- name: ListTasteProfileTagsForUser :many
|
|
SELECT tag, weight
|
|
FROM taste_profile_tags
|
|
WHERE user_id = $1
|
|
ORDER BY weight DESC
|
|
LIMIT $2;
|
|
|
|
-- name: DeleteTasteProfileErasForUser :exec
|
|
DELETE FROM taste_profile_eras WHERE user_id = $1;
|
|
|
|
-- name: InsertTasteProfileEra :exec
|
|
INSERT INTO taste_profile_eras (user_id, era, weight)
|
|
VALUES ($1, $2, $3);
|
|
|
|
-- name: ListTasteProfileErasForUser :many
|
|
-- Top-weighted taste eras; consumed by the scorer's era term.
|
|
SELECT era, weight
|
|
FROM taste_profile_eras
|
|
WHERE user_id = $1
|
|
ORDER BY weight DESC
|
|
LIMIT $2;
|
|
|
|
-- name: DeleteTasteProfileMoodsForUser :exec
|
|
DELETE FROM taste_profile_moods WHERE user_id = $1;
|
|
|
|
-- name: InsertTasteProfileMood :exec
|
|
INSERT INTO taste_profile_moods (user_id, mood, weight)
|
|
VALUES ($1, $2, $3);
|
|
|
|
-- name: ListTasteProfileMoodsForUser :many
|
|
-- Top-weighted taste moods; consumed by the scorer's mood term.
|
|
SELECT mood, weight
|
|
FROM taste_profile_moods
|
|
WHERE user_id = $1
|
|
ORDER BY weight DESC
|
|
LIMIT $2;
|