[fix] incorrect week streak calculation

This commit is contained in:
Evan Reichard 2023-09-21 07:22:23 -04:00
parent bf24207948
commit c1f463f0b9
2 changed files with 33 additions and 23 deletions

View File

@ -270,10 +270,12 @@ FROM document_days;
-- name: GetUserWindowStreaks :one -- name: GetUserWindowStreaks :one
WITH document_windows AS ( WITH document_windows AS (
SELECT CASE SELECT
WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', start_time, 'weekday 0', '-7 day', time_offset) CASE
WHEN ?2 = "DAY" THEN DATE(start_time, time_offset) WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', start_time, time_offset, 'weekday 0', '-7 day')
END AS read_window WHEN ?2 = "DAY" THEN DATE(start_time, time_offset)
END AS read_window,
time_offset
FROM activity FROM activity
JOIN users ON users.id = activity.user_id JOIN users ON users.id = activity.user_id
WHERE user_id = $user_id WHERE user_id = $user_id
@ -293,12 +295,15 @@ streaks AS (
SELECT SELECT
count(*) AS streak, count(*) AS streak,
MIN(read_window) AS start_date, MIN(read_window) AS start_date,
MAX(read_window) AS end_date MAX(read_window) AS end_date,
time_offset
FROM partitions FROM partitions
GROUP BY CASE GROUP BY
WHEN ?2 = "DAY" THEN DATE(read_window, '+' || seqnum || ' day') CASE
WHEN ?2 = "WEEK" THEN DATE(read_window, '+' || (seqnum * 7) || ' day') WHEN ?2 = "DAY" THEN DATE(read_window, '+' || seqnum || ' day')
END WHEN ?2 = "WEEK" THEN DATE(read_window, '+' || (seqnum * 7) || ' day')
END,
time_offset
ORDER BY end_date DESC ORDER BY end_date DESC
), ),
max_streak AS ( max_streak AS (
@ -315,8 +320,8 @@ current_streak AS (
end_date AS current_streak_end_date end_date AS current_streak_end_date
FROM streaks FROM streaks
WHERE CASE WHERE CASE
WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', 'now', 'weekday 0', '-7 day') = current_streak_end_date WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', 'now', time_offset, 'weekday 0', '-7 day') = current_streak_end_date
WHEN ?2 = "DAY" THEN DATE('now', '-1 day') = current_streak_end_date OR DATE('now') = current_streak_end_date WHEN ?2 = "DAY" THEN DATE('now', time_offset, '-1 day') = current_streak_end_date OR DATE('now', time_offset) = current_streak_end_date
END END
LIMIT 1 LIMIT 1
) )

View File

@ -781,10 +781,12 @@ func (q *Queries) GetUser(ctx context.Context, userID string) (User, error) {
const getUserWindowStreaks = `-- name: GetUserWindowStreaks :one const getUserWindowStreaks = `-- name: GetUserWindowStreaks :one
WITH document_windows AS ( WITH document_windows AS (
SELECT CASE SELECT
WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', start_time, 'weekday 0', '-7 day', time_offset) CASE
WHEN ?2 = "DAY" THEN DATE(start_time, time_offset) WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', start_time, time_offset, 'weekday 0', '-7 day')
END AS read_window WHEN ?2 = "DAY" THEN DATE(start_time, time_offset)
END AS read_window,
time_offset
FROM activity FROM activity
JOIN users ON users.id = activity.user_id JOIN users ON users.id = activity.user_id
WHERE user_id = ?1 WHERE user_id = ?1
@ -794,7 +796,7 @@ WITH document_windows AS (
), ),
partitions AS ( partitions AS (
SELECT SELECT
document_windows.read_window, document_windows.read_window, document_windows.time_offset,
row_number() OVER ( row_number() OVER (
PARTITION BY 1 ORDER BY read_window DESC PARTITION BY 1 ORDER BY read_window DESC
) AS seqnum ) AS seqnum
@ -804,12 +806,15 @@ streaks AS (
SELECT SELECT
count(*) AS streak, count(*) AS streak,
MIN(read_window) AS start_date, MIN(read_window) AS start_date,
MAX(read_window) AS end_date MAX(read_window) AS end_date,
time_offset
FROM partitions FROM partitions
GROUP BY CASE GROUP BY
WHEN ?2 = "DAY" THEN DATE(read_window, '+' || seqnum || ' day') CASE
WHEN ?2 = "WEEK" THEN DATE(read_window, '+' || (seqnum * 7) || ' day') WHEN ?2 = "DAY" THEN DATE(read_window, '+' || seqnum || ' day')
END WHEN ?2 = "WEEK" THEN DATE(read_window, '+' || (seqnum * 7) || ' day')
END,
time_offset
ORDER BY end_date DESC ORDER BY end_date DESC
), ),
max_streak AS ( max_streak AS (
@ -826,8 +831,8 @@ current_streak AS (
end_date AS current_streak_end_date end_date AS current_streak_end_date
FROM streaks FROM streaks
WHERE CASE WHERE CASE
WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', 'now', 'weekday 0', '-7 day') = current_streak_end_date WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', 'now', time_offset, 'weekday 0', '-7 day') = current_streak_end_date
WHEN ?2 = "DAY" THEN DATE('now', '-1 day') = current_streak_end_date OR DATE('now') = current_streak_end_date WHEN ?2 = "DAY" THEN DATE('now', time_offset, '-1 day') = current_streak_end_date OR DATE('now', time_offset) = current_streak_end_date
END END
LIMIT 1 LIMIT 1
) )