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
6 changed files with 586 additions and 8 deletions
22
web/src/api/schema.d.ts
vendored
22
web/src/api/schema.d.ts
vendored
|
|
@ -3563,6 +3563,28 @@ export interface components {
|
|||
listens: number;
|
||||
}[];
|
||||
genres: components["schemas"]["GenreShares"];
|
||||
/**
|
||||
* @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`.
|
||||
*/
|
||||
genre_timeline: {
|
||||
/** @enum {string} */
|
||||
unit: "year" | "month";
|
||||
/** @description The five biggest genres, biggest first. Each bucket has a share for each, in this order. */
|
||||
genres: string[];
|
||||
buckets: {
|
||||
year: number;
|
||||
/** @description Left out when `unit` is `year`. */
|
||||
month?: number;
|
||||
listens: number;
|
||||
shares: number[];
|
||||
/** @description The share of the smaller genres. */
|
||||
other: number;
|
||||
/** @description The share with no genre. */
|
||||
untagged: number;
|
||||
}[];
|
||||
};
|
||||
};
|
||||
ListeningSuggestions: {
|
||||
library: components["schemas"]["ListeningSuggestion"][];
|
||||
|
|
|
|||
|
|
@ -58,3 +58,38 @@ export function Columns({ label, columns, className }: { label: string; columns:
|
|||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
export interface StackedColumn {
|
||||
key: string
|
||||
/** What is shown under the column. */
|
||||
label: string
|
||||
/** What a screen reader reads for the column. */
|
||||
name: string
|
||||
/** The parts of the whole, from the bottom up, each from 0 to 1. A column of no parts is empty. */
|
||||
parts: number[]
|
||||
}
|
||||
|
||||
const STACK_HEIGHT = 144
|
||||
|
||||
/**
|
||||
* Columns that are each the whole, cut in parts: how the share of each genre changed from one year to the next. The
|
||||
* colours are given by index, the same as in the legend; a part that is 0 is not drawn.
|
||||
*/
|
||||
export function StackedColumns({ label, columns, colors }: { label: string; columns: StackedColumn[]; colors: string[] }) {
|
||||
return (
|
||||
<ul aria-label={label} className="flex items-end gap-1">
|
||||
{columns.map((c) => (
|
||||
<li key={c.key} aria-label={c.name} title={c.name} className="flex min-w-0 flex-1 flex-col items-center justify-end gap-1">
|
||||
<span aria-hidden="true" className="flex w-full flex-col-reverse overflow-hidden rounded-sm bg-card" style={{ height: `${STACK_HEIGHT}px` }}>
|
||||
{c.parts.map((p, i) =>
|
||||
p > 0 ? <span key={i} className="block w-full border-t border-background first:border-t-0" style={{ height: `${p * 100}%`, background: colors[i] }} /> : null,
|
||||
)}
|
||||
</span>
|
||||
<span aria-hidden="true" className="h-4 text-[13px] leading-4 tabular-nums text-muted-foreground">
|
||||
{c.label}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1127,6 +1127,9 @@
|
|||
"songsIn_other": "{{label}}: {{n}} songs",
|
||||
"listensIn_one": "{{label}}: {{n}} listen",
|
||||
"listensIn_other": "{{label}}: {{n}} listens",
|
||||
"decadeShort": "{{short}}s"
|
||||
"decadeShort": "{{short}}s",
|
||||
"overTime": "Genres over time",
|
||||
"timelineNone": "{{label}}: no listens",
|
||||
"timelineIn": "{{label}}: {{parts}}"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1127,6 +1127,9 @@
|
|||
"songsIn_other": "{{label}}: {{n}} brani",
|
||||
"listensIn_one": "{{label}}: {{n}} ascolto",
|
||||
"listensIn_other": "{{label}}: {{n}} ascolti",
|
||||
"decadeShort": "{{short}}"
|
||||
"decadeShort": "{{short}}",
|
||||
"overTime": "Generi nel tempo",
|
||||
"timelineNone": "{{label}}: nessun ascolto",
|
||||
"timelineIn": "{{label}}: {{parts}}"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,15 @@ const listening = {
|
|||
],
|
||||
top_songs: [{ id: 5, title: 'Karma Police', artists: ['Radiohead'], listens: 33 }],
|
||||
genres: { top: [{ name: 'rock', count: 700, share: 0.7 }], other: 0.1, untagged: 200, untagged_share: 0.2 },
|
||||
genre_timeline: {
|
||||
unit: 'year',
|
||||
genres: ['rock', 'jazz'],
|
||||
buckets: [
|
||||
{ year: 2019, listens: 100, shares: [0.6, 0.2], other: 0.1, untagged: 0.1 },
|
||||
{ year: 2020, listens: 0, shares: [0, 0], other: 0, untagged: 0 },
|
||||
{ year: 2021, listens: 400, shares: [0.5, 0.3], other: 0.2, untagged: 0 },
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
const none = {
|
||||
|
|
@ -74,6 +83,7 @@ const none = {
|
|||
top_artists: [],
|
||||
top_songs: [],
|
||||
genres: { top: [], other: 0, untagged: 0, untagged_share: 0 },
|
||||
genre_timeline: { unit: 'year', genres: [], buckets: [] },
|
||||
}
|
||||
|
||||
function setup(routes: MockRoutes = {}) {
|
||||
|
|
@ -168,6 +178,54 @@ test('one year has no chart by year: there is nothing to compare', async () => {
|
|||
expect(within(l).queryByRole('list', { name: 'Listens by year' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the genres over time are columns by year, each said in words, with the parts that are there', async () => {
|
||||
setup()
|
||||
const l = await section('Your listening')
|
||||
const chart = within(l).getByRole('list', { name: 'Genres over time' })
|
||||
const items = within(chart).getAllByRole('listitem')
|
||||
expect(items.map((li) => li.getAttribute('aria-label'))).toEqual([
|
||||
'2019: rock 60%, jazz 20%, Smaller genres 10%, No genre 10%',
|
||||
'2020: no listens',
|
||||
'2021: rock 50%, jazz 30%, Smaller genres 20%',
|
||||
])
|
||||
expect(items.map((li) => li.textContent)).toEqual(['2019', '2020', '2021'])
|
||||
})
|
||||
|
||||
test('the chart has a legend of the genres it names, the smaller ones and what has no genre', async () => {
|
||||
setup()
|
||||
const l = await section('Your listening')
|
||||
const legend = within(l).getByTestId('timeline-legend')
|
||||
expect(within(legend).getAllByRole('listitem').map((li) => li.textContent)).toEqual(['rock', 'jazz', 'Smaller genres', 'No genre'])
|
||||
})
|
||||
|
||||
test('listens in a single year are shown by month', async () => {
|
||||
setup({
|
||||
'GET /api/v1/stats/listening': () => ({
|
||||
...listening,
|
||||
genre_timeline: {
|
||||
unit: 'month',
|
||||
genres: ['rock'],
|
||||
buckets: [
|
||||
{ year: 2025, month: 3, listens: 4, shares: [1], other: 0, untagged: 0 },
|
||||
{ year: 2025, month: 4, listens: 2, shares: [0.5], other: 0.5, untagged: 0 },
|
||||
],
|
||||
},
|
||||
}),
|
||||
})
|
||||
const l = await section('Your listening')
|
||||
const items = within(within(l).getByRole('list', { name: 'Genres over time' })).getAllByRole('listitem')
|
||||
expect(items.map((li) => li.getAttribute('aria-label'))).toEqual(['March 2025: rock 100%', 'April 2025: rock 50%, Smaller genres 50%'])
|
||||
expect(items.map((li) => li.textContent)).toEqual(['M', 'A'])
|
||||
})
|
||||
|
||||
test('one column is not a change over time: the chart is left out', async () => {
|
||||
setup({
|
||||
'GET /api/v1/stats/listening': () => ({ ...listening, genre_timeline: { unit: 'year', genres: ['rock'], buckets: [{ year: 2021, listens: 400, shares: [1], other: 0, untagged: 0 }] } }),
|
||||
})
|
||||
const l = await section('Your listening')
|
||||
expect(within(l).queryByRole('list', { name: 'Genres over time' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the most listened artists link to their page, and the songs name their artists', async () => {
|
||||
setup()
|
||||
const l = await section('Your listening')
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { PERIODS, useLibraryStats, useListeningStats, type GenreShares, type Period } from '@/api/stats'
|
||||
import { Columns, ShareBars, type Bar } from '@/components/StatBars'
|
||||
import { PERIODS, useLibraryStats, useListeningStats, type GenreShares, type ListeningStats, type Period } from '@/api/stats'
|
||||
import { Columns, ShareBars, StackedColumns, type Bar } from '@/components/StatBars'
|
||||
import PageTitle from '@/components/PageTitle'
|
||||
import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
|
@ -59,6 +59,55 @@ function GenreBars({ label, genres }: { label: string; genres: GenreShares }) {
|
|||
return <ShareBars label={label} bars={bars} />
|
||||
}
|
||||
|
||||
/**
|
||||
* The colours of the parts of a column of genres, from the biggest genre on: the colour of this server, thinner and
|
||||
* thinner towards the background, so that the order of the legend is the order of the stack in every theme. Then the
|
||||
* smaller genres and what has no genre, in greys.
|
||||
*/
|
||||
const TINTS = [100, 82, 66, 52, 42]
|
||||
const tint = (i: number) => `color-mix(in oklab, var(--primary) ${TINTS[Math.min(i, TINTS.length - 1)]}%, var(--background))`
|
||||
const OTHER_COLOR = 'color-mix(in oklab, var(--muted-foreground) 65%, var(--background))'
|
||||
const NONE_COLOR = 'var(--muted)'
|
||||
|
||||
/** How the genres changed over time: a column for each year (or month), each genre's share of it, and the legend. */
|
||||
function GenreTimeline({ timeline }: { timeline: ListeningStats['genre_timeline'] }) {
|
||||
const { t } = useTranslation()
|
||||
const f = useFormat()
|
||||
if (timeline.buckets.length < 2) return null
|
||||
const withOther = timeline.buckets.some((b) => b.other > 0)
|
||||
const withNone = timeline.buckets.some((b) => b.untagged > 0)
|
||||
const names = [...timeline.genres, ...(withOther ? [t('stats.other')] : []), ...(withNone ? [t('stats.noGenre')] : [])]
|
||||
const colors = [...timeline.genres.map((_, i) => tint(i)), ...(withOther ? [OTHER_COLOR] : []), ...(withNone ? [NONE_COLOR] : [])]
|
||||
const monthShort = new Intl.DateTimeFormat(f.lang, { month: 'narrow' })
|
||||
const monthLong = new Intl.DateTimeFormat(f.lang, { month: 'long', year: 'numeric' })
|
||||
const many = timeline.buckets.length > 8
|
||||
const columns = timeline.buckets.map((b) => {
|
||||
const parts = [...b.shares, ...(withOther ? [b.other] : []), ...(withNone ? [b.untagged] : [])]
|
||||
const date = new Date(b.year, (b.month ?? 1) - 1, 1)
|
||||
const label = timeline.unit === 'month' ? monthLong.format(date) : String(b.year)
|
||||
const said = parts.flatMap((p, i) => (p > 0 ? [`${names[i]} ${f.percent(p)}`] : []))
|
||||
return {
|
||||
key: `${b.year}-${b.month ?? 0}`,
|
||||
label: timeline.unit === 'month' ? monthShort.format(date) : many ? String(b.year).slice(2) : String(b.year),
|
||||
name: b.listens === 0 ? t('stats.timelineNone', { label }) : t('stats.timelineIn', { label, parts: said.join(', ') }),
|
||||
parts,
|
||||
}
|
||||
})
|
||||
return (
|
||||
<Block title={t('stats.overTime')}>
|
||||
<StackedColumns label={t('stats.overTime')} columns={columns} colors={colors} />
|
||||
<ul data-testid="timeline-legend" className="flex flex-wrap gap-x-5 gap-y-1 text-sm">
|
||||
{names.map((n, i) => (
|
||||
<li key={n} className="flex items-center gap-2">
|
||||
<span aria-hidden="true" className="size-3 rounded-sm" style={{ background: colors[i] }} />
|
||||
<span className="first-letter:uppercase">{n}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Block>
|
||||
)
|
||||
}
|
||||
|
||||
function LibrarySection() {
|
||||
const { t } = useTranslation()
|
||||
const f = useFormat()
|
||||
|
|
@ -204,6 +253,7 @@ function ListeningSection() {
|
|||
<GenreBars label={t('stats.yourGenres')} genres={l.genres} />
|
||||
</Block>
|
||||
)}
|
||||
<GenreTimeline timeline={l.genre_timeline} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue