From 385573727bb9bbb08c68c8b1377e82295b1e585a Mon Sep 17 00:00:00 2001 From: Dylan Jeffers Date: Wed, 11 Nov 2026 12:45:08 -0800 Subject: [PATCH] feat(api): weekly artist recap notification One weekly_artist_recap notification per artist on rollover Wednesday with plays, new followers, top city, saves, and saves from Weekly Rotation listeners for the week that just closed. Artists with no plays get nothing. Co-Authored-By: Claude Opus 5.5 --- api/swagger/swagger-v1.yaml | 64 ++++ api/v1_notifications.go | 2 +- indexer/indexer.go | 5 + ...reate_weekly_artist_recap_notifications.go | 344 ++++++++++++++++++ ..._weekly_artist_recap_notifications_test.go | 250 +++++++++++++ 5 files changed, 664 insertions(+), 1 deletion(-) create mode 100644 jobs/create_weekly_artist_recap_notifications.go create mode 100644 jobs/create_weekly_artist_recap_notifications_test.go diff --git a/api/swagger/swagger-v1.yaml b/api/swagger/swagger-v1.yaml index 7816c024..bbd76b40 100644 --- a/api/swagger/swagger-v1.yaml +++ b/api/swagger/swagger-v1.yaml @@ -8531,6 +8531,7 @@ paths: - listen_streak_reminder - weekly_rotation - weekly_rotation_featured + - weekly_artist_recap - fan_remix_contest_started - fan_remix_contest_ended - fan_remix_contest_ending_soon @@ -17385,6 +17386,7 @@ components: - $ref: "#/components/schemas/listen_streak_reminder_notification" - $ref: "#/components/schemas/weekly_rotation_notification" - $ref: "#/components/schemas/weekly_rotation_featured_notification" + - $ref: "#/components/schemas/weekly_artist_recap_notification" - $ref: "#/components/schemas/fan_remix_contest_started_notification" - $ref: "#/components/schemas/fan_remix_contest_ended_notification" - $ref: "#/components/schemas/fan_remix_contest_ending_soon_notification" @@ -17436,6 +17438,7 @@ components: listen_streak_reminder: "#/components/schemas/listen_streak_reminder_notification" weekly_rotation: "#/components/schemas/weekly_rotation_notification" weekly_rotation_featured: "#/components/schemas/weekly_rotation_featured_notification" + weekly_artist_recap: "#/components/schemas/weekly_artist_recap_notification" fan_remix_contest_started: "#/components/schemas/fan_remix_contest_started_notification" fan_remix_contest_ended: "#/components/schemas/fan_remix_contest_ended_notification" fan_remix_contest_ending_soon: "#/components/schemas/fan_remix_contest_ending_soon_notification" @@ -18109,6 +18112,26 @@ components: type: array items: $ref: "#/components/schemas/weekly_rotation_featured_notification_action" + weekly_artist_recap_notification: + required: + - actions + - group_id + - is_seen + - type + type: object + properties: + type: + type: string + group_id: + type: string + is_seen: + type: boolean + seen_at: + type: integer + actions: + type: array + items: + $ref: "#/components/schemas/weekly_artist_recap_notification_action" comment_thread_notification_action: required: - data @@ -19360,6 +19383,22 @@ components: type: integer data: $ref: "#/components/schemas/weekly_rotation_featured_notification_action_data" + weekly_artist_recap_notification_action: + required: + - data + - specifier + - timestamp + - type + type: object + properties: + specifier: + type: string + type: + type: string + timestamp: + type: integer + data: + $ref: "#/components/schemas/weekly_artist_recap_notification_action_data" comment_reaction_notification_action_data: required: - entity_id @@ -19885,6 +19924,31 @@ components: type: integer rotation_count: type: integer + weekly_artist_recap_notification_action_data: + description: An artist's recap of the period that just closed (an ISO (year, week) pair, Wednesday to Wednesday UTC). Plays exclude the artist's own. rotation_save_count is how many of the saves came from listeners whose Weekly Rotation included the track. top_city is omitted when no play had a city. + required: + - year + - week + - play_count + - follower_count + - save_count + - rotation_save_count + type: object + properties: + year: + type: integer + week: + type: integer + play_count: + type: integer + follower_count: + type: integer + save_count: + type: integer + rotation_save_count: + type: integer + top_city: + type: string artist_remix_contest_submissions_notification_action_data: required: - entity_id diff --git a/api/v1_notifications.go b/api/v1_notifications.go index 411483b4..633fc48d 100644 --- a/api/v1_notifications.go +++ b/api/v1_notifications.go @@ -56,7 +56,7 @@ const ( type GetNotificationsQueryParams struct { Limit int `query:"limit" default:"20" validate:"min=0,max=100"` - Types []string `query:"types" validate:"dive,oneof=announcement follow repost save remix cosign create tip_receive tip_send challenge_reward repost_of_repost save_of_repost tastemaker reaction supporter_dethroned supporter_rank_up supporting_rank_up milestone track_added_to_playlist tier_change trending trending_playlist trending_underground usdc_purchase_buyer usdc_purchase_seller track_added_to_purchased_album request_manager approve_manager_request track_collaborator_invite track_collaborator_accept claimable_reward comment comment_thread comment_mention comment_reaction listen_streak_reminder weekly_rotation weekly_rotation_featured fan_remix_contest_started fan_remix_contest_ended fan_remix_contest_ending_soon fan_remix_contest_winners_selected fan_remix_contest_submission artist_remix_contest_ended artist_remix_contest_ending_soon artist_remix_contest_submissions fan_club_text_post remix_contest_update"` + Types []string `query:"types" validate:"dive,oneof=announcement follow repost save remix cosign create tip_receive tip_send challenge_reward repost_of_repost save_of_repost tastemaker reaction supporter_dethroned supporter_rank_up supporting_rank_up milestone track_added_to_playlist tier_change trending trending_playlist trending_underground usdc_purchase_buyer usdc_purchase_seller track_added_to_purchased_album request_manager approve_manager_request track_collaborator_invite track_collaborator_accept claimable_reward comment comment_thread comment_mention comment_reaction listen_streak_reminder weekly_rotation weekly_rotation_featured weekly_artist_recap fan_remix_contest_started fan_remix_contest_ended fan_remix_contest_ending_soon fan_remix_contest_winners_selected fan_remix_contest_submission artist_remix_contest_ended artist_remix_contest_ending_soon artist_remix_contest_submissions fan_club_text_post remix_contest_update"` GroupID string `query:"group_id" validate:"omitempty"` Timestamp float64 `query:"timestamp" validate:"omitempty,min=0"` } diff --git a/indexer/indexer.go b/indexer/indexer.go index 51235636..3db5d10e 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -249,6 +249,11 @@ func (ci *CoreIndexer) startParityJobs(ctx context.Context) { jobs.NewWeeklyRotationFeaturedNotificationsJob(ci.Config, ci.pool). ScheduleEvery(ctx, 30*time.Second) + // Weekly recap for artists (plays, new followers, top city, saves) for + // the period that just closed. Same timing as the featured notification. + jobs.NewWeeklyArtistRecapNotificationsJob(ci.Config, ci.pool). + ScheduleEvery(ctx, 30*time.Second) + // Backfill missing track bpm / musical_key from content-node audio // analyses. Mirrors apps' repair_audio_analyses celery task, whose beat // schedule ran every 3 minutes. Needs the SDK for content-node discovery. diff --git a/jobs/create_weekly_artist_recap_notifications.go b/jobs/create_weekly_artist_recap_notifications.go new file mode 100644 index 00000000..95a474f1 --- /dev/null +++ b/jobs/create_weekly_artist_recap_notifications.go @@ -0,0 +1,344 @@ +package jobs + +import ( + "context" + "fmt" + "sort" + "sync" + "time" + + "api.audius.co/config" + "api.audius.co/database" + "api.audius.co/logging" + "api.audius.co/weeklyrotation" + "github.com/jackc/pgx/v5" + "go.uber.org/zap" +) + +// WeeklyArtistRecapNotificationsJob sends each artist a recap of the week +// that just closed: plays, new followers, top listening city and saves, plus +// how many of those saves came from listeners whose Weekly Rotation included +// the track. One weekly_artist_recap notification per artist per week; +// pedalboard turns the row into a push, gated by the push_weekly_artist_recap +// remote-config variable. Artists with no plays that week get nothing. +// +// The week is the Weekly Rotation period (Wednesday 00:00 UTC to the next +// Wednesday), and timing matches WeeklyRotationFeaturedNotificationsJob: sends +// start at weeklyArtistRecapSendHourUTC on rollover day, last for +// weeklyArtistRecapSendWindow, and report the period that just closed. Stage +// sends all period. +// +// Counting a week of plays is the expensive part, so each replica does it once +// per period and keeps the result in memory sorted by artist. Each run then +// inserts up to batchSize rows past an owner_id cursor, so pedalboard and SNS +// see a stream rather than a burst. uq_notification (group_id, specifier) plus +// NOT EXISTS makes reruns, restarts and multiple replicas safe. +type WeeklyArtistRecapNotificationsJob struct { + pool database.DbPool + logger *zap.Logger + env string + now func() time.Time + batchSize int + + mutex sync.Mutex + isRunning bool + + cursorPeriod string + cursorOwnerId int32 + recaps []artistRecap + loaded bool + drained bool +} + +type artistRecap struct { + OwnerId int32 + PlayCount int64 + FollowerCount int64 + SaveCount int64 + RotationSaveCount int64 + TopCity *string +} + +const ( + // weeklyArtistRecapSendHourUTC is the hour (UTC) on rollover day the + // fan-out begins, alongside the weekly_rotation_featured fan-out. + weeklyArtistRecapSendHourUTC = 18 + // weeklyArtistRecapSendWindow is how long after that the job keeps sending. + weeklyArtistRecapSendWindow = 24 * time.Hour + // weeklyArtistRecapBatchSize is the most rows one run inserts. + weeklyArtistRecapBatchSize = 1000 +) + +func NewWeeklyArtistRecapNotificationsJob(cfg config.Config, pool database.DbPool) *WeeklyArtistRecapNotificationsJob { + return &WeeklyArtistRecapNotificationsJob{ + pool: pool, + logger: logging.NewZapLogger(cfg).Named("WeeklyArtistRecapNotificationsJob"), + env: cfg.Env, + now: time.Now, + batchSize: weeklyArtistRecapBatchSize, + } +} + +// ScheduleEvery runs the job every `interval` until the context is cancelled. +func (j *WeeklyArtistRecapNotificationsJob) ScheduleEvery(ctx context.Context, interval time.Duration) *WeeklyArtistRecapNotificationsJob { + go func() { + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ticker.C: + j.Run(ctx) + case <-ctx.Done(): + j.logger.Info("Job shutting down") + return + } + } + }() + return j +} + +// Run executes the job once. +func (j *WeeklyArtistRecapNotificationsJob) Run(ctx context.Context) { + if err := j.run(ctx); err != nil { + j.logger.Error("Job run failed", zap.Error(err)) + } +} + +// sendWindow returns the instants between which the period before the one +// containing `now` is reported. +func (j *WeeklyArtistRecapNotificationsJob) sendWindow(now time.Time) (start, end time.Time) { + year, week := weeklyrotation.Period(now) + periodStart := weeklyrotation.PeriodStart(year, week) + if j.env == "stage" { + return periodStart, periodStart.AddDate(0, 0, 7) + } + start = periodStart.Add(weeklyArtistRecapSendHourUTC * time.Hour) + return start, start.Add(weeklyArtistRecapSendWindow) +} + +func (j *WeeklyArtistRecapNotificationsJob) run(ctx context.Context) error { + start := time.Now() + j.mutex.Lock() + if j.isRunning { + j.mutex.Unlock() + return fmt.Errorf("job is already running") + } + j.isRunning = true + j.mutex.Unlock() + defer func() { + j.mutex.Lock() + j.isRunning = false + j.mutex.Unlock() + }() + + now := j.now() + windowStart, windowEnd := j.sendWindow(now) + if now.Before(windowStart) || !now.Before(windowEnd) { + return nil + } + + // The period being reported is the one that just ended. + year, week := weeklyrotation.Period(now.AddDate(0, 0, -7)) + period := weeklyrotation.PeriodKey(year, week) + if j.cursorPeriod != period { + j.cursorPeriod = period + j.cursorOwnerId = 0 + j.recaps = nil + j.loaded = false + j.drained = false + } + if j.drained { + return nil + } + + periodStart := weeklyrotation.PeriodStart(year, week) + if !j.loaded { + recaps, err := j.loadRecaps(ctx, periodStart) + if err != nil { + return err + } + j.recaps = recaps + j.loaded = true + j.logger.Info("Loaded weekly_artist_recap counts", + zap.String("period", period), + zap.Int("artists", len(recaps)), + zap.Duration("duration", time.Since(start))) + } + + // Next batch past the cursor. + i := sort.Search(len(j.recaps), func(i int) bool { return j.recaps[i].OwnerId > j.cursorOwnerId }) + batch := j.recaps[i:min(i+j.batchSize, len(j.recaps))] + if len(batch) == 0 { + j.drained = true + j.recaps = nil + return nil + } + + ownerIds := make([]int32, len(batch)) + plays := make([]int64, len(batch)) + followers := make([]int64, len(batch)) + saves := make([]int64, len(batch)) + rotationSaves := make([]int64, len(batch)) + cities := make([]*string, len(batch)) + for k, r := range batch { + ownerIds[k] = r.OwnerId + plays[k] = r.PlayCount + followers[k] = r.FollowerCount + saves[k] = r.SaveCount + rotationSaves[k] = r.RotationSaveCount + cities[k] = r.TopCity + } + + tag, err := j.pool.Exec(ctx, ` + INSERT INTO notification (specifier, group_id, blocknumber, user_ids, type, data, timestamp) + SELECT + r.owner_id::text, + @group_prefix || r.owner_id::text, + NULL, + ARRAY[r.owner_id], + 'weekly_artist_recap', + jsonb_strip_nulls(jsonb_build_object( + 'year', @year::int, + 'week', @week::int, + 'play_count', r.play_count, + 'follower_count', r.follower_count, + 'save_count', r.save_count, + 'rotation_save_count', r.rotation_save_count, + 'top_city', r.top_city + )), + @now + FROM UNNEST( + @owner_ids::int[], @plays::bigint[], @followers::bigint[], + @saves::bigint[], @rotation_saves::bigint[], @cities::text[] + ) AS r(owner_id, play_count, follower_count, save_count, rotation_save_count, top_city) + WHERE NOT EXISTS ( + SELECT 1 FROM notification n + WHERE n.group_id = @group_prefix || r.owner_id::text + ) + ON CONFLICT (group_id, specifier) DO NOTHING + `, pgx.NamedArgs{ + "group_prefix": "weekly_artist_recap:" + period + ":", + "year": year, + "week": week, + "now": now, + "owner_ids": ownerIds, + "plays": plays, + "followers": followers, + "saves": saves, + "rotation_saves": rotationSaves, + "cities": cities, + }) + if err != nil { + return fmt.Errorf("insert weekly_artist_recap notifications: %w", err) + } + j.cursorOwnerId = batch[len(batch)-1].OwnerId + if len(batch) < j.batchSize { + j.drained = true + j.recaps = nil + } + + if tag.RowsAffected() > 0 { + j.logger.Info("Inserted weekly_artist_recap notifications", + zap.String("period", period), + zap.Int64("count", tag.RowsAffected()), + zap.Int32("cursor", j.cursorOwnerId), + zap.Duration("duration", time.Since(start))) + } + return nil +} + +// loadRecaps counts the period starting at periodStart for every artist whose +// tracks were played by someone else that week, skipping deactivated and +// unavailable artists. Sorted by owner_id. +func (j *WeeklyArtistRecapNotificationsJob) loadRecaps(ctx context.Context, periodStart time.Time) ([]artistRecap, error) { + rows, err := j.pool.Query(ctx, ` + WITH week_plays AS ( + SELECT t.owner_id, NULLIF(TRIM(p.city), '') AS city + FROM plays p + JOIN tracks t ON t.track_id = p.play_item_id AND t.is_current + WHERE p.created_at >= @start AND p.created_at < @end + AND p.user_id IS DISTINCT FROM t.owner_id + ), + per_artist AS ( + SELECT owner_id, COUNT(*) AS play_count + FROM week_plays + GROUP BY owner_id + ), + top_city AS ( + SELECT DISTINCT ON (owner_id) owner_id, city + FROM ( + SELECT owner_id, city, COUNT(*) AS plays + FROM week_plays + WHERE city IS NOT NULL + GROUP BY owner_id, city + ) c + ORDER BY owner_id, plays DESC, city + ), + artists AS ( + SELECT a.owner_id, a.play_count + FROM per_artist a + JOIN users u ON u.user_id = a.owner_id + AND u.is_current + AND NOT u.is_deactivated + AND u.is_available + ), + new_followers AS ( + SELECT f.followee_user_id AS owner_id, COUNT(*) AS follower_count + FROM follows f + JOIN artists a ON a.owner_id = f.followee_user_id + WHERE f.is_current AND NOT f.is_delete + AND f.created_at >= @start AND f.created_at < @end + GROUP BY f.followee_user_id + ), + week_saves AS ( + SELECT t.owner_id, s.user_id, s.save_item_id + FROM saves s + JOIN tracks t ON t.track_id = s.save_item_id AND t.is_current + JOIN artists a ON a.owner_id = t.owner_id + WHERE s.save_type = 'track' + AND s.is_current AND NOT s.is_delete + AND s.created_at >= @start AND s.created_at < @end + ), + save_counts AS ( + SELECT + ws.owner_id, + COUNT(*) AS save_count, + COUNT(*) FILTER (WHERE EXISTS ( + SELECT 1 FROM weekly_rotation_mixes m + WHERE m.user_id = ws.user_id + AND m.period_start = @start::date + AND ws.save_item_id = ANY(m.track_ids[1:@shown_size::int]) + )) AS rotation_save_count + FROM week_saves ws + GROUP BY ws.owner_id + ) + SELECT + a.owner_id, + a.play_count, + COALESCE(nf.follower_count, 0), + COALESCE(sc.save_count, 0), + COALESCE(sc.rotation_save_count, 0), + tc.city + FROM artists a + LEFT JOIN new_followers nf ON nf.owner_id = a.owner_id + LEFT JOIN save_counts sc ON sc.owner_id = a.owner_id + LEFT JOIN top_city tc ON tc.owner_id = a.owner_id + ORDER BY a.owner_id + `, pgx.NamedArgs{ + "start": periodStart, + "end": periodStart.AddDate(0, 0, 7), + "shown_size": weeklyrotation.ShownSize, + }) + if err != nil { + return nil, fmt.Errorf("count weekly_artist_recap: %w", err) + } + recaps, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (artistRecap, error) { + var r artistRecap + err := row.Scan(&r.OwnerId, &r.PlayCount, &r.FollowerCount, &r.SaveCount, &r.RotationSaveCount, &r.TopCity) + return r, err + }) + if err != nil { + return nil, fmt.Errorf("collect weekly_artist_recap: %w", err) + } + return recaps, nil +} diff --git a/jobs/create_weekly_artist_recap_notifications_test.go b/jobs/create_weekly_artist_recap_notifications_test.go new file mode 100644 index 00000000..ead28bf4 --- /dev/null +++ b/jobs/create_weekly_artist_recap_notifications_test.go @@ -0,0 +1,250 @@ +package jobs + +import ( + "context" + "testing" + "time" + + "api.audius.co/database" + "api.audius.co/weeklyrotation" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// 2026-09-16 is the Wednesday that opens period 2026-38, so this is the first +// send instant for the 2026-37 recap (2026-09-09 to 2026-09-16). +var weeklyArtistRecapSendInstant = time.Date(2026, time.September, 16, 18, 0, 0, 0, time.UTC) + +func seedWeeklyArtistRecap(t *testing.T, pool *pgxpool.Pool) { + inWeek := weeklyrotation.PeriodStart(2026, 37).Add(36 * time.Hour) + beforeWeek := weeklyrotation.PeriodStart(2026, 37).Add(-time.Minute) + nextWeek := weeklyrotation.PeriodStart(2026, 38).Add(time.Hour) + + play := func(id, userId, trackId int, city any, at time.Time) map[string]any { + var u any = userId + if userId == 0 { + u = nil + } + return map[string]any{"id": id, "user_id": u, "play_item_id": trackId, "city": city, "created_at": at} + } + + database.Seed(pool, database.FixtureMap{ + "users": { + {"user_id": 1, "handle": "listener1", "wallet": "0x01"}, + {"user_id": 2, "handle": "listener2", "wallet": "0x02"}, + {"user_id": 3, "handle": "listener3", "wallet": "0x03"}, + {"user_id": 10, "handle": "busy", "wallet": "0x10"}, + {"user_id": 20, "handle": "quiet", "wallet": "0x20"}, + {"user_id": 30, "handle": "deactivated", "wallet": "0x30", "is_deactivated": true}, + {"user_id": 40, "handle": "self_only", "wallet": "0x40"}, + {"user_id": 50, "handle": "no_plays", "wallet": "0x50"}, + {"user_id": 60, "handle": "next_week", "wallet": "0x60"}, + }, + "tracks": { + {"track_id": 101, "owner_id": 10, "title": "a"}, + {"track_id": 102, "owner_id": 10, "title": "b"}, + {"track_id": 201, "owner_id": 20, "title": "c"}, + {"track_id": 301, "owner_id": 30, "title": "d"}, + {"track_id": 401, "owner_id": 40, "title": "e"}, + {"track_id": 501, "owner_id": 50, "title": "f"}, + {"track_id": 601, "owner_id": 60, "title": "g"}, + }, + "plays": { + // Artist 10: 4 plays this week, Atlanta leads; one anonymous. + play(1, 1, 101, "Atlanta", inWeek), + play(2, 2, 101, "Atlanta", inWeek), + play(3, 3, 102, "Berlin", inWeek), + play(4, 0, 102, nil, inWeek), + // Last week and next week don't count. + play(5, 1, 101, "Paris", beforeWeek), + play(6, 1, 101, "Paris", nextWeek), + // Artist 20: one play with no city. + play(7, 1, 201, "", inWeek), + // Artist 30 is deactivated; 40 only played their own track. + play(8, 1, 301, "Atlanta", inWeek), + play(9, 40, 401, "Atlanta", inWeek), + // Artist 60 only has plays in the in-progress period. + play(10, 1, 601, "Lagos", nextWeek), + }, + "follows": { + {"follower_user_id": 1, "followee_user_id": 10, "created_at": inWeek}, + {"follower_user_id": 2, "followee_user_id": 10, "created_at": inWeek}, + // Unfollowed, and followed before the week: neither counts. + {"follower_user_id": 3, "followee_user_id": 10, "created_at": inWeek, "is_delete": true}, + {"follower_user_id": 40, "followee_user_id": 10, "created_at": beforeWeek}, + {"follower_user_id": 1, "followee_user_id": 50, "created_at": inWeek}, + }, + "saves": { + {"user_id": 1, "save_item_id": 101, "save_type": "track", "created_at": inWeek}, + {"user_id": 2, "save_item_id": 102, "save_type": "track", "created_at": inWeek}, + {"user_id": 3, "save_item_id": 101, "save_type": "track", "created_at": inWeek}, + {"user_id": 3, "save_item_id": 102, "save_type": "track", "created_at": nextWeek}, + {"user_id": 1, "save_item_id": 501, "save_type": "track", "created_at": inWeek}, + }, + }) + + // Listener 1 had 101 in their 2026-37 rotation, listener 2 had 102 but + // past the shown slots, and listener 3 had 101 only in the next period. + seedWeeklyRotationMix(t, pool, 1, 2026, 37, []int32{101}) + listener2 := []int32{} + for len(listener2) < weeklyrotation.ShownSize { + listener2 = append(listener2, 201) + } + seedWeeklyRotationMix(t, pool, 2, 2026, 37, append(listener2, 102)) + seedWeeklyRotationMix(t, pool, 3, 2026, 38, []int32{101}) +} + +func newWeeklyArtistRecapJob(pool database.DbPool, now time.Time) *WeeklyArtistRecapNotificationsJob { + job := NewWeeklyArtistRecapNotificationsJob(newTestConfig(), pool) + job.now = func() time.Time { return now } + return job +} + +func TestWeeklyArtistRecapNotifications_FanOut(t *testing.T) { + pool := database.CreateTestDatabase(t, "test_jobs") + defer pool.Close() + ctx := context.Background() + seedWeeklyArtistRecap(t, pool) + + now := weeklyArtistRecapSendInstant.Add(30 * time.Minute) + job := newWeeklyArtistRecapJob(pool, now) + require.NoError(t, job.run(ctx)) + + rows, err := pool.Query(ctx, ` + SELECT specifier, group_id, user_ids, data, timestamp + FROM notification WHERE type = 'weekly_artist_recap' ORDER BY specifier::int`) + require.NoError(t, err) + defer rows.Close() + + type row struct { + specifier string + groupID string + userIDs []int32 + data map[string]any + ts time.Time + } + var got []row + for rows.Next() { + var r row + require.NoError(t, rows.Scan(&r.specifier, &r.groupID, &r.userIDs, &r.data, &r.ts)) + got = append(got, r) + } + // 30 (deactivated), 40 (self plays only), 50 (no plays) and 60 (plays + // only in the in-progress period) get nothing. + require.Len(t, got, 2) + + assert.Equal(t, "10", got[0].specifier) + assert.Equal(t, "weekly_artist_recap:2026-37:10", got[0].groupID) + assert.Equal(t, []int32{10}, got[0].userIDs) + assert.Equal(t, map[string]any{ + "year": float64(2026), "week": float64(37), + "play_count": float64(4), + "follower_count": float64(2), + "save_count": float64(3), + "rotation_save_count": float64(1), + "top_city": "Atlanta", + }, got[0].data) + assert.WithinDuration(t, now, got[0].ts, time.Second) + + // No city on record: top_city is left out. + assert.Equal(t, "20", got[1].specifier) + assert.Equal(t, map[string]any{ + "year": float64(2026), "week": float64(37), + "play_count": float64(1), + "follower_count": float64(0), + "save_count": float64(0), + "rotation_save_count": float64(0), + }, got[1].data) + + // Idempotent: the same job is drained, and a restart adds nothing. + assert.True(t, job.drained) + require.NoError(t, job.run(ctx)) + fresh := newWeeklyArtistRecapJob(pool, now) + require.NoError(t, fresh.run(ctx)) + assert.Equal(t, 2, countNotifications(t, ctx, pool, "weekly_artist_recap")) + + // The next rollover reports 2026-38. + nextWeek := newWeeklyArtistRecapJob(pool, now.AddDate(0, 0, 7)) + require.NoError(t, nextWeek.run(ctx)) + var groupIDs []string + rows2, err := pool.Query(ctx, + `SELECT group_id FROM notification WHERE group_id LIKE 'weekly_artist_recap:2026-38:%' ORDER BY group_id`) + require.NoError(t, err) + for rows2.Next() { + var g string + require.NoError(t, rows2.Scan(&g)) + groupIDs = append(groupIDs, g) + } + assert.Equal(t, []string{"weekly_artist_recap:2026-38:10", "weekly_artist_recap:2026-38:60"}, groupIDs) +} + +func TestWeeklyArtistRecapNotifications_SendWindow(t *testing.T) { + pool := database.CreateTestDatabase(t, "test_jobs") + defer pool.Close() + ctx := context.Background() + seedWeeklyArtistRecap(t, pool) + + cases := []struct { + name string + now time.Time + sent bool + }{ + {"rollover instant, before send hour", weeklyArtistRecapSendInstant.Add(-18 * time.Hour), false}, + {"one second before send hour", weeklyArtistRecapSendInstant.Add(-time.Second), false}, + {"send hour", weeklyArtistRecapSendInstant, true}, + {"one second before the window closes", weeklyArtistRecapSendInstant.Add(24*time.Hour - time.Second), true}, + {"window closed (Thursday 18:00)", weeklyArtistRecapSendInstant.Add(24 * time.Hour), false}, + {"the following Monday", weeklyArtistRecapSendInstant.AddDate(0, 0, 5), false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := pool.Exec(ctx, `DELETE FROM notification WHERE type = 'weekly_artist_recap'`) + require.NoError(t, err) + + job := newWeeklyArtistRecapJob(pool, tc.now) + require.NoError(t, job.run(ctx)) + want := 0 + if tc.sent { + want = 2 + } + assert.Equal(t, want, countNotifications(t, ctx, pool, "weekly_artist_recap")) + }) + } +} + +func TestWeeklyArtistRecapNotifications_StageSendsAllWeek(t *testing.T) { + pool := database.CreateTestDatabase(t, "test_jobs") + defer pool.Close() + ctx := context.Background() + seedWeeklyArtistRecap(t, pool) + + job := newWeeklyArtistRecapJob(pool, weeklyArtistRecapSendInstant.Add(-18*time.Hour)) + job.env = "stage" + require.NoError(t, job.run(ctx)) + assert.Equal(t, 2, countNotifications(t, ctx, pool, "weekly_artist_recap")) +} + +func TestWeeklyArtistRecapNotifications_Pacing(t *testing.T) { + pool := database.CreateTestDatabase(t, "test_jobs") + defer pool.Close() + ctx := context.Background() + seedWeeklyArtistRecap(t, pool) + + job := newWeeklyArtistRecapJob(pool, weeklyArtistRecapSendInstant) + job.batchSize = 1 + + require.NoError(t, job.run(ctx)) + assert.Equal(t, 1, countNotifications(t, ctx, pool, "weekly_artist_recap")) + assert.Equal(t, int32(10), job.cursorOwnerId) + assert.False(t, job.drained) + + require.NoError(t, job.run(ctx)) + assert.Equal(t, 2, countNotifications(t, ctx, pool, "weekly_artist_recap")) + assert.Equal(t, int32(20), job.cursorOwnerId) + + // The next run finds nothing past the cursor and marks the period drained. + require.NoError(t, job.run(ctx)) + assert.Equal(t, 2, countNotifications(t, ctx, pool, "weekly_artist_recap")) + assert.True(t, job.drained) +}