- 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
331 lines
12 KiB
Go
331 lines
12 KiB
Go
package stats
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type fakeStore struct {
|
|
totals Totals
|
|
formats []FormatCount
|
|
genres []GenreWeight
|
|
tagged int
|
|
years []YearCount
|
|
listen ListenTotals
|
|
times []int64
|
|
artists []Ranked
|
|
songs []Ranked
|
|
genreTime []GenreListen
|
|
gotRange Range
|
|
gotMinMs int64
|
|
gotUserID int64
|
|
}
|
|
|
|
func (f *fakeStore) Totals(context.Context) (Totals, error) { return f.totals, nil }
|
|
func (f *fakeStore) Formats(context.Context) ([]FormatCount, error) { return f.formats, nil }
|
|
func (f *fakeStore) Years(context.Context) ([]YearCount, error) { return f.years, nil }
|
|
func (f *fakeStore) Genres(context.Context) ([]GenreWeight, int, error) {
|
|
return f.genres, f.tagged, nil
|
|
}
|
|
func (f *fakeStore) note(userID, minMs int64, r Range) {
|
|
f.gotUserID, f.gotMinMs, f.gotRange = userID, minMs, r
|
|
}
|
|
func (f *fakeStore) ListenTotals(_ context.Context, u, m int64, r Range) (ListenTotals, error) {
|
|
f.note(u, m, r)
|
|
return f.listen, nil
|
|
}
|
|
func (f *fakeStore) ListenTimes(_ context.Context, u, m int64, r Range) ([]int64, error) {
|
|
f.note(u, m, r)
|
|
return f.times, nil
|
|
}
|
|
func (f *fakeStore) TopArtists(_ context.Context, u, m int64, r Range, _ int) ([]Ranked, error) {
|
|
f.note(u, m, r)
|
|
return f.artists, nil
|
|
}
|
|
func (f *fakeStore) TopSongs(_ context.Context, u, m int64, r Range, _ int) ([]Ranked, error) {
|
|
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
|
|
}
|
|
|
|
func near(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
|
|
|
func TestLibraryGenresAreTheTenBiggestAndTheRest(t *testing.T) {
|
|
var genres []GenreWeight
|
|
for i := 0; i < 12; i++ {
|
|
genres = append(genres, GenreWeight{Genre: fmt.Sprintf("g%02d", i), Songs: 12 - i, Weight: float64(12 - i)})
|
|
}
|
|
// 78 weight in all, 22 songs without a genre: 100 songs.
|
|
s := NewService(&fakeStore{totals: Totals{Songs: 100}, genres: genres, tagged: 78}, 20*time.Second)
|
|
lib, err := s.Library(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := lib.Genres
|
|
if len(g.Top) != 10 || g.Top[0].Name != "g00" || g.Top[9].Name != "g09" {
|
|
t.Fatalf("top = %+v", g.Top)
|
|
}
|
|
if !near(g.Top[0].Share, 0.12) || g.Top[0].Count != 12 {
|
|
t.Errorf("first = %+v", g.Top[0])
|
|
}
|
|
if !near(g.Other, 0.03) { // g10 (2) + g11 (1) of 100
|
|
t.Errorf("other = %v", g.Other)
|
|
}
|
|
if g.Untagged != 22 || !near(g.UntaggedShare, 0.22) {
|
|
t.Errorf("untagged = %d, %v", g.Untagged, g.UntaggedShare)
|
|
}
|
|
}
|
|
|
|
func TestSharesOfAGenreThatSharesItsSongsAddUpToTheTaggedSongs(t *testing.T) {
|
|
// One song with two genres weighs half for each: 1 song in all.
|
|
s := NewService(&fakeStore{totals: Totals{Songs: 1}, genres: []GenreWeight{{"rock", 1, 0.5}, {"blues", 1, 0.5}}, tagged: 1}, time.Second)
|
|
lib, _ := s.Library(context.Background())
|
|
if len(lib.Genres.Top) != 2 || !near(lib.Genres.Top[0].Share, 0.5) || !near(lib.Genres.Top[1].Share, 0.5) {
|
|
t.Errorf("%+v", lib.Genres)
|
|
}
|
|
if lib.Genres.Top[0].Name != "blues" { // equal weights: by name
|
|
t.Errorf("order: %+v", lib.Genres.Top)
|
|
}
|
|
if lib.Genres.Untagged != 0 || lib.Genres.Other != 0 {
|
|
t.Errorf("%+v", lib.Genres)
|
|
}
|
|
}
|
|
|
|
func TestAnEmptyLibraryHasNoSharesAndNoNaN(t *testing.T) {
|
|
lib, err := NewService(&fakeStore{}, time.Second).Library(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if lib.Songs != 0 || len(lib.Genres.Top) != 0 || lib.Genres.Other != 0 || lib.Genres.UntaggedShare != 0 {
|
|
t.Errorf("%+v", lib)
|
|
}
|
|
if lib.Formats == nil || lib.Decades == nil || lib.Genres.Top == nil {
|
|
t.Errorf("lists must be empty, not null: %+v", lib)
|
|
}
|
|
}
|
|
|
|
func TestDecadesAreCountedFromTheYearsAndTheRestHaveNone(t *testing.T) {
|
|
s := NewService(&fakeStore{
|
|
totals: Totals{Songs: 10},
|
|
years: []YearCount{{1994, 2}, {1999, 3}, {2003, 1}, {0, 9}, {12, 1}},
|
|
}, time.Second)
|
|
lib, _ := s.Library(context.Background())
|
|
want := []Decade{{1990, 5}, {2000, 1}}
|
|
if len(lib.Decades) != 2 || lib.Decades[0] != want[0] || lib.Decades[1] != want[1] {
|
|
t.Errorf("decades = %+v", lib.Decades)
|
|
}
|
|
if lib.NoYear != 4 { // 10 songs, 6 with a believable year
|
|
t.Errorf("no year = %d", lib.NoYear)
|
|
}
|
|
}
|
|
|
|
func TestListeningIsCountedInTheZoneOfTheListener(t *testing.T) {
|
|
rome, err := time.LoadLocation("Europe/Rome")
|
|
if err != nil {
|
|
t.Skip("no tzdata")
|
|
}
|
|
// 2025-12-31 23:30 UTC is already 2026-01-01 00:30 in Rome, a Thursday.
|
|
at := time.Date(2025, 12, 31, 23, 30, 0, 0, time.UTC).Unix()
|
|
st := &fakeStore{listen: ListenTotals{Listens: 1, First: at}, times: []int64{at}}
|
|
got, err := NewService(st, 20*time.Second).Listening(context.Background(), 7, Range{}, rome)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Years) != 1 || got.Years[0].Key != 2026 || got.Years[0].Count != 1 {
|
|
t.Errorf("years = %+v", got.Years)
|
|
}
|
|
if got.Hours[0] != 1 || got.Weekdays[3] != 1 {
|
|
t.Errorf("hours %v weekdays %v", got.Hours, got.Weekdays)
|
|
}
|
|
utc, _ := NewService(st, 20*time.Second).Listening(context.Background(), 7, Range{}, nil)
|
|
if utc.Years[0].Key != 2025 || utc.Hours[23] != 1 || utc.Weekdays[2] != 1 { // Wednesday
|
|
t.Errorf("utc: years %+v hours %v weekdays %v", utc.Years, utc.Hours, utc.Weekdays)
|
|
}
|
|
if st.gotUserID != 7 || st.gotMinMs != 20000 {
|
|
t.Errorf("asked for user %d, min %d", st.gotUserID, st.gotMinMs)
|
|
}
|
|
}
|
|
|
|
func TestYearsWithoutListensBetweenTwoYearsAreShownAsZero(t *testing.T) {
|
|
y := func(year int) int64 { return time.Date(year, 6, 1, 12, 0, 0, 0, time.UTC).Unix() }
|
|
st := &fakeStore{listen: ListenTotals{Listens: 3, First: y(2019)}, times: []int64{y(2019), y(2019), y(2022)}}
|
|
got, _ := NewService(st, time.Second).Listening(context.Background(), 1, Range{}, time.UTC)
|
|
want := []Bucket{{2019, 2}, {2020, 0}, {2021, 0}, {2022, 1}}
|
|
if len(got.Years) != len(want) {
|
|
t.Fatalf("years = %+v", got.Years)
|
|
}
|
|
for i := range want {
|
|
if got.Years[i] != want[i] {
|
|
t.Errorf("years[%d] = %+v, want %+v", i, got.Years[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListeningTotalsAndRankingsPassThrough(t *testing.T) {
|
|
first := time.Date(2020, 5, 1, 10, 0, 0, 0, time.UTC)
|
|
st := &fakeStore{
|
|
listen: ListenTotals{Listens: 5, PlayedMs: 900_000, Songs: 3, Artists: 2, First: first.Unix()},
|
|
artists: []Ranked{{ID: 1, Name: "Blur", Listens: 3}},
|
|
songs: []Ranked{{ID: 9, Name: "Song 2", Artists: []string{"Blur"}, Listens: 2}},
|
|
genres: []GenreWeight{{"rock", 5, 5}}, tagged: 5,
|
|
}
|
|
got, err := NewService(st, time.Second).Listening(context.Background(), 1, Range{}, time.UTC)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Listens != 5 || got.PlayedMs != 900_000 || got.Songs != 3 || got.Artists != 2 || !got.First.Equal(first) {
|
|
t.Errorf("%+v", got)
|
|
}
|
|
if len(got.TopArtists) != 1 || got.TopArtists[0].Name != "Blur" || len(got.TopSongs) != 1 {
|
|
t.Errorf("rankings: %+v %+v", got.TopArtists, got.TopSongs)
|
|
}
|
|
if len(got.Genres.Top) != 1 || !near(got.Genres.Top[0].Share, 1) {
|
|
t.Errorf("genres: %+v", got.Genres)
|
|
}
|
|
}
|
|
|
|
func TestNoListensHaveNoFirstDayAndEmptyLists(t *testing.T) {
|
|
got, err := NewService(&fakeStore{}, time.Second).Listening(context.Background(), 1, Range{}, time.UTC)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !got.First.IsZero() || got.Years == nil || got.TopArtists == nil || got.TopSongs == nil || got.Genres.Top == nil {
|
|
t.Errorf("%+v", got)
|
|
}
|
|
}
|
|
|
|
func TestARangeThatEndsBeforeItStartsIsRefused(t *testing.T) {
|
|
r := Range{From: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), Until: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}
|
|
if _, err := NewService(&fakeStore{}, time.Second).Listening(context.Background(), 1, r, time.UTC); !errors.Is(err, ErrInvalidRange) {
|
|
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)
|
|
}
|
|
}
|