Skip to content

Fix broken listen streak challenge state - #11739

Merged
dharit-tan merged 2 commits into
mainfrom
rt-listen-again
Apr 3, 2025
Merged

Fix broken listen streak challenge state#11739
dharit-tan merged 2 commits into
mainfrom
rt-listen-again

Conversation

@dharit-tan

Copy link
Copy Markdown
Contributor

Description

The bug: in this PR I changed the listen streak time window for continuing the listen streak from 24-48 hours to 16-48 hours. This required a specifier change - we had to increase granularity from days to hours in order to allow 2 user_challenge rows to exist that were in the same day.

I failed to realize that this would break this part of the challenge manager - since the specifier changed it wouldn't correctly grab the most recent challenge and would think it should start a new one.

This migration does 2 things:

  • update the challenge_listen_streaklisten_streak column to correct values (in most cases this is decreasing the streak bc there are user_challenges rows with current_step_count = 8 which shouldn't be possible.
  • Mark all affected user_challenge rows with is_complete = true and current_step_count = 7. Criteria: current_step_count != amount and the immediate later neighbor row has is_complete = true. Ie. if there is a 7-day row that erroneously did not get marked complete, but then there was a later 1-day row that is complete, then that 7-day row should have been marked complete.

How Has This Been Tested?

Had AI spit out SELECT versions of these queries (dry-runs) that i manually verified.

WITH latest_incomplete_seven_per_user AS (
SELECT DISTINCT ON (user_id)
user_id,
created_at as seven_created_at
FROM user_challenges
WHERE challenge_id = 'e' AND
amount = 7 AND
current_step_count != amount AND
created_at <= '2025-03-25' AND created_at > '2025-03-14'
ORDER BY user_id, created_at DESC
),
valid_streaks AS (
SELECT ls.user_id,
ls.seven_created_at,
7 + COUNT(uc.user_id) as streak_count,
array_agg(uc.created_at ORDER BY uc.created_at) as counted_dates
FROM latest_incomplete_seven_per_user ls
LEFT JOIN user_challenges uc ON uc.user_id = ls.user_id AND
uc.challenge_id = 'e' AND
uc.created_at > ls.seven_created_at AND
uc.amount = 1 AND
uc.is_complete = true
WHERE NOT EXISTS (
-- Exclude if there are ANY incomplete rows after our target seven
SELECT 1 FROM user_challenges uc_incomplete
WHERE uc_incomplete.user_id = ls.user_id AND
uc_incomplete.challenge_id = 'e' AND
uc_incomplete.created_at > ls.seven_created_at AND
uc_incomplete.created_at != ls.seven_created_at AND
uc_incomplete.current_step_count != uc_incomplete.amount
)
AND EXISTS (
-- Still require at least one completed amount=1 row after the seven
SELECT 1 FROM user_challenges uc_next
WHERE uc_next.user_id = ls.user_id AND
uc_next.challenge_id = 'e' AND
uc_next.created_at > ls.seven_created_at AND
uc_next.amount = 1 AND
uc_next.is_complete = true
)
GROUP BY ls.user_id, ls.seven_created_at
)
SELECT cls.user_id,
cls.listen_streak as current_streak,
vs.streak_count as new_streak,
vs.streak_count - cls.listen_streak as streak_difference,
vs.seven_created_at,
vs.counted_dates,
(
SELECT json_agg(json_build_object(
'created_at', uc.created_at,
'amount', uc.amount,
'current_step_count', uc.current_step_count,
'is_complete', uc.is_complete
) ORDER BY uc.created_at)
FROM user_challenges uc
WHERE uc.user_id = cls.user_id
AND uc.challenge_id = 'e'
AND uc.created_at >= vs.seven_created_at
) as relevant_challenges
FROM challenge_listen_streak cls
JOIN valid_streaks vs ON cls.user_id = vs.user_id
WHERE cls.listen_streak != vs.streak_count
ORDER BY abs(vs.streak_count - cls.listen_streak) DESC;
WITH next_row AS (
SELECT uc1.*,
LEAD(amount) OVER (PARTITION BY user_id, challenge_id ORDER BY created_at ASC) as next_amount,
LEAD(is_complete) OVER (PARTITION BY user_id, challenge_id ORDER BY created_at ASC) as next_is_complete
FROM user_challenges uc1
WHERE challenge_id = 'e'
)
SELECT nr.user_id,
nr.specifier,
nr.amount,
nr.current_step_count as current_step_count,
7 as new_step_count,
nr.is_complete as current_is_complete,
true as new_is_complete,
nr.created_at,
nr.next_amount,
nr.next_is_complete
FROM user_challenges uc
JOIN next_row nr ON
uc.user_id = nr.user_id AND
uc.challenge_id = nr.challenge_id AND
uc.specifier = nr.specifier
WHERE nr.amount = 7 AND nr.current_step_count != nr.amount AND
nr.next_amount = 1 AND nr.next_is_complete = true AND
nr.created_at <= '2025-03-25' AND nr.created_at > '2025-03-14'
ORDER BY nr.created_at DESC;

@changeset-bot

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b08dc24

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dharit-tan
dharit-tan merged commit 84fb624 into mainApr 3, 2025
@dharit-tan
dharit-tan deleted the rt-listen-again branch April 3, 2025 19:31
schottra added a commit that referenced this pull request Apr 3, 2025
* origin/main:
[PE-5891] Get event by entity ID endpoint (#11725)
Fix mobile tile layout bug (#11745)
[QA-2034] Fix notification loading state (#11734)
Fix broken listen streak challenge state (#11739)
[QA-2031] Fix embed play-button, scrubber, and stream (#11741)
Fix search due to my bad pythoning creating invalid ES queries (#11740)
[API-13] Only update scores when they need to be (#11737)
[PE-5900] Add audio wallet banner (#11738)
[PE-5881] Reduce data stored in lineup caches (#11713)
Updates to run-always cursor rule (#11726)
Fix user card bug (#11736)
Audius Protocol v0.7.125
[QA-2016] Fix play button getting out of sync (#11733)
Skip busy work in plays indexer if not a play tx (#11732)
Fix artist pick on profile page (#11731)
Disable lua scripts in nginx_container.conf (#11730)
Fix ffmpeg-kit-react-native ios install (#11715)
[QA-1997] Fix comment layout (#11719)
[PE-5899] Add new cash wallet UI (#11727)
Fix user card bug (#11722)
audius-infra pushed a commit that referenced this pull request Apr 4, 2025
[ae017f3] Improve trending rewards plugin (#11555) Raymond Jacobson
[89d85dd] Add index and use indexes better in block confirmation (#11742) Raymond Jacobson
[7aefb4c] [PE-5891] Get event by entity ID endpoint (#11725) Reed
[84fb624] Fix broken listen streak challenge state (#11739) Reed
[fe341d5] Fix search due to my bad pythoning creating invalid ES queries (#11740) Randy Schott
[0951262] [API-13] Only update scores when they need to be (#11737) Raymond Jacobson
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@dharit-tan@faridsalau