package recommendation import ( "github.com/jackc/pgx/v5/pgtype" "github.com/Fabled-Sword/minstrel/internal/db/dbq" "github.com/Fabled-Sword/minstrel/internal/genretag" ) type SessionVector struct { Seed bool `json:"seed"` Artists []string `json:"artists"` Tags map[string]int `json:"tags"` RecentTrackIDs []string `json:"recent_track_ids"` // DeviceClass is the current request's device, set by the radio // handler from the user's latest play; drives the device dimension of the // context-affinity term. Empty (omitted) for the daily mixes and for the // snapshot stored on play_events, so it never narrows those. DeviceClass string `json:"device_class,omitempty"` } func BuildSessionVector(priorTracks []dbq.Track) SessionVector { v := SessionVector{ Seed: len(priorTracks) < 3, Artists: []string{}, Tags: map[string]int{}, RecentTrackIDs: []string{}, } seen := map[string]bool{} for _, t := range priorTracks { artistID := uuidString(t.ArtistID) if !seen[artistID] { seen[artistID] = true v.Artists = append(v.Artists, artistID) } for _, g := range genretag.Split(t.Genre) { v.Tags[g]++ } v.RecentTrackIDs = append(v.RecentTrackIDs, uuidString(t.ID)) } return v } func uuidString(u pgtype.UUID) string { if !u.Valid { return "" } return u.String() }