denormalize user streak to avoid extra dip
This commit is contained in:
parent
c1080ec4d7
commit
3a9994a9a6
|
@ -490,15 +490,6 @@ export default {
|
|||
}
|
||||
})
|
||||
},
|
||||
streak: async (user, args, { models }) => {
|
||||
const res = await models.$queryRaw`
|
||||
SELECT (now() at time zone 'America/Chicago')::date - "startedAt" AS days
|
||||
FROM "Streak"
|
||||
WHERE "userId" = ${user.id} AND "endedAt" IS NULL
|
||||
`
|
||||
|
||||
return res.length ? res[0].days : null
|
||||
},
|
||||
stacked: async (user, { when }, { models }) => {
|
||||
if (user.stacked) {
|
||||
return user.stacked
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "users" ADD COLUMN "streak" INTEGER;
|
|
@ -0,0 +1,3 @@
|
|||
UPDATE users set streak = (now() AT TIME ZONE 'America/Chicago')::date - "Streak"."startedAt"
|
||||
FROM "Streak"
|
||||
WHERE "endedAt" IS NULL AND users.id = "Streak"."userId";
|
|
@ -45,6 +45,9 @@ model User {
|
|||
photoId Int?
|
||||
photo Upload? @relation(fields: [photoId], references: [id])
|
||||
|
||||
// streak
|
||||
streak Int?
|
||||
|
||||
// walkthrough
|
||||
upvotePopover Boolean @default(false)
|
||||
tipPopover Boolean @default(false)
|
||||
|
|
|
@ -13,17 +13,17 @@ function computeStreaks ({ models }) {
|
|||
FROM
|
||||
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
||||
FROM "ItemAct"
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date >= (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date
|
||||
GROUP BY "userId")
|
||||
UNION ALL
|
||||
(SELECT "userId", sats as sats_spent
|
||||
FROM "Donation"
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date >= (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date
|
||||
)) spending
|
||||
GROUP BY "userId"
|
||||
HAVING sum(sats_spent) >= ${STREAK_THRESHOLD}
|
||||
), existing_streaks (id) AS (
|
||||
SELECT "userId"
|
||||
), existing_streaks (id, started_at) AS (
|
||||
SELECT "userId", "startedAt"
|
||||
FROM "Streak"
|
||||
WHERE "Streak"."endedAt" IS NULL
|
||||
), new_streaks (id) AS (
|
||||
|
@ -36,10 +36,24 @@ function computeStreaks ({ models }) {
|
|||
FROM existing_streaks
|
||||
LEFT JOIN day_streaks ON existing_streaks.id = day_streaks.id
|
||||
WHERE day_streaks.id IS NULL
|
||||
), streak_insert AS (
|
||||
), extending_streaks (id, started_at) AS (
|
||||
SELECT existing_streaks.id, existing_streaks.started_at
|
||||
FROM existing_streaks
|
||||
JOIN day_streaks ON existing_streaks.id = day_streaks.id
|
||||
),
|
||||
-- a bunch of mutations
|
||||
streak_insert AS (
|
||||
INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at)
|
||||
SELECT id, (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, now_utc(), now_utc()
|
||||
FROM new_streaks
|
||||
), user_update_new_streaks AS (
|
||||
UPDATE users SET streak = 1 FROM new_streaks WHERE new_streaks.id = users.id
|
||||
), user_update_end_streaks AS (
|
||||
UPDATE users SET streak = NULL FROM ending_streaks WHERE ending_streaks.id = users.id
|
||||
), user_update_extend_streaks AS (
|
||||
UPDATE users
|
||||
SET streak = (now() AT TIME ZONE 'America/Chicago')::date - extending_streaks.started_at
|
||||
FROM extending_streaks WHERE extending_streaks.id = users.id
|
||||
)
|
||||
UPDATE "Streak"
|
||||
SET "endedAt" = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, updated_at = now_utc()
|
||||
|
@ -73,17 +87,19 @@ function checkStreak ({ models }) {
|
|||
FROM
|
||||
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
||||
FROM "ItemAct"
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date = (now() AT TIME ZONE 'America/Chicago')::date
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date >= (now() AT TIME ZONE 'America/Chicago')::date
|
||||
AND "userId" = ${Number(id)}
|
||||
GROUP BY "userId")
|
||||
UNION ALL
|
||||
(SELECT "userId", sats as sats_spent
|
||||
FROM "Donation"
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date = (now() AT TIME ZONE 'America/Chicago')::date
|
||||
WHERE (created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago')::date >= (now() AT TIME ZONE 'America/Chicago')::date
|
||||
AND "userId" = ${Number(id)}
|
||||
)) spending
|
||||
GROUP BY "userId"
|
||||
HAVING sum(sats_spent) >= ${STREAK_THRESHOLD}
|
||||
), user_start_streak AS (
|
||||
UPDATE users SET streak = 0 FROM streak_started WHERE streak_started.id = users.id
|
||||
)
|
||||
INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at)
|
||||
SELECT id, (now() AT TIME ZONE 'America/Chicago')::date, now_utc(), now_utc()
|
||||
|
|
Loading…
Reference in New Issue