277898a49a
Add per-user artist-suggestion service ranking out-of-library MBIDs by signal x similarity. Single-CTE SQL collects user likes (5x weight) and recency-decayed plays, joins against artist_similarity_unmatched, and filters in-library candidates plus non-terminal lidarr_requests. The service resolves top-3 attribution seeds to artist names in a batched GetArtistsByIDs call so the UI can render "because you liked X" reasons. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
45 lines
1.4 KiB
SQL
45 lines
1.4 KiB
SQL
-- name: UpsertArtist :one
|
|
-- Insert or update by mbid when present; otherwise by (name, sort_name).
|
|
-- Callers pass the canonical sort_name; scanner is responsible for derivation.
|
|
INSERT INTO artists (name, sort_name, mbid)
|
|
VALUES ($1, $2, $3)
|
|
ON CONFLICT (mbid) WHERE mbid IS NOT NULL
|
|
DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
sort_name = EXCLUDED.sort_name,
|
|
updated_at = now()
|
|
RETURNING *;
|
|
|
|
-- name: GetArtistByID :one
|
|
SELECT * FROM artists WHERE id = $1;
|
|
|
|
-- name: GetArtistByName :one
|
|
SELECT * FROM artists WHERE name = $1 LIMIT 1;
|
|
|
|
-- name: ListArtists :many
|
|
SELECT * FROM artists ORDER BY sort_name;
|
|
|
|
-- name: SearchArtists :many
|
|
SELECT * FROM artists
|
|
WHERE name ILIKE '%' || $1 || '%'
|
|
ORDER BY sort_name
|
|
LIMIT $2 OFFSET $3;
|
|
|
|
-- name: ListArtistsAlpha :many
|
|
SELECT * FROM artists ORDER BY sort_name, name, id LIMIT $1 OFFSET $2;
|
|
|
|
-- name: ListArtistsNewest :many
|
|
-- Secondary id tiebreaker keeps pagination stable when created_at ties.
|
|
SELECT * FROM artists ORDER BY created_at DESC, id DESC LIMIT $1 OFFSET $2;
|
|
|
|
-- name: CountArtists :one
|
|
SELECT COUNT(*) FROM artists;
|
|
|
|
-- name: CountArtistsMatching :one
|
|
SELECT COUNT(*) FROM artists WHERE name ILIKE '%' || $1::text || '%';
|
|
|
|
-- name: GetArtistsByIDs :many
|
|
-- Batched lookup used by M5c suggestion attribution to resolve top-3
|
|
-- contributing seed UUIDs back to artist names in one round-trip.
|
|
SELECT * FROM artists WHERE id = ANY($1::uuid[]);
|