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
|
||||
// the other 50% goes to the founder of the sub
|
||||
|
||||
// grab a greedy connection
|
||||
const models = new PrismaClient()
|
||||
|
||||
try {
|
||||
// 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
|
||||
FROM (
|
||||
(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)
|
||||
) subquery`
|
||||
|
||||
// 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)
|
||||
// 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
|
||||
if (!sumDecimal || sumDecimal.lessThanOrEqualTo(0)) {
|
||||
console.log('done', name, 'no sats to award today')
|
||||
return
|
||||
}
|
||||
// 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)
|
||||
// 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
|
||||
if (!sumDecimal || sumDecimal.lessThanOrEqualTo(0)) {
|
||||
console.log('done', name, 'no sats to award today')
|
||||
return
|
||||
}
|
||||
|
||||
// extra sanity check on rewards ... if it's more than upper bound, we
|
||||
// probably have a bug somewhere or we've grown A LOT
|
||||
if (sumDecimal.greaterThan(TOTAL_UPPER_BOUND_MSATS)) {
|
||||
console.log('done', name, 'error: too many sats to award today', sumDecimal)
|
||||
return
|
||||
}
|
||||
// extra sanity check on rewards ... if it's more than upper bound, we
|
||||
// probably have a bug somewhere or we've grown A LOT
|
||||
if (sumDecimal.greaterThan(TOTAL_UPPER_BOUND_MSATS)) {
|
||||
console.log('done', name, 'error: too many sats to award today', sumDecimal)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
const itemRewardMult = heads ? 0 : 2.0
|
||||
const upvoteRewardMult = heads ? 2.0 : 0
|
||||
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
|
||||
const itemRewardMult = heads ? 0 : 2.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:
|
||||
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
|
||||
@ -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
|
||||
*/
|
||||
|
||||
// get earners { userId, id, type, rank, proportion }
|
||||
const earners = await models.$queryRaw`
|
||||
// get earners { userId, id, type, rank, proportion }
|
||||
const earners = await models.$queryRaw`
|
||||
-- get top 21% of posts and comments
|
||||
WITH item_ratios AS (
|
||||
SELECT *,
|
||||
@ -143,56 +145,59 @@ export async function earn ({ name }) {
|
||||
FROM proportions
|
||||
WHERE proportion > 0.000001`
|
||||
|
||||
// in order to group earnings for users we use the same createdAt time for
|
||||
// all earnings
|
||||
const now = new Date(new Date().getTime())
|
||||
// in order to group earnings for users we use the same createdAt time for
|
||||
// all earnings
|
||||
const now = new Date(new Date().getTime())
|
||||
|
||||
// this is just a sanity check because it seems like a good idea
|
||||
let total = 0
|
||||
// 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
|
||||
}
|
||||
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)
|
||||
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},
|
||||
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)`)
|
||||
|
||||
const userN = notifications[earner.userId] || {}
|
||||
const userN = notifications[earner.userId] || {}
|
||||
|
||||
// sum total
|
||||
const prevMsats = userN.msats || 0
|
||||
const msats = earnings + prevMsats
|
||||
// sum total
|
||||
const prevMsats = userN.msats || 0
|
||||
const msats = earnings + prevMsats
|
||||
|
||||
// sum total per earn type (POST, COMMENT, TIP_COMMENT, TIP_POST)
|
||||
const prevEarnTypeMsats = userN[earner.type]?.msats || 0
|
||||
const earnTypeMsats = earnings + prevEarnTypeMsats
|
||||
// sum total per earn type (POST, COMMENT, TIP_COMMENT, TIP_POST)
|
||||
const prevEarnTypeMsats = userN[earner.type]?.msats || 0
|
||||
const earnTypeMsats = earnings + prevEarnTypeMsats
|
||||
|
||||
// best (=lowest) rank per earn type
|
||||
const prevEarnTypeBestRank = userN[earner.type]?.bestRank
|
||||
const earnTypeBestRank = prevEarnTypeBestRank ? Math.min(prevEarnTypeBestRank, Number(earner.rank)) : Number(earner.rank)
|
||||
// best (=lowest) rank per earn type
|
||||
const prevEarnTypeBestRank = userN[earner.type]?.bestRank
|
||||
const earnTypeBestRank = prevEarnTypeBestRank ? Math.min(prevEarnTypeBestRank, Number(earner.rank)) : Number(earner.rank)
|
||||
|
||||
notifications[earner.userId] = {
|
||||
...userN,
|
||||
msats,
|
||||
[earner.type]: { msats: earnTypeMsats, bestRank: earnTypeBestRank }
|
||||
notifications[earner.userId] = {
|
||||
...userN,
|
||||
msats,
|
||||
[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 }) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user