sonda/internal/storage/taste.go
tgiachi d251cc4f39 fix(taste): order by the score, clamp future events, keep tags short
- Order the liked artists by the weighted sum, not by tanh, which rounds to
  exactly 1 after about 58 listens and made heavy artists tie.
- An event dated after now counts as happening now, also for the last listen.
- Genres of files that are gone no longer shape the tag profile, as in
  /artists/{id}/tags.
- The tag list is capped at 50.
- A zero half-life falls back to 90 days instead of producing NaN.
- Document the 10 half-lives window and the new rules in the spec and README.
- Tests: heavy artists keep their order, tracks with two artists credit both,
  genres of a missing file are left out, the tag cap, a zero half-life.
2026-11-11 18:52:08 +01:00

195 lines
5.3 KiB
Go

package storage
import (
"context"
"fmt"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/listening"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/taste"
)
// TasteRepo reads what the taste model needs. It implements taste.Store and
// never writes.
type TasteRepo struct{ db *bun.DB }
// NewTasteRepo builds a TasteRepo.
func NewTasteRepo(db *bun.DB) *TasteRepo { return &TasteRepo{db: db} }
// ArtistEvents returns the user's events since the given time, one per credited
// artist of the track. Tracks whose files are gone still count: the history is
// the listener's own.
func (r *TasteRepo) ArtistEvents(ctx context.Context, userID int64, since time.Time) ([]taste.Event, error) {
rows, err := r.db.QueryContext(ctx,
`SELECT ta.artist_id, e.event_type, e.duration_played_ms, e.occurred_at
FROM listening_events e JOIN track_artists ta ON ta.track_id = e.track_id
WHERE e.user_id = ? AND e.occurred_at >= ? AND ta.role = 'main'
ORDER BY e.occurred_at, e.id, ta.artist_id`,
userID, since.Unix())
if err != nil {
return nil, fmt.Errorf("read listening events: %w", err)
}
defer rows.Close()
var out []taste.Event
for rows.Next() {
var (
e taste.Event
typ string
at int64
)
if err := rows.Scan(&e.ArtistID, &typ, &e.PlayedMs, &at); err != nil {
return nil, err
}
e.Type, e.At = listening.EventType(typ), time.Unix(at, 0)
out = append(out, e)
}
return out, rows.Err()
}
// ArtistNames returns the names of the artists that exist.
func (r *TasteRepo) ArtistNames(ctx context.Context, ids []int64) (map[int64]string, error) {
out := map[int64]string{}
if len(ids) == 0 {
return out, nil
}
rows, err := r.db.QueryContext(ctx, "SELECT id, name FROM artists WHERE id IN (?)", bun.In(ids))
if err != nil {
return nil, fmt.Errorf("read artist names: %w", err)
}
defer rows.Close()
for rows.Next() {
var (
id int64
name string
)
if err := rows.Scan(&id, &name); err != nil {
return nil, err
}
out[id] = name
}
return out, rows.Err()
}
// ArtistTags returns the stored tags of the artists with weights from 0 to 1,
// from the genres of the user's files and from what the providers once said. It
// asks nobody. Each source is scaled to its own best tag (Last.fm's weights are
// already a percentage, MusicBrainz's are votes, the files' are track counts) and
// a tag several sources know keeps the largest weight.
func (r *TasteRepo) ArtistTags(ctx context.Context, ids []int64) (map[int64]map[string]float64, error) {
out := map[int64]map[string]float64{}
if len(ids) == 0 {
return out, nil
}
put := func(artist int64, tag string, w float64) {
if w > 1 {
w = 1
}
if out[artist] == nil {
out[artist] = map[string]float64{}
}
if w > out[artist][tag] {
out[artist][tag] = w
}
}
// Files: track counts, scaled to the artist's most common genre. Only tracks
// that still have a file: the genre of a file that is gone says nothing about
// the music that is there (the same rule as the genres of /artists/{id}/tags).
vis, visArgs := visible("t", 0)
files, err := r.db.QueryContext(ctx,
`SELECT ta.artist_id, g.genre, COUNT(DISTINCT ta.track_id)
FROM track_artists ta JOIN track_genres g ON g.track_id = ta.track_id JOIN tracks t ON t.id = ta.track_id
WHERE ta.artist_id IN (?) AND ta.role = 'main' AND `+vis+` GROUP BY ta.artist_id, g.genre`,
append([]any{bun.In(ids)}, visArgs...)...)
if err != nil {
return nil, fmt.Errorf("read genres: %w", err)
}
if err := scaleToBest(files, put); err != nil {
return nil, err
}
// Providers.
stored, err := r.db.QueryContext(ctx,
"SELECT artist_id, provider, tag, weight FROM artist_tags WHERE artist_id IN (?)", bun.In(ids))
if err != nil {
return nil, fmt.Errorf("read tags: %w", err)
}
defer stored.Close()
type key struct {
artist int64
provider string
}
type row struct {
artist int64
tag string
weight float64
}
byProvider := map[key][]row{}
best := map[key]float64{}
for stored.Next() {
var (
rw row
provider string
)
if err := stored.Scan(&rw.artist, &provider, &rw.tag, &rw.weight); err != nil {
return nil, err
}
k := key{rw.artist, provider}
byProvider[k] = append(byProvider[k], rw)
if rw.weight > best[k] {
best[k] = rw.weight
}
}
if err := stored.Err(); err != nil {
return nil, err
}
for k, rows := range byProvider {
for _, rw := range rows {
switch {
case k.provider == providers.LastFM:
put(rw.artist, rw.tag, rw.weight/100)
case best[k] > 0:
put(rw.artist, rw.tag, rw.weight/best[k])
}
}
}
return out, nil
}
// scaleToBest reads (artist, tag, count) rows and hands each one over scaled to
// the largest count of its artist.
func scaleToBest(rows interface {
Next() bool
Scan(...any) error
Err() error
Close() error
}, put func(int64, string, float64)) error {
defer rows.Close()
type row struct {
artist int64
tag string
n float64
}
var all []row
best := map[int64]float64{}
for rows.Next() {
var rw row
if err := rows.Scan(&rw.artist, &rw.tag, &rw.n); err != nil {
return err
}
all = append(all, rw)
if rw.n > best[rw.artist] {
best[rw.artist] = rw.n
}
}
if err := rows.Err(); err != nil {
return err
}
for _, rw := range all {
put(rw.artist, rw.tag, rw.n/best[rw.artist])
}
return nil
}