51 lines
1.8 KiB
SQL
51 lines
1.8 KiB
SQL
-- name: ListPlayedTracksNeedingSimilarity :many
|
|
-- Tracks with at least one play, an MBID, and no fresh listenbrainz row
|
|
-- (no row at all, OR fetched_at older than 7 days). Used by the worker
|
|
-- to find work each tick. Bounded by $1.
|
|
SELECT DISTINCT t.id, t.mbid
|
|
FROM tracks t
|
|
JOIN play_events pe ON pe.track_id = t.id
|
|
WHERE t.mbid IS NOT NULL
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM track_similarity ts
|
|
WHERE ts.track_a_id = t.id
|
|
AND ts.source = 'listenbrainz'
|
|
AND ts.fetched_at > now() - interval '7 days'
|
|
)
|
|
ORDER BY t.id
|
|
LIMIT $1;
|
|
|
|
-- name: ListPlayedArtistsNeedingSimilarity :many
|
|
SELECT DISTINCT ar.id, ar.mbid
|
|
FROM artists ar
|
|
JOIN tracks t ON t.artist_id = ar.id
|
|
JOIN play_events pe ON pe.track_id = t.id
|
|
WHERE ar.mbid IS NOT NULL
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM artist_similarity asim
|
|
WHERE asim.artist_a_id = ar.id
|
|
AND asim.source = 'listenbrainz'
|
|
AND asim.fetched_at > now() - interval '7 days'
|
|
)
|
|
ORDER BY ar.id
|
|
LIMIT $1;
|
|
|
|
-- name: GetTracksByMBIDs :many
|
|
-- Bulk in-library lookup: maps a slice of MBIDs back to local track IDs.
|
|
SELECT id, mbid FROM tracks WHERE mbid = ANY($1::text[]);
|
|
|
|
-- name: GetArtistsByMBIDs :many
|
|
SELECT id, mbid FROM artists WHERE mbid = ANY($1::text[]);
|
|
|
|
-- name: UpsertTrackSimilarity :exec
|
|
INSERT INTO track_similarity (track_a_id, track_b_id, score, source, fetched_at)
|
|
VALUES ($1, $2, $3, 'listenbrainz', now())
|
|
ON CONFLICT (track_a_id, track_b_id, source)
|
|
DO UPDATE SET score = EXCLUDED.score, fetched_at = EXCLUDED.fetched_at;
|
|
|
|
-- name: UpsertArtistSimilarity :exec
|
|
INSERT INTO artist_similarity (artist_a_id, artist_b_id, score, source, fetched_at)
|
|
VALUES ($1, $2, $3, 'listenbrainz', now())
|
|
ON CONFLICT (artist_a_id, artist_b_id, source)
|
|
DO UPDATE SET score = EXCLUDED.score, fetched_at = EXCLUDED.fetched_at;
|