Rewrites the web app's comments to rule 32. Task, milestone, rule and spec ids are gone; regression stories are restated as the trap they guard (public auth routes, the menu's stopPropagation, universal loads under adapter-static, metric deltas with their margin). Flutter and Tauri mentions now name the Android app or a native shell, operator quotes and dates are dropped, and one library's counts no longer appear outside test fixtures. Verified with a TypeScript-scanner token comparison (template literals and regex literals re-scanned): only comment text changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
214 lines
9.7 KiB
Svelte
214 lines
9.7 KiB
Svelte
<script lang="ts">
|
||
import type { CreateQueryResult } from '@tanstack/svelte-query';
|
||
import {
|
||
createRecommendationMetricsQuery,
|
||
type RecommendationMetrics,
|
||
type SurfaceIntent,
|
||
type MetricDelta
|
||
} from '#lib/api/metrics.js';
|
||
|
||
// How plays launched from each recommendation surface land against the
|
||
// music you pick yourself. On Library › Your listening: a report on your
|
||
// own listening, kept out of Settings, which holds choices.
|
||
|
||
const metrics = createRecommendationMetricsQuery() as CreateQueryResult<RecommendationMetrics>;
|
||
|
||
// Per-intent expectation copy: each surface band is judged against its
|
||
// job — discovery mixes are supposed to run hotter skip rates.
|
||
const intentHints: Record<SurfaceIntent, string> = {
|
||
go_to: 'Everyday surfaces — these should beat your own picking.',
|
||
discovery: 'Exploration surfaces — higher skip rates here are expected and healthy.',
|
||
direct: 'Music you chose yourself — naturally close to the baseline.'
|
||
};
|
||
|
||
function pct(v: number): string {
|
||
return `${(v * 100).toFixed(0)}%`;
|
||
}
|
||
|
||
// Deltas come from the server with their margin of error. The client does
|
||
// not subtract rates itself: the margin needs the sample sizes and
|
||
// variances, and having both clients re-derive it invites them to disagree.
|
||
//
|
||
// A delta that isn't distinguishable from zero is prefixed "≈" and dimmed,
|
||
// so a −12 on 59 plays, which is noise, does not read as boldly as a −6 on
|
||
// 400.
|
||
function deltaText(d: MetricDelta | undefined): string {
|
||
if (!d) return '';
|
||
const pts = Math.round(d.delta_pp);
|
||
const signed = pts > 0 ? `+${pts}` : `${pts}`;
|
||
return d.distinguishable ? signed : `≈${signed}`;
|
||
}
|
||
|
||
function deltaTitle(d: MetricDelta | undefined): string | undefined {
|
||
if (!d) return undefined;
|
||
const range = `${d.delta_pp.toFixed(1)} ± ${d.margin_pp.toFixed(1)} points vs baseline`;
|
||
return d.distinguishable
|
||
? `${range} (95% confidence)`
|
||
: `${range} — not distinguishable from zero at 95% confidence, so read this as no measured difference.`;
|
||
}
|
||
|
||
// A skip delta is "worse" above the baseline; a completion delta is "worse"
|
||
// below it. Neither gets a colour unless it's distinguishable — colouring
|
||
// noise red is what made the old card misleading.
|
||
function skipDeltaClass(d: MetricDelta | undefined): string {
|
||
if (!d?.distinguishable) return 'text-text-secondary opacity-60';
|
||
return d.delta_pp > 0 ? 'text-danger' : 'text-text-secondary';
|
||
}
|
||
|
||
function completionDeltaClass(d: MetricDelta | undefined): string {
|
||
if (!d?.distinguishable) return 'text-text-secondary opacity-60';
|
||
return d.delta_pp < 0 ? 'text-danger' : 'text-text-secondary';
|
||
}
|
||
|
||
// Pick-kind breakdowns are collapsed by default: with every
|
||
// system mix stamping provenance, always-open sub-rows would triple
|
||
// the table height and bury the surface-level comparison.
|
||
let expandedBreakdowns = $state(new Set<string>());
|
||
|
||
function toggleBreakdown(key: string) {
|
||
const next = new Set(expandedBreakdowns);
|
||
if (next.has(key)) next.delete(key);
|
||
else next.add(key);
|
||
expandedBreakdowns = next;
|
||
}
|
||
</script>
|
||
|
||
<section class="space-y-3 rounded border border-border bg-surface p-4">
|
||
<h2 class="text-lg font-semibold">Recommendation metrics</h2>
|
||
<p class="text-sm text-text-secondary">
|
||
How plays launched from each recommendation surface land, over the last
|
||
{$metrics.data?.window_days ?? 30} days — compared against the baseline of
|
||
music you picked yourself. Deltas are percentage points vs that baseline.
|
||
</p>
|
||
{#if $metrics.isPending}
|
||
<p class="text-sm text-text-secondary">Loading…</p>
|
||
{:else if $metrics.isError}
|
||
<p class="text-sm text-text-secondary">Couldn't load metrics.</p>
|
||
{:else if $metrics.data && ($metrics.data.groups.length > 0 || $metrics.data.baseline)}
|
||
{@const baseline = $metrics.data.baseline}
|
||
{#if baseline}
|
||
<p class="rounded bg-background px-3 py-2 text-sm">
|
||
<span class="font-medium">Baseline — manual plays:</span>
|
||
<span class="text-text-secondary">
|
||
{baseline.plays} plays · {pct(baseline.skip_rate)} skip ·
|
||
{pct(baseline.avg_completion)} completion
|
||
</span>
|
||
</p>
|
||
{:else}
|
||
<p class="text-sm text-text-secondary">
|
||
No manual plays in this window yet, so deltas are hidden — raw rates only.
|
||
</p>
|
||
{/if}
|
||
{#each $metrics.data.groups as group (group.intent)}
|
||
<div class="space-y-1">
|
||
<h3 class="text-sm font-medium">{group.label}</h3>
|
||
<p class="text-xs text-text-secondary">{intentHints[group.intent]}</p>
|
||
<table class="w-full text-sm">
|
||
<thead>
|
||
<tr class="text-left text-text-secondary">
|
||
<th class="py-1 font-medium">Surface</th>
|
||
<th class="py-1 text-right font-medium">Plays</th>
|
||
<th class="py-1 text-right font-medium">Skip rate</th>
|
||
<th class="py-1 text-right font-medium">Avg completion</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{#each group.surfaces as m (m.key)}
|
||
<tr
|
||
class="border-t border-border"
|
||
class:opacity-60={m.low_confidence}
|
||
title={m.low_confidence
|
||
? 'Fewer than 20 plays — treat these rates as anecdote, not signal.'
|
||
: undefined}
|
||
>
|
||
<td class="py-1">
|
||
{#if (m.breakdown ?? []).length > 0}
|
||
<button
|
||
type="button"
|
||
class="inline-flex items-center gap-1 rounded outline-hidden focus-visible:ring-1 focus-visible:ring-accent"
|
||
aria-expanded={expandedBreakdowns.has(m.key)}
|
||
onclick={() => toggleBreakdown(m.key)}
|
||
>
|
||
<span class="text-xs text-text-secondary" aria-hidden="true">
|
||
{expandedBreakdowns.has(m.key) ? '▾' : '▸'}
|
||
</span>
|
||
{m.label}
|
||
</button>
|
||
{:else}
|
||
{m.label}
|
||
{/if}{#if m.low_confidence}<span class="ml-1 text-xs text-text-secondary">· low data</span>{/if}
|
||
</td>
|
||
<td class="py-1 text-right tabular-nums">{m.plays}</td>
|
||
<td class="py-1 text-right tabular-nums">
|
||
{pct(m.skip_rate)}
|
||
{#if m.skip_delta}
|
||
<span class="ml-1 text-xs {skipDeltaClass(m.skip_delta)}"
|
||
data-testid="skip-delta-{m.key}"
|
||
title={deltaTitle(m.skip_delta)}>{deltaText(m.skip_delta)}</span>
|
||
{/if}
|
||
</td>
|
||
<td class="py-1 text-right tabular-nums">
|
||
{pct(m.avg_completion)}
|
||
{#if m.completion_delta}
|
||
<span class="ml-1 text-xs {completionDeltaClass(m.completion_delta)}"
|
||
data-testid="completion-delta-{m.key}"
|
||
title={deltaTitle(m.completion_delta)}>{deltaText(m.completion_delta)}</span>
|
||
{/if}
|
||
</td>
|
||
</tr>
|
||
<!-- Pick-kind provenance sub-rows: each
|
||
surface splits into the populations its builder
|
||
stamped — For You's taste vs freshness injection,
|
||
Discover's buckets, the tiered mixes' tiers — the
|
||
numbers that say WHERE a surface's skips come from.
|
||
Toggled per surface to keep the default view flat. -->
|
||
{#if expandedBreakdowns.has(m.key)}
|
||
{#each m.breakdown ?? [] as b (b.key)}
|
||
<tr
|
||
class="border-t border-border/50 text-text-secondary"
|
||
class:opacity-60={b.low_confidence}
|
||
title={b.low_confidence
|
||
? 'Fewer than 20 plays — treat these rates as anecdote, not signal.'
|
||
: undefined}
|
||
>
|
||
<td class="py-1 pl-4 text-xs">
|
||
↳ {b.label}{#if b.low_confidence}<span class="ml-1">· low data</span>{/if}
|
||
</td>
|
||
<td class="py-1 text-right text-xs tabular-nums">{b.plays}</td>
|
||
<td class="py-1 text-right text-xs tabular-nums">
|
||
{pct(b.skip_rate)}
|
||
{#if b.skip_delta}
|
||
<span class="ml-1 {skipDeltaClass(b.skip_delta)}"
|
||
data-testid="skip-delta-{b.key}"
|
||
title={deltaTitle(b.skip_delta)}>{deltaText(b.skip_delta)}</span>
|
||
{/if}
|
||
</td>
|
||
<td class="py-1 text-right text-xs tabular-nums">
|
||
{pct(b.avg_completion)}
|
||
{#if b.completion_delta}
|
||
<span class="ml-1 {completionDeltaClass(b.completion_delta)}"
|
||
data-testid="completion-delta-{b.key}"
|
||
title={deltaTitle(b.completion_delta)}>{deltaText(b.completion_delta)}</span>
|
||
{/if}
|
||
</td>
|
||
</tr>
|
||
{/each}
|
||
{/if}
|
||
{/each}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
{/each}
|
||
<p class="text-xs text-text-secondary">
|
||
Deltas compare each surface with your manual plays. A delta marked
|
||
<span class="opacity-60">≈</span> is smaller than its own margin of error at this
|
||
sample size — it can't be told apart from no difference, however big it looks.
|
||
Hover any delta for its range.
|
||
</p>
|
||
{:else}
|
||
<p class="text-sm text-text-secondary">
|
||
No plays recorded yet. Play something from For You, Discover, or a mix.
|
||
</p>
|
||
{/if}
|
||
</section>
|