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 }) => {
|
stacked: async (user, { when }, { models }) => {
|
||||||
if (user.stacked) {
|
if (user.stacked) {
|
||||||
return 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?
|
photoId Int?
|
||||||
photo Upload? @relation(fields: [photoId], references: [id])
|
photo Upload? @relation(fields: [photoId], references: [id])
|
||||||
|
|
||||||
|
// streak
|
||||||
|
streak Int?
|
||||||
|
|
||||||
// walkthrough
|
// walkthrough
|
||||||
upvotePopover Boolean @default(false)
|
upvotePopover Boolean @default(false)
|
||||||
tipPopover Boolean @default(false)
|
tipPopover Boolean @default(false)
|
||||||
|
|
|
@ -13,33 +13,47 @@ function computeStreaks ({ models }) {
|
||||||
FROM
|
FROM
|
||||||
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
||||||
FROM "ItemAct"
|
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")
|
GROUP BY "userId")
|
||||||
UNION ALL
|
UNION ALL
|
||||||
(SELECT "userId", sats as sats_spent
|
(SELECT "userId", sats as sats_spent
|
||||||
FROM "Donation"
|
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
|
)) spending
|
||||||
GROUP BY "userId"
|
GROUP BY "userId"
|
||||||
HAVING sum(sats_spent) >= ${STREAK_THRESHOLD}
|
HAVING sum(sats_spent) >= ${STREAK_THRESHOLD}
|
||||||
), existing_streaks (id) AS (
|
), existing_streaks (id, started_at) AS (
|
||||||
SELECT "userId"
|
SELECT "userId", "startedAt"
|
||||||
FROM "Streak"
|
FROM "Streak"
|
||||||
WHERE "Streak"."endedAt" IS NULL
|
WHERE "Streak"."endedAt" IS NULL
|
||||||
), new_streaks (id) AS (
|
), new_streaks (id) AS (
|
||||||
SELECT day_streaks.id
|
SELECT day_streaks.id
|
||||||
FROM day_streaks
|
FROM day_streaks
|
||||||
LEFT JOIN existing_streaks ON existing_streaks.id = day_streaks.id
|
LEFT JOIN existing_streaks ON existing_streaks.id = day_streaks.id
|
||||||
WHERE existing_streaks.id IS NULL
|
WHERE existing_streaks.id IS NULL
|
||||||
), ending_streaks (id) AS (
|
), ending_streaks (id) AS (
|
||||||
SELECT existing_streaks.id
|
SELECT existing_streaks.id
|
||||||
FROM existing_streaks
|
FROM existing_streaks
|
||||||
LEFT JOIN day_streaks ON existing_streaks.id = day_streaks.id
|
LEFT JOIN day_streaks ON existing_streaks.id = day_streaks.id
|
||||||
WHERE day_streaks.id IS NULL
|
WHERE day_streaks.id IS NULL
|
||||||
), streak_insert AS (
|
), extending_streaks (id, started_at) AS (
|
||||||
INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at)
|
SELECT existing_streaks.id, existing_streaks.started_at
|
||||||
SELECT id, (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, now_utc(), now_utc()
|
FROM existing_streaks
|
||||||
FROM new_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"
|
UPDATE "Streak"
|
||||||
SET "endedAt" = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, updated_at = now_utc()
|
SET "endedAt" = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, updated_at = now_utc()
|
||||||
|
@ -73,17 +87,19 @@ function checkStreak ({ models }) {
|
||||||
FROM
|
FROM
|
||||||
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent
|
||||||
FROM "ItemAct"
|
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)}
|
AND "userId" = ${Number(id)}
|
||||||
GROUP BY "userId")
|
GROUP BY "userId")
|
||||||
UNION ALL
|
UNION ALL
|
||||||
(SELECT "userId", sats as sats_spent
|
(SELECT "userId", sats as sats_spent
|
||||||
FROM "Donation"
|
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)}
|
AND "userId" = ${Number(id)}
|
||||||
)) spending
|
)) spending
|
||||||
GROUP BY "userId"
|
GROUP BY "userId"
|
||||||
HAVING sum(sats_spent) >= ${STREAK_THRESHOLD}
|
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)
|
INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at)
|
||||||
SELECT id, (now() AT TIME ZONE 'America/Chicago')::date, now_utc(), now_utc()
|
SELECT id, (now() AT TIME ZONE 'America/Chicago')::date, now_utc(), now_utc()
|
||||||
|
|
Loading…
Reference in New Issue