disconnect greedy rewards connection after they run
This commit is contained in:
parent
d14123cc42
commit
64e49934d4
129
worker/earn.js
129
worker/earn.js
@ -14,10 +14,12 @@ export async function earn ({ name }) {
|
|||||||
// however for user gen subs currently only 50% of their fees go to rewards
|
// however for user gen subs currently only 50% of their fees go to rewards
|
||||||
// the other 50% goes to the founder of the sub
|
// the other 50% goes to the founder of the sub
|
||||||
|
|
||||||
|
// grab a greedy connection
|
||||||
const models = new PrismaClient()
|
const models = new PrismaClient()
|
||||||
|
|
||||||
|
try {
|
||||||
// compute how much sn earned today
|
// compute how much sn earned today
|
||||||
const [{ sum: sumDecimal }] = await models.$queryRaw`
|
const [{ sum: sumDecimal }] = await models.$queryRaw`
|
||||||
SELECT coalesce(sum(msats), 0) as sum
|
SELECT coalesce(sum(msats), 0) as sum
|
||||||
FROM (
|
FROM (
|
||||||
(SELECT ("ItemAct".msats - COALESCE("ReferralAct".msats, 0)) * COALESCE("Sub"."rewardsPct", 100) * 0.01 as msats
|
(SELECT ("ItemAct".msats - COALESCE("ReferralAct".msats, 0)) * COALESCE("Sub"."rewardsPct", 100) * 0.01 as msats
|
||||||
@ -43,32 +45,32 @@ export async function earn ({ name }) {
|
|||||||
HAVING COUNT("ItemForward".id) = 0)
|
HAVING COUNT("ItemForward".id) = 0)
|
||||||
) subquery`
|
) subquery`
|
||||||
|
|
||||||
// XXX primsa will return a Decimal (https://mikemcl.github.io/decimal.js)
|
// XXX primsa will return a Decimal (https://mikemcl.github.io/decimal.js)
|
||||||
// because sum of a BIGINT returns a NUMERIC type (https://www.postgresql.org/docs/13/functions-aggregate.html)
|
// because sum of a BIGINT returns a NUMERIC type (https://www.postgresql.org/docs/13/functions-aggregate.html)
|
||||||
// and Decimal is what prisma maps it to https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#raw-query-type-mapping
|
// and Decimal is what prisma maps it to https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#raw-query-type-mapping
|
||||||
// so check it before coercing to Number
|
// so check it before coercing to Number
|
||||||
if (!sumDecimal || sumDecimal.lessThanOrEqualTo(0)) {
|
if (!sumDecimal || sumDecimal.lessThanOrEqualTo(0)) {
|
||||||
console.log('done', name, 'no sats to award today')
|
console.log('done', name, 'no sats to award today')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// extra sanity check on rewards ... if it's more than upper bound, we
|
// extra sanity check on rewards ... if it's more than upper bound, we
|
||||||
// probably have a bug somewhere or we've grown A LOT
|
// probably have a bug somewhere or we've grown A LOT
|
||||||
if (sumDecimal.greaterThan(TOTAL_UPPER_BOUND_MSATS)) {
|
if (sumDecimal.greaterThan(TOTAL_UPPER_BOUND_MSATS)) {
|
||||||
console.log('done', name, 'error: too many sats to award today', sumDecimal)
|
console.log('done', name, 'error: too many sats to award today', sumDecimal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const sum = Number(sumDecimal)
|
const sum = Number(sumDecimal)
|
||||||
const heads = Math.random() < 0.5
|
const heads = Math.random() < 0.5
|
||||||
// if this category is selected, double its proportion
|
// if this category is selected, double its proportion
|
||||||
// if it isn't select, zero its proportion
|
// if it isn't select, zero its proportion
|
||||||
const itemRewardMult = heads ? 0 : 2.0
|
const itemRewardMult = heads ? 0 : 2.0
|
||||||
const upvoteRewardMult = heads ? 2.0 : 0
|
const upvoteRewardMult = heads ? 2.0 : 0
|
||||||
|
|
||||||
console.log(name, 'giving away', sum, 'msats', 'rewarding', heads ? 'items' : 'upvotes')
|
console.log(name, 'giving away', sum, 'msats', 'rewarding', heads ? 'items' : 'upvotes')
|
||||||
|
|
||||||
/*
|
/*
|
||||||
How earnings (used to) work:
|
How earnings (used to) work:
|
||||||
1/3: top 21% posts over last 36 hours, scored on a relative basis
|
1/3: top 21% posts over last 36 hours, scored on a relative basis
|
||||||
1/3: top 21% comments over last 36 hours, scored on a relative basis
|
1/3: top 21% comments over last 36 hours, scored on a relative basis
|
||||||
@ -81,8 +83,8 @@ export async function earn ({ name }) {
|
|||||||
Now: 100% of earnings go to either top 33% of comments/posts or top 33% of upvoters
|
Now: 100% of earnings go to either top 33% of comments/posts or top 33% of upvoters
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// get earners { userId, id, type, rank, proportion }
|
// get earners { userId, id, type, rank, proportion }
|
||||||
const earners = await models.$queryRaw`
|
const earners = await models.$queryRaw`
|
||||||
-- get top 21% of posts and comments
|
-- get top 21% of posts and comments
|
||||||
WITH item_ratios AS (
|
WITH item_ratios AS (
|
||||||
SELECT *,
|
SELECT *,
|
||||||
@ -143,56 +145,59 @@ export async function earn ({ name }) {
|
|||||||
FROM proportions
|
FROM proportions
|
||||||
WHERE proportion > 0.000001`
|
WHERE proportion > 0.000001`
|
||||||
|
|
||||||
// in order to group earnings for users we use the same createdAt time for
|
// in order to group earnings for users we use the same createdAt time for
|
||||||
// all earnings
|
// 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
|
// this is just a sanity check because it seems like a good idea
|
||||||
let total = 0
|
let total = 0
|
||||||
|
|
||||||
const notifications = {}
|
const notifications = {}
|
||||||
for (const earner of earners) {
|
for (const earner of earners) {
|
||||||
const earnings = Math.floor(parseFloat(earner.proportion) * sum)
|
const earnings = Math.floor(parseFloat(earner.proportion) * sum)
|
||||||
total += earnings
|
total += earnings
|
||||||
if (total > sum) {
|
if (total > sum) {
|
||||||
console.log(name, 'total exceeds sum', total, '>', sum)
|
console.log(name, 'total exceeds sum', total, '>', sum)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('stacker', earner.userId, 'earned', earnings, 'proportion', earner.proportion, 'rank', earner.rank, 'type', earner.type)
|
console.log('stacker', earner.userId, 'earned', earnings, 'proportion', earner.proportion, 'rank', earner.rank, 'type', earner.type)
|
||||||
|
|
||||||
if (earnings > 0) {
|
if (earnings > 0) {
|
||||||
await serialize(models,
|
await serialize(models,
|
||||||
models.$executeRaw`SELECT earn(${earner.userId}::INTEGER, ${earnings},
|
models.$executeRaw`SELECT earn(${earner.userId}::INTEGER, ${earnings},
|
||||||
${now}::timestamp without time zone, ${earner.type}::"EarnType", ${earner.id}::INTEGER, ${earner.rank}::INTEGER)`)
|
${now}::timestamp without time zone, ${earner.type}::"EarnType", ${earner.id}::INTEGER, ${earner.rank}::INTEGER)`)
|
||||||
|
|
||||||
const userN = notifications[earner.userId] || {}
|
const userN = notifications[earner.userId] || {}
|
||||||
|
|
||||||
// sum total
|
// sum total
|
||||||
const prevMsats = userN.msats || 0
|
const prevMsats = userN.msats || 0
|
||||||
const msats = earnings + prevMsats
|
const msats = earnings + prevMsats
|
||||||
|
|
||||||
// sum total per earn type (POST, COMMENT, TIP_COMMENT, TIP_POST)
|
// sum total per earn type (POST, COMMENT, TIP_COMMENT, TIP_POST)
|
||||||
const prevEarnTypeMsats = userN[earner.type]?.msats || 0
|
const prevEarnTypeMsats = userN[earner.type]?.msats || 0
|
||||||
const earnTypeMsats = earnings + prevEarnTypeMsats
|
const earnTypeMsats = earnings + prevEarnTypeMsats
|
||||||
|
|
||||||
// best (=lowest) rank per earn type
|
// best (=lowest) rank per earn type
|
||||||
const prevEarnTypeBestRank = userN[earner.type]?.bestRank
|
const prevEarnTypeBestRank = userN[earner.type]?.bestRank
|
||||||
const earnTypeBestRank = prevEarnTypeBestRank ? Math.min(prevEarnTypeBestRank, Number(earner.rank)) : Number(earner.rank)
|
const earnTypeBestRank = prevEarnTypeBestRank ? Math.min(prevEarnTypeBestRank, Number(earner.rank)) : Number(earner.rank)
|
||||||
|
|
||||||
notifications[earner.userId] = {
|
notifications[earner.userId] = {
|
||||||
...userN,
|
...userN,
|
||||||
msats,
|
msats,
|
||||||
[earner.type]: { msats: earnTypeMsats, bestRank: earnTypeBestRank }
|
[earner.type]: { msats: earnTypeMsats, bestRank: earnTypeBestRank }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await territoryRevenue({ models })
|
||||||
|
|
||||||
|
Promise.allSettled(Object.entries(notifications).map(([userId, earnings]) =>
|
||||||
|
sendUserNotification(parseInt(userId, 10), buildUserNotification(earnings))
|
||||||
|
)).catch(console.error)
|
||||||
|
} finally {
|
||||||
|
models.$disconnect().catch(console.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
await territoryRevenue({ models })
|
|
||||||
|
|
||||||
Promise.allSettled(Object.entries(notifications).map(([userId, earnings]) =>
|
|
||||||
sendUserNotification(parseInt(userId, 10), buildUserNotification(earnings))
|
|
||||||
)).catch(console.error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function territoryRevenue ({ models }) {
|
async function territoryRevenue ({ models }) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user