- 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
165 lines
6.8 KiB
Go
165 lines
6.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/stats"
|
|
)
|
|
|
|
type fakeStats struct {
|
|
library stats.Library
|
|
listening stats.Listening
|
|
err error
|
|
user int64
|
|
rng stats.Range
|
|
loc *time.Location
|
|
}
|
|
|
|
func (f *fakeStats) Library(context.Context) (stats.Library, error) { return f.library, f.err }
|
|
func (f *fakeStats) Listening(_ context.Context, user int64, r stats.Range, loc *time.Location) (stats.Listening, error) {
|
|
f.user, f.rng, f.loc = user, r, loc
|
|
return f.listening, f.err
|
|
}
|
|
|
|
func statsAPI(t *testing.T, f *fakeStats) (http.Handler, string) {
|
|
t.Helper()
|
|
h := newTestAPIWith(t, func(d *Deps) { d.Stats = f })
|
|
return h, registerAndLogin(t, h)
|
|
}
|
|
|
|
func TestStatsAreForSignedInUsers(t *testing.T) {
|
|
h, _ := statsAPI(t, &fakeStats{})
|
|
for _, p := range []string{"/api/v1/stats/library", "/api/v1/stats/listening"} {
|
|
if r := do(t, h, "GET", p, "", nil); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s without a token: %d", p, r.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTheLibraryStatsAreAnswered(t *testing.T) {
|
|
f := &fakeStats{library: stats.Library{
|
|
Totals: stats.Totals{Artists: 2, Albums: 3, Songs: 40, DurationMs: 9_000_000, SizeBytes: 123456},
|
|
Formats: []stats.Format{{Format: "flac", Files: 30, Bytes: 100000}},
|
|
Genres: stats.Genres{Top: []stats.Share{{Name: "rock", Count: 20, Share: 0.5}}, Other: 0.25, Untagged: 10, UntaggedShare: 0.25},
|
|
Decades: []stats.Decade{{Decade: 1990, Songs: 12}},
|
|
NoYear: 4,
|
|
}}
|
|
h, tok := statsAPI(t, f)
|
|
r := do(t, h, "GET", "/api/v1/stats/library", tok, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %v", r.Code, r.Body)
|
|
}
|
|
if r.Body["songs"] != 40.0 || r.Body["albums"] != 3.0 || r.Body["artists"] != 2.0 || r.Body["duration_ms"] != 9000000.0 || r.Body["size_bytes"] != 123456.0 || r.Body["no_year"] != 4.0 {
|
|
t.Errorf("body = %v", r.Body)
|
|
}
|
|
formats, _ := r.Body["formats"].([]any)
|
|
if len(formats) != 1 || formats[0].(map[string]any)["format"] != "flac" || formats[0].(map[string]any)["bytes"] != 100000.0 {
|
|
t.Errorf("formats = %v", r.Body["formats"])
|
|
}
|
|
g := sub(r.Body, "genres")
|
|
top, _ := g["top"].([]any)
|
|
if len(top) != 1 || top[0].(map[string]any)["name"] != "rock" || top[0].(map[string]any)["share"] != 0.5 || g["other"] != 0.25 || g["untagged"] != 10.0 || g["untagged_share"] != 0.25 {
|
|
t.Errorf("genres = %v", g)
|
|
}
|
|
decades, _ := r.Body["decades"].([]any)
|
|
if len(decades) != 1 || decades[0].(map[string]any)["decade"] != 1990.0 || decades[0].(map[string]any)["songs"] != 12.0 {
|
|
t.Errorf("decades = %v", r.Body["decades"])
|
|
}
|
|
}
|
|
|
|
func TestTheListeningStatsAreAskedForThePersonAndThePeriod(t *testing.T) {
|
|
first := time.Date(2020, 5, 1, 10, 0, 0, 0, time.UTC)
|
|
f := &fakeStats{listening: stats.Listening{
|
|
Listens: 5, Songs: 3, Artists: 2, PlayedMs: 900_000, First: first,
|
|
Years: []stats.Bucket{{Key: 2020, Count: 5}},
|
|
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
|
|
h, tok := statsAPI(t, f)
|
|
r := do(t, h, "GET", "/api/v1/stats/listening?from=2025-01-01T00:00:00Z&until=2026-01-01T00:00:00Z&tz=Europe/Rome", tok, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %v", r.Code, r.Body)
|
|
}
|
|
if f.user == 0 || !f.rng.From.Equal(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)) || !f.rng.Until.Equal(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)) {
|
|
t.Errorf("asked for user %d, %+v", f.user, f.rng)
|
|
}
|
|
if f.loc == nil || f.loc.String() != "Europe/Rome" {
|
|
t.Errorf("zone = %v", f.loc)
|
|
}
|
|
b := r.Body
|
|
if b["listens"] != 5.0 || b["songs"] != 3.0 || b["artists"] != 2.0 || b["played_ms"] != 900000.0 || b["first"] != "2020-05-01T10:00:00Z" {
|
|
t.Errorf("body = %v", b)
|
|
}
|
|
hours, _ := b["hours"].([]any)
|
|
weekdays, _ := b["weekdays"].([]any)
|
|
if len(hours) != 24 || hours[21] != 5.0 || len(weekdays) != 7 || weekdays[2] != 5.0 {
|
|
t.Errorf("hours %v weekdays %v", b["hours"], b["weekdays"])
|
|
}
|
|
years, _ := b["years"].([]any)
|
|
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 {
|
|
t.Errorf("artists = %v", b["top_artists"])
|
|
}
|
|
s0, _ := songs[0].(map[string]any)
|
|
if s0["title"] != "Song 2" || s0["listens"] != 2.0 || s0["id"] != 9.0 || s0["artists"].([]any)[0] != "Blur" {
|
|
t.Errorf("songs = %v", b["top_songs"])
|
|
}
|
|
}
|
|
|
|
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{}}, 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 {
|
|
t.Fatalf("%d %v", r.Code, r.Body)
|
|
}
|
|
if _, ok := r.Body["first"]; ok {
|
|
t.Errorf("first should be left out: %v", r.Body)
|
|
}
|
|
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"])
|
|
}
|
|
}
|
|
|
|
func TestListeningStatsRefuseABadQuery(t *testing.T) {
|
|
h, tok := statsAPI(t, &fakeStats{})
|
|
for _, q := range []string{"tz=Mars/Base", "from=yesterday", "until=2026", "from=2026-01-01T00:00:00Z&until=2025-01-01T00:00:00Z"} {
|
|
r := do(t, h, "GET", "/api/v1/stats/listening?"+q, tok, nil)
|
|
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Errorf("%s: %d %v", q, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|