feat: how the genres I listen to changed over time
- GET /stats/listening has genre_timeline: the share of the five biggest genres, of the smaller ones and of the songs with no genre, for each year, or for each month when all the listens are in one year, in the listener's time zone; years and months with no listen are empty - web: a stacked column chart on the Statistics page, coloured in tints of the colour of this server so it follows every theme, with a legend; left out when there is only one column - README: the new field
This commit is contained in:
parent
c52386f335
commit
08c83c948f
7 changed files with 586 additions and 8 deletions
|
|
@ -18,6 +18,36 @@ type statsService interface {
|
|||
|
||||
type statsHandlers struct{ svc statsService }
|
||||
|
||||
type timelineBucketJSON struct {
|
||||
Year int `json:"year"`
|
||||
Month int `json:"month,omitempty"`
|
||||
Listens int `json:"listens"`
|
||||
Shares []float64 `json:"shares"`
|
||||
Other float64 `json:"other"`
|
||||
Untagged float64 `json:"untagged"`
|
||||
}
|
||||
|
||||
type timelineJSON struct {
|
||||
Unit string `json:"unit"`
|
||||
Genres []string `json:"genres"`
|
||||
Buckets []timelineBucketJSON `json:"buckets"`
|
||||
}
|
||||
|
||||
func timelineBody(t stats.Timeline) timelineJSON {
|
||||
out := timelineJSON{Unit: t.Unit, Genres: t.Genres, Buckets: make([]timelineBucketJSON, 0, len(t.Buckets))}
|
||||
if out.Genres == nil {
|
||||
out.Genres = []string{}
|
||||
}
|
||||
for _, b := range t.Buckets {
|
||||
shares := b.Shares
|
||||
if shares == nil {
|
||||
shares = []float64{}
|
||||
}
|
||||
out.Buckets = append(out.Buckets, timelineBucketJSON{Year: b.Year, Month: b.Month, Listens: b.Listens, Shares: shares, Other: b.Other, Untagged: b.Untagged})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type sharesJSON struct {
|
||||
Top []shareJSON `json:"top"`
|
||||
Other float64 `json:"other"`
|
||||
|
|
@ -118,6 +148,7 @@ func (h *statsHandlers) listening(c *gin.Context) {
|
|||
"listens": got.Listens, "songs": got.Songs, "artists": got.Artists, "played_ms": got.PlayedMs,
|
||||
"years": years, "hours": got.Hours, "weekdays": got.Weekdays,
|
||||
"top_artists": artists, "top_songs": songs, "genres": genresJSON(got.Genres),
|
||||
"genre_timeline": timelineBody(got.GenreTimeline),
|
||||
}
|
||||
if !got.First.IsZero() {
|
||||
body["first"] = got.First.UTC().Format(time.RFC3339)
|
||||
|
|
|
|||
|
|
@ -78,6 +78,10 @@ func TestTheListeningStatsAreAskedForThePersonAndThePeriod(t *testing.T) {
|
|||
TopArtists: []stats.Ranked{{ID: 1, Name: "Blur", Listens: 3}},
|
||||
TopSongs: []stats.Ranked{{ID: 9, Name: "Song 2", Artists: []string{"Blur"}, Listens: 2}},
|
||||
Genres: stats.Genres{Top: []stats.Share{}},
|
||||
GenreTimeline: stats.Timeline{Unit: "month", Genres: []string{"rock", "jazz"}, Buckets: []stats.TimelineBucket{
|
||||
{Year: 2025, Month: 3, Listens: 4, Shares: []float64{0.5, 0.25}, Other: 0, Untagged: 0.25},
|
||||
{Year: 2025, Month: 4, Shares: []float64{0, 0}},
|
||||
}},
|
||||
}}
|
||||
f.listening.Hours[21] = 5
|
||||
f.listening.Weekdays[2] = 5
|
||||
|
|
@ -105,6 +109,19 @@ func TestTheListeningStatsAreAskedForThePersonAndThePeriod(t *testing.T) {
|
|||
if len(years) != 1 || years[0].(map[string]any)["year"] != 2020.0 || years[0].(map[string]any)["listens"] != 5.0 {
|
||||
t.Errorf("years = %v", b["years"])
|
||||
}
|
||||
tl := sub(b, "genre_timeline")
|
||||
buckets, _ := tl["buckets"].([]any)
|
||||
genres, _ := tl["genres"].([]any)
|
||||
if tl["unit"] != "month" || len(genres) != 2 || genres[0] != "rock" || len(buckets) != 2 {
|
||||
t.Fatalf("timeline = %v", tl)
|
||||
}
|
||||
m := buckets[0].(map[string]any)
|
||||
if m["year"] != 2025.0 || m["month"] != 3.0 || m["listens"] != 4.0 || m["other"] != 0.0 || m["untagged"] != 0.25 || m["shares"].([]any)[1] != 0.25 {
|
||||
t.Errorf("bucket = %v", m)
|
||||
}
|
||||
if e := buckets[1].(map[string]any); e["listens"] != 0.0 || len(e["shares"].([]any)) != 2 {
|
||||
t.Errorf("empty bucket = %v", e)
|
||||
}
|
||||
artists, _ := b["top_artists"].([]any)
|
||||
songs, _ := b["top_songs"].([]any)
|
||||
if len(artists) != 1 || artists[0].(map[string]any)["name"] != "Blur" || artists[0].(map[string]any)["id"] != 1.0 {
|
||||
|
|
@ -117,7 +134,7 @@ func TestTheListeningStatsAreAskedForThePersonAndThePeriod(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestListeningStatsWithNoListensLeaveOutTheFirstDay(t *testing.T) {
|
||||
f := &fakeStats{listening: stats.Listening{Years: []stats.Bucket{}, TopArtists: []stats.Ranked{}, TopSongs: []stats.Ranked{}, Genres: stats.Genres{Top: []stats.Share{}}}}
|
||||
f := &fakeStats{listening: stats.Listening{Years: []stats.Bucket{}, TopArtists: []stats.Ranked{}, TopSongs: []stats.Ranked{}, Genres: stats.Genres{Top: []stats.Share{}}, GenreTimeline: stats.Timeline{Unit: "year", Genres: []string{}, Buckets: []stats.TimelineBucket{}}}}
|
||||
h, tok := statsAPI(t, f)
|
||||
r := do(t, h, "GET", "/api/v1/stats/listening", tok, nil)
|
||||
if r.Code != 200 {
|
||||
|
|
@ -129,6 +146,9 @@ func TestListeningStatsWithNoListensLeaveOutTheFirstDay(t *testing.T) {
|
|||
if f.loc == nil || f.loc != time.UTC {
|
||||
t.Errorf("zone without tz = %v, want UTC", f.loc)
|
||||
}
|
||||
if tl := sub(r.Body, "genre_timeline"); tl["unit"] != "year" || len(tl["genres"].([]any)) != 0 || len(tl["buckets"].([]any)) != 0 {
|
||||
t.Errorf("genre_timeline = %v, want an empty one", tl)
|
||||
}
|
||||
if a, ok := r.Body["top_artists"].([]any); !ok || len(a) != 0 {
|
||||
t.Errorf("top_artists = %v, want []", r.Body["top_artists"])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5226,7 +5226,7 @@ components:
|
|||
ListeningStats:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [listens, songs, artists, played_ms, years, hours, weekdays, top_artists, top_songs, genres]
|
||||
required: [listens, songs, artists, played_ms, years, hours, weekdays, top_artists, top_songs, genres, genre_timeline]
|
||||
properties:
|
||||
listens: {type: integer}
|
||||
songs: {type: integer, description: How many different songs.}
|
||||
|
|
@ -5276,6 +5276,35 @@ components:
|
|||
artists: {type: array, items: {type: string}}
|
||||
listens: {type: integer}
|
||||
genres: {$ref: '#/components/schemas/GenreShares'}
|
||||
genre_timeline:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [unit, genres, buckets]
|
||||
description: |
|
||||
How the genres you listen to changed over time: by year, or by month when all the listens fall in one
|
||||
year. Years and months with no listen between the first and the last are there, with no listens. The
|
||||
time zone is `tz`.
|
||||
properties:
|
||||
unit: {type: string, enum: [year, month]}
|
||||
genres:
|
||||
type: array
|
||||
description: The five biggest genres, biggest first. Each bucket has a share for each, in this order.
|
||||
items: {type: string}
|
||||
buckets:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [year, listens, shares, other, untagged]
|
||||
properties:
|
||||
year: {type: integer}
|
||||
month: {type: integer, minimum: 1, maximum: 12, description: Left out when `unit` is `year`.}
|
||||
listens: {type: integer}
|
||||
shares:
|
||||
type: array
|
||||
items: {type: number, minimum: 0, maximum: 1}
|
||||
other: {type: number, minimum: 0, maximum: 1, description: The share of the smaller genres.}
|
||||
untagged: {type: number, minimum: 0, maximum: 1, description: The share with no genre.}
|
||||
|
||||
ListeningSuggestions:
|
||||
type: object
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ package stats
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -21,6 +22,8 @@ const (
|
|||
topRanked = 10
|
||||
// A year outside these is not a year a song came out: a bad tag.
|
||||
minYear, maxYear = 1000, 2999
|
||||
// timelineGenres is how many genres the chart over time names; the rest are one "other" part.
|
||||
timelineGenres = 5
|
||||
)
|
||||
|
||||
// Range is a period: From is included and Until is excluded. A zero time leaves that side open.
|
||||
|
|
@ -47,6 +50,14 @@ type GenreWeight struct {
|
|||
Weight float64
|
||||
}
|
||||
|
||||
// GenreListen is a listen with one of the genres of its song, and the part of the listen that genre has: 1 for a song
|
||||
// with one genre, 1/n for a song with n. A listen of a song with no genre is one row with an empty Genre and weight 1.
|
||||
type GenreListen struct {
|
||||
At int64 // unix seconds
|
||||
Genre string
|
||||
Weight float64
|
||||
}
|
||||
|
||||
// YearCount is how many songs came out in a year.
|
||||
type YearCount struct{ Year, Songs int }
|
||||
|
||||
|
|
@ -80,6 +91,8 @@ type Store interface {
|
|||
TopSongs(ctx context.Context, userID, minMs int64, r Range, limit int) ([]Ranked, error)
|
||||
// ListenGenres gives the weight of each genre in the listens, and how many listens have at least one.
|
||||
ListenGenres(ctx context.Context, userID, minMs int64, r Range) ([]GenreWeight, int, error)
|
||||
// ListenGenreTimes gives every listen with the genres of its song, for the chart of the genres over time.
|
||||
ListenGenreTimes(ctx context.Context, userID, minMs int64, r Range) ([]GenreListen, error)
|
||||
}
|
||||
|
||||
// Share is a genre and how much of the whole it is, from 0 to 1.
|
||||
|
|
@ -117,6 +130,25 @@ type Library struct {
|
|||
NoYear int // songs whose file has no year
|
||||
}
|
||||
|
||||
// TimelineBucket is a year or a month of listens, and what share of them each genre of the timeline has. Shares go with
|
||||
// Timeline.Genres; Other is the part of the smaller genres and Untagged the part with no genre. A bucket with listens
|
||||
// adds up to 1; one with none is all zero.
|
||||
type TimelineBucket struct {
|
||||
Year, Month int // Month is 0 when the timeline is by year
|
||||
Listens int
|
||||
Shares []float64
|
||||
Other float64
|
||||
Untagged float64
|
||||
}
|
||||
|
||||
// Timeline is how the genres you listen to changed over time: by year, or by month when the listens are all in one
|
||||
// year. The years and months with no listen between the first and the last are there, empty.
|
||||
type Timeline struct {
|
||||
Unit string // "year" or "month"
|
||||
Genres []string
|
||||
Buckets []TimelineBucket
|
||||
}
|
||||
|
||||
// Bucket is a count under a key: a year.
|
||||
type Bucket struct{ Key, Count int }
|
||||
|
||||
|
|
@ -130,6 +162,7 @@ type Listening struct {
|
|||
Weekdays [7]int // from Monday
|
||||
TopArtists, TopSongs []Ranked
|
||||
Genres Genres
|
||||
GenreTimeline Timeline
|
||||
}
|
||||
|
||||
// Service works out the statistics.
|
||||
|
|
@ -201,8 +234,14 @@ func (s *Service) Listening(ctx context.Context, userID int64, r Range, loc *tim
|
|||
return Listening{}, err
|
||||
}
|
||||
|
||||
genreTimes, err := s.store.ListenGenreTimes(ctx, userID, minMs, r)
|
||||
if err != nil {
|
||||
return Listening{}, err
|
||||
}
|
||||
|
||||
out := Listening{
|
||||
Listens: totals.Listens, Songs: totals.Songs, Artists: totals.Artists, PlayedMs: totals.PlayedMs,
|
||||
GenreTimeline: shapeTimeline(genreTimes, loc),
|
||||
Listens: totals.Listens, Songs: totals.Songs, Artists: totals.Artists, PlayedMs: totals.PlayedMs,
|
||||
Years: []Bucket{},
|
||||
TopArtists: nonNil(artists), TopSongs: nonNil(songs),
|
||||
Genres: shapeGenres(weights, tagged, totals.Listens),
|
||||
|
|
@ -221,6 +260,95 @@ func (s *Service) Listening(ctx context.Context, userID int64, r Range, loc *tim
|
|||
return out, nil
|
||||
}
|
||||
|
||||
// shapeTimeline groups the genres of the listens by year, or by month when they all fall in one year, in the time zone
|
||||
// loc, and works out the share of each of the biggest genres in each.
|
||||
func shapeTimeline(rows []GenreListen, loc *time.Location) Timeline {
|
||||
out := Timeline{Unit: "year", Genres: []string{}, Buckets: []TimelineBucket{}}
|
||||
if len(rows) == 0 {
|
||||
return out
|
||||
}
|
||||
years := map[int]bool{}
|
||||
weight := map[string]float64{}
|
||||
for _, r := range rows {
|
||||
years[time.Unix(r.At, 0).In(loc).Year()] = true
|
||||
if r.Genre != "" {
|
||||
weight[r.Genre] += r.Weight
|
||||
}
|
||||
}
|
||||
if len(years) < 2 {
|
||||
out.Unit = "month"
|
||||
}
|
||||
names := make([]string, 0, len(weight))
|
||||
for g := range weight {
|
||||
names = append(names, g)
|
||||
}
|
||||
sort.Slice(names, func(i, j int) bool {
|
||||
if weight[names[i]] != weight[names[j]] {
|
||||
return weight[names[i]] > weight[names[j]]
|
||||
}
|
||||
return names[i] < names[j]
|
||||
})
|
||||
if len(names) > timelineGenres {
|
||||
names = names[:timelineGenres]
|
||||
}
|
||||
out.Genres = names
|
||||
column := map[string]int{}
|
||||
for i, g := range names {
|
||||
column[g] = i
|
||||
}
|
||||
|
||||
type sums struct {
|
||||
total, other, untagged float64
|
||||
genre []float64
|
||||
}
|
||||
key := func(t time.Time) int { // a year, or a year and a month: 202603
|
||||
if out.Unit == "year" {
|
||||
return t.Year() * 100
|
||||
}
|
||||
return t.Year()*100 + int(t.Month())
|
||||
}
|
||||
by := map[int]*sums{}
|
||||
first, last := math.MaxInt, 0
|
||||
for _, r := range rows {
|
||||
k := key(time.Unix(r.At, 0).In(loc))
|
||||
first, last = min(first, k), max(last, k)
|
||||
b := by[k]
|
||||
if b == nil {
|
||||
b = &sums{genre: make([]float64, len(names))}
|
||||
by[k] = b
|
||||
}
|
||||
b.total += r.Weight
|
||||
switch i, named := column[r.Genre]; {
|
||||
case r.Genre == "":
|
||||
b.untagged += r.Weight
|
||||
case named:
|
||||
b.genre[i] += r.Weight
|
||||
default:
|
||||
b.other += r.Weight
|
||||
}
|
||||
}
|
||||
for k := first; k <= last; {
|
||||
year, month := k/100, k%100
|
||||
bucket := TimelineBucket{Year: year, Month: month, Shares: make([]float64, len(names))}
|
||||
if b := by[k]; b != nil && b.total > 0 {
|
||||
bucket.Listens = int(math.Round(b.total))
|
||||
for i, w := range b.genre {
|
||||
bucket.Shares[i] = w / b.total
|
||||
}
|
||||
bucket.Other, bucket.Untagged = b.other/b.total, b.untagged/b.total
|
||||
}
|
||||
out.Buckets = append(out.Buckets, bucket)
|
||||
if out.Unit == "year" {
|
||||
k += 100
|
||||
} else if month == 12 {
|
||||
k = (year+1)*100 + 1
|
||||
} else {
|
||||
k++
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func nonNil(r []Ranked) []Ranked {
|
||||
if r == nil {
|
||||
return []Ranked{}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ type fakeStore struct {
|
|||
times []int64
|
||||
artists []Ranked
|
||||
songs []Ranked
|
||||
genreTime []GenreListen
|
||||
gotRange Range
|
||||
gotMinMs int64
|
||||
gotUserID int64
|
||||
|
|
@ -49,6 +50,10 @@ func (f *fakeStore) TopSongs(_ context.Context, u, m int64, r Range, _ int) ([]R
|
|||
f.note(u, m, r)
|
||||
return f.songs, nil
|
||||
}
|
||||
func (f *fakeStore) ListenGenreTimes(_ context.Context, u, m int64, r Range) ([]GenreListen, error) {
|
||||
f.note(u, m, r)
|
||||
return f.genreTime, nil
|
||||
}
|
||||
func (f *fakeStore) ListenGenres(_ context.Context, u, m int64, r Range) ([]GenreWeight, int, error) {
|
||||
f.note(u, m, r)
|
||||
return f.genres, f.tagged, nil
|
||||
|
|
@ -206,3 +211,121 @@ func TestARangeThatEndsBeforeItStartsIsRefused(t *testing.T) {
|
|||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func at(y int, m time.Month, d int) int64 { return time.Date(y, m, d, 12, 0, 0, 0, time.UTC).Unix() }
|
||||
|
||||
// one is a listen of a song with one genre; two is a listen of a song with two, each weighing half; none has no genre.
|
||||
func one(when int64, genre string) []GenreListen { return []GenreListen{{when, genre, 1}} }
|
||||
func two(when int64, a, b string) []GenreListen {
|
||||
return []GenreListen{{when, a, 0.5}, {when, b, 0.5}}
|
||||
}
|
||||
func none(when int64) []GenreListen { return []GenreListen{{when, "", 1}} }
|
||||
func join(parts ...[]GenreListen) []GenreListen {
|
||||
var out []GenreListen
|
||||
for _, p := range parts {
|
||||
out = append(out, p...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func timeline(t *testing.T, rows []GenreListen, loc *time.Location) Timeline {
|
||||
t.Helper()
|
||||
got, err := NewService(&fakeStore{genreTime: rows}, time.Second).Listening(context.Background(), 1, Range{}, loc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return got.GenreTimeline
|
||||
}
|
||||
|
||||
func TestGenresOverTimeAreSharesOfEachYearAndYearsWithNoListensAreEmpty(t *testing.T) {
|
||||
tl := timeline(t, join(
|
||||
one(at(2019, 3, 1), "rock"), one(at(2019, 4, 1), "rock"), one(at(2019, 5, 1), "jazz"), none(at(2019, 6, 1)),
|
||||
one(at(2021, 1, 1), "jazz"), two(at(2021, 2, 1), "jazz", "rock"),
|
||||
), time.UTC)
|
||||
if tl.Unit != "year" {
|
||||
t.Fatalf("unit = %q, two years of listens are shown by year", tl.Unit)
|
||||
}
|
||||
// rock: 2 + 0.5, jazz: 1 + 1.5 = 2.5 each; equal weights: by name.
|
||||
if len(tl.Genres) != 2 || tl.Genres[0] != "jazz" || tl.Genres[1] != "rock" {
|
||||
t.Fatalf("genres = %v", tl.Genres)
|
||||
}
|
||||
if len(tl.Buckets) != 3 || tl.Buckets[0].Year != 2019 || tl.Buckets[1].Year != 2020 || tl.Buckets[2].Year != 2021 {
|
||||
t.Fatalf("buckets = %+v", tl.Buckets)
|
||||
}
|
||||
b := tl.Buckets[0] // 4 listens: 2 rock, 1 jazz, 1 without a genre
|
||||
if b.Listens != 4 || !near(b.Shares[0], 0.25) || !near(b.Shares[1], 0.5) || !near(b.Other, 0) || !near(b.Untagged, 0.25) {
|
||||
t.Errorf("2019 = %+v", b)
|
||||
}
|
||||
if e := tl.Buckets[1]; e.Listens != 0 || e.Shares[0] != 0 || e.Shares[1] != 0 || e.Other != 0 || e.Untagged != 0 {
|
||||
t.Errorf("2020 = %+v, want empty", e)
|
||||
}
|
||||
if c := tl.Buckets[2]; c.Listens != 2 || !near(c.Shares[0], 0.75) || !near(c.Shares[1], 0.25) {
|
||||
t.Errorf("2021 = %+v (a listen of two genres is split)", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneYearOfListensIsShownByMonthWithMonthsInBetween(t *testing.T) {
|
||||
tl := timeline(t, join(one(at(2025, 1, 10), "rock"), one(at(2025, 4, 10), "rock"), one(at(2025, 4, 11), "jazz")), time.UTC)
|
||||
if tl.Unit != "month" || len(tl.Buckets) != 4 {
|
||||
t.Fatalf("unit %q, buckets %+v", tl.Unit, tl.Buckets)
|
||||
}
|
||||
for i, m := range []int{1, 2, 3, 4} {
|
||||
if tl.Buckets[i].Year != 2025 || tl.Buckets[i].Month != m {
|
||||
t.Errorf("bucket %d = %d-%d", i, tl.Buckets[i].Year, tl.Buckets[i].Month)
|
||||
}
|
||||
}
|
||||
if tl.Buckets[1].Listens != 0 || tl.Buckets[3].Listens != 2 || !near(tl.Buckets[3].Shares[0], 0.5) {
|
||||
t.Errorf("%+v", tl.Buckets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListensInTwoCalendarYearsAreShownByYearEvenWhenCloseTogether(t *testing.T) {
|
||||
tl := timeline(t, join(one(at(2025, 11, 10), "rock"), one(at(2026, 1, 10), "rock")), time.UTC)
|
||||
// Two calendar years: by year. The month view is for the listens of a single year.
|
||||
if tl.Unit != "year" || len(tl.Buckets) != 2 {
|
||||
t.Fatalf("%q %+v", tl.Unit, tl.Buckets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenresOverTimeAreCountedInTheZoneOfTheListener(t *testing.T) {
|
||||
rome, err := time.LoadLocation("Europe/Rome")
|
||||
if err != nil {
|
||||
t.Skip("no tzdata")
|
||||
}
|
||||
// 2025-12-31 23:30 UTC is 2026-01-01 00:30 in Rome.
|
||||
tl := timeline(t, one(time.Date(2025, 12, 31, 23, 30, 0, 0, time.UTC).Unix(), "rock"), rome)
|
||||
if tl.Unit != "month" || len(tl.Buckets) != 1 || tl.Buckets[0].Year != 2026 || tl.Buckets[0].Month != 1 {
|
||||
t.Errorf("%q %+v", tl.Unit, tl.Buckets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyTheFiveBiggestGenresAreNamedAndTheRestAreOther(t *testing.T) {
|
||||
var rows []GenreListen
|
||||
for i, g := range []string{"a", "b", "c", "d", "e", "f", "g"} {
|
||||
for n := 0; n <= 6-i; n++ { // a: 7 listens ... g: 1
|
||||
rows = append(rows, one(at(2025, 3, 1+n), g)...)
|
||||
}
|
||||
}
|
||||
tl := timeline(t, rows, time.UTC)
|
||||
if len(tl.Genres) != 5 || tl.Genres[0] != "a" || tl.Genres[4] != "e" {
|
||||
t.Fatalf("genres = %v", tl.Genres)
|
||||
}
|
||||
b := tl.Buckets[0]
|
||||
if b.Listens != 28 || !near(b.Other, 3.0/28) { // f (2) + g (1)
|
||||
t.Errorf("%+v", b)
|
||||
}
|
||||
sum := b.Other + b.Untagged
|
||||
for _, s := range b.Shares {
|
||||
sum += s
|
||||
}
|
||||
if !near(sum, 1) {
|
||||
t.Errorf("shares add up to %v", sum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoListensHaveNoTimeline(t *testing.T) {
|
||||
tl := timeline(t, nil, time.UTC)
|
||||
if tl.Genres == nil || tl.Buckets == nil || len(tl.Genres) != 0 || len(tl.Buckets) != 0 || tl.Unit != "year" {
|
||||
t.Errorf("%+v", tl)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package storage
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
|
|
@ -254,3 +255,32 @@ func (r *StatsRepo) ListenGenres(ctx context.Context, userID, minMs int64, rng s
|
|||
sort.Slice(out, func(i, j int) bool { return out[i].Genre < out[j].Genre })
|
||||
return out, tagged, nil
|
||||
}
|
||||
|
||||
// ListenGenreTimes gives every listen with the genres of its song: one row for each genre, weighing 1/n for a song with
|
||||
// n, or one row with no genre for a song that has none. The listens of a year or a month, and the share of each genre in
|
||||
// them, are worked out by the caller in the time zone of the listener.
|
||||
func (r *StatsRepo) ListenGenreTimes(ctx context.Context, userID, minMs int64, rng stats.Range) ([]stats.GenreListen, error) {
|
||||
where, args := listensWhere(userID, minMs, rng)
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
"SELECT e.occurred_at, g.genre, 1.0 / c.n FROM listening_events e LEFT JOIN track_genres g ON g.track_id = e.track_id "+
|
||||
"LEFT JOIN "+genresPerTrack+" c ON c.track_id = e.track_id WHERE "+where, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read the genres of the listens: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []stats.GenreListen
|
||||
for rows.Next() {
|
||||
var at int64
|
||||
var genre sql.NullString
|
||||
var weight sql.NullFloat64
|
||||
if err := rows.Scan(&at, &genre, &weight); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g := stats.GenreListen{At: at, Genre: genre.String, Weight: 1}
|
||||
if genre.Valid {
|
||||
g.Weight = weight.Float64
|
||||
}
|
||||
out = append(out, g)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,3 +239,49 @@ func TestTheStatsOfAPersonWithNoListensAreEmpty(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestEachListenCarriesTheGenresOfItsSongAndTheyWeighOne(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
w := seedStats(t, db)
|
||||
lib, _ := NewLibraryRepo(db).ListLibraries(w.ctx)
|
||||
bare := affinitySong(t, db, lib[0].ID, "e.flac", "Untitled", "Nobody") // a song with no genre
|
||||
if _, err := w.listens.Record(w.ctx, listening.Event{UserID: w.alice, TrackID: bare, Type: listening.Complete, OccurredAt: w.jun.Add(time.Hour), DurationPlayedMs: 100_000, IdempotencyKey: "bare"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rows, err := w.repo.ListenGenreTimes(w.ctx, w.alice, 20_000, stats.Range{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Debaser three times with two genres (6 rows), Where Is My Mind, Song 2 and the song with no genre (1 row each).
|
||||
if len(rows) != 9 {
|
||||
t.Fatalf("%d rows: %+v", len(rows), rows)
|
||||
}
|
||||
perListen := map[int64]float64{}
|
||||
genres := map[string]float64{}
|
||||
for _, r := range rows {
|
||||
perListen[r.At] += r.Weight
|
||||
genres[r.Genre] += r.Weight
|
||||
}
|
||||
if len(perListen) != 6 {
|
||||
t.Errorf("%d listens, want 6", len(perListen))
|
||||
}
|
||||
for at, sum := range perListen {
|
||||
if sum != 1 {
|
||||
t.Errorf("the listen at %d weighs %v, want 1", at, sum)
|
||||
}
|
||||
}
|
||||
if genres["rock"] != 2.5 || genres["alternative"] != 1.5 || genres["britpop"] != 1 || genres[""] != 1 {
|
||||
t.Errorf("genres = %v (the empty one is the song with no genre)", genres)
|
||||
}
|
||||
|
||||
year := stats.Range{From: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), Until: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}
|
||||
if cut, _ := w.repo.ListenGenreTimes(w.ctx, w.alice, 20_000, year); len(cut) != 7 { // not the listen of 2026 (2 rows), nor bob's
|
||||
t.Errorf("2025 has %d rows, want 7", len(cut))
|
||||
}
|
||||
bob, _ := w.repo.ListenGenreTimes(w.ctx, w.bob, 20_000, stats.Range{})
|
||||
if len(bob) != 2 {
|
||||
t.Errorf("bob has %d rows, want the 2 of one listen", len(bob))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue