60533073ad
The recommendation metrics table was observable but not actionable: raw source strings (album:<uuid> one-offs) drowned the stable surfaces, and manual plays were excluded so skip rates had no control group. - SQL: include NULL-source rows (the baseline) and carry completion_n so family merges can weight avg_completion correctly. - Handler buckets raw sources into stable families (radio:<uuid> → Radio, album:/artist: → direct plays, etc.) grouped by surface intent: go-to / discovery / direct — each band judged against its job, since discovery mixes are expected to skip hotter. Families under 20 plays are flagged low-confidence, not hidden. - Settings card renders the baseline row and per-surface deltas in percentage points vs baseline (worse-than-baseline deltas in danger color), intent hint copy per group, low-data rows dimmed. - Pure-unit test for the bucketing/merge; DB test updated to the new contract (baseline included, radio:<uuid> collapse). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TsF3cNoKrqCYsU78cXC8U6
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: recommendation_metrics.sql
|
|
|
|
package dbq
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const recommendationSourceMetricsForUser = `-- name: RecommendationSourceMetricsForUser :many
|
|
|
|
SELECT
|
|
pe.source,
|
|
count(*)::bigint AS plays,
|
|
count(*) FILTER (WHERE pe.was_skipped)::bigint AS skips,
|
|
count(pe.completion_ratio)::bigint AS completion_n,
|
|
COALESCE(avg(pe.completion_ratio), 0)::float8 AS avg_completion
|
|
FROM play_events pe
|
|
WHERE pe.user_id = $1
|
|
AND pe.started_at > now() - ($2::float8 * INTERVAL '1 day')
|
|
GROUP BY pe.source
|
|
ORDER BY plays DESC
|
|
`
|
|
|
|
type RecommendationSourceMetricsForUserParams struct {
|
|
UserID pgtype.UUID
|
|
Column2 float64
|
|
}
|
|
|
|
type RecommendationSourceMetricsForUserRow struct {
|
|
Source *string
|
|
Plays int64
|
|
Skips int64
|
|
CompletionN int64
|
|
AvgCompletion float64
|
|
}
|
|
|
|
// Recommendation observability (#796 phase 4). Per-source play outcomes so the
|
|
// operator can see whether each recommendation surface is landing and tune the
|
|
// taste weights. Source is stamped on play_events when a play is launched from
|
|
// a recommendation surface; NULL means the user picked the track manually —
|
|
// those rows are INCLUDED here as the baseline control group the surfaces are
|
|
// judged against (milestone #127: delta-vs-baseline is what makes the numbers
|
|
// actionable). Raw source strings are bucketed into stable surface families in
|
|
// the Go handler; completion_n is carried so family merges can weight
|
|
// avg_completion correctly.
|
|
// $1 user_id, $2 window_days. plays/skips are counts; avg_completion is the
|
|
// mean completion ratio over the completion_n plays that recorded one.
|
|
func (q *Queries) RecommendationSourceMetricsForUser(ctx context.Context, arg RecommendationSourceMetricsForUserParams) ([]RecommendationSourceMetricsForUserRow, error) {
|
|
rows, err := q.db.Query(ctx, recommendationSourceMetricsForUser, arg.UserID, arg.Column2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []RecommendationSourceMetricsForUserRow
|
|
for rows.Next() {
|
|
var i RecommendationSourceMetricsForUserRow
|
|
if err := rows.Scan(
|
|
&i.Source,
|
|
&i.Plays,
|
|
&i.Skips,
|
|
&i.CompletionN,
|
|
&i.AvgCompletion,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|