Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix earn push notifications #560

Merged
merged 1 commit into from
Oct 21, 2023
Merged

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented Oct 13, 2023

Fixes #530 (comment)

Changes

Tested this manually with some mock data:

diff --git a/worker/earn.js b/worker/earn.js
index b0f00fd..73c926c 100644
--- a/worker/earn.js
+++ b/worker/earn.js
@@ -13,6 +13,62 @@ export function earn ({ models }) {
   return async function ({ name }) {
     console.log('running', name)
 
+    const now = new Date(new Date().getTime())
+    const sum = 1e8
+    let total = 0
+    let earners = [
+      {
+        userId: 14912,
+        proportion: 0.041,
+        rank: 21,
+        type: 'POST'
+      },
+      {
+        userId: 14912,
+        proportion: 0.11,
+        rank: 3,
+        type: 'COMMENT'
+      },
+      {
+        userId: 14912,
+        proportion: 0.021,
+        rank: 32,
+        type: 'TIP_POST'
+      },
+      {
+        userId: 14912,
+        proportion: 0.041,
+        rank: 13,
+        type: 'TIP_COMMENT'
+      }
+    ]
+    const notifications = {}
+    for (const earner of earners) {
+      const earnings = Math.floor(parseFloat(earner.proportion) * sum)
+      total += earnings
+      if (total > sum) {
+        console.log(name, 'total exceeds sum', total, '>', sum)
+        return
+      }
+
+      console.log('stacker', earner.userId, 'earned', earnings, 'proportion', earner.proportion, 'rank', earner.rank, 'type', earner.type)
+
+      if (earnings > 0) {
+        await serialize(models,
+          models.$executeRaw`SELECT earn(${earner.userId}::INTEGER, ${earnings},
+          ${now}::timestamp without time zone, ${earner.type}::"EarnType", ${earner.id}::INTEGER, ${earner.rank}::INTEGER)`)
+        notifications[earner.userId] = {
+          ...notifications[earner.userId],
+          total: earnings + (notifications[earner.userId]?.total || 0),
+          [earner.type]: { msats: earnings, rank: earner.rank }
+        }
+      }
+    }
+
+    Promise.allSettled(Object.entries(notifications).map(([userId, earnings]) =>
+      sendUserNotification(parseInt(userId, 10), buildUserNotification(earnings))
+    )).catch(console.error)
+
     // compute how much sn earned today
     const [{ sum: sumDecimal }] = await models.$queryRaw`
       SELECT coalesce(sum(msats), 0) as sum
@@ -54,7 +110,7 @@ export function earn ({ models }) {
       return
     }
 
-    const sum = Number(sumDecimal)
+    // const sum = Number(sumDecimal)
     const heads = Math.random() < 0.5
     // if this category is selected, double its proportion
     // if it isn't select, zero its proportion
@@ -77,7 +133,7 @@ export function earn ({ models }) {
     */
 
     // get earners { userId, id, type, rank, proportion }
-    const earners = await models.$queryRaw`
+    earners = await models.$queryRaw`
       -- get top 21% of posts and comments
       WITH item_ratios AS (
           SELECT *,
@@ -140,37 +196,10 @@ export function earn ({ models }) {
 
     // in order to group earnings for users we use the same createdAt time for
     // all earnings
-    const now = new Date(new Date().getTime())
+    // const now = new Date(new Date().getTime())
 
     // this is just a sanity check because it seems like a good idea
-    let total = 0
-
-    const notifications = {}
-    for (const earner of earners) {
-      const earnings = Math.floor(parseFloat(earner.proportion) * sum)
-      total += earnings
-      if (total > sum) {
-        console.log(name, 'total exceeds sum', total, '>', sum)
-        return
-      }
-
-      console.log('stacker', earner.userId, 'earned', earnings, 'proportion', earner.proportion, 'rank', earner.rank, 'type', earner.type)
-
-      if (earnings > 0) {
-        await serialize(models,
-          models.$executeRaw`SELECT earn(${earner.userId}::INTEGER, ${earnings},
-          ${now}::timestamp without time zone, ${earner.type}::"EarnType", ${earner.id}::INTEGER, ${earner.rank}::INTEGER)`)
-        notifications[earner.userId] = {
-          ...notifications[earner.userId],
-          total: earnings + (notifications[earner.userId]?.total || 0),
-          [earner.type]: { msats: earnings, rank: earner.rank }
-        }
-      }
-    }
-
-    Promise.allSettled(Object.entries(notifications).map(([userId, earnings]) =>
-      sendUserNotification(parseInt(userId, 10), buildUserNotification(earnings))
-    )).catch(console.error)
+    // const total = 0
 
     console.log('done', name)
   }

2023-10-13-214800_317x227_scrot

@ekzyis ekzyis added the bug label Oct 13, 2023
@ekzyis ekzyis force-pushed the fix-earn-notification branch from 2288e5c to 942b1e0 Compare October 13, 2023 19:52
@huumn
Copy link
Member

huumn commented Oct 13, 2023

just fyi another way to mock is to use the scrubbed DB I sent you and fast-forward all the items/itemacts by incrementing the createdAt times

@huumn huumn merged commit 162c959 into stackernews:master Oct 21, 2023
1 check passed
Sign up for free to 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