improvements to earning

This commit is contained in:
keyan 2022-03-18 07:29:02 -05:00
parent 5ff856d061
commit 7a78210cb6
3 changed files with 59 additions and 3 deletions

View File

@ -161,7 +161,16 @@ export default {
WHERE "ItemAct"."userId" <> ${user.id} AND "ItemAct".act <> 'BOOST' WHERE "ItemAct"."userId" <> ${user.id} AND "ItemAct".act <> 'BOOST'
AND "Item"."userId" = ${user.id}` AND "Item"."userId" = ${user.id}`
return sum || 0 const { sum: { msats } } = await models.earn.aggregate({
sum: {
msats: true
},
where: {
userId: Number(user.id)
}
})
return (sum || 0) + Math.floor((msats || 0) / 1000)
}, },
sats: async (user, args, { models, me }) => { sats: async (user, args, { models, me }) => {
if (me?.id !== user.id) { if (me?.id !== user.id) {
@ -241,6 +250,9 @@ export default {
userId: user.id, userId: user.id,
createdAt: { createdAt: {
gt: user.checkedNotesAt || new Date(0) gt: user.checkedNotesAt || new Date(0)
},
msats: {
gte: 1000
} }
} }
}) })

View File

@ -41,4 +41,47 @@ function earn ({ models }) {
} }
} }
module.exports = { earn } // earn historical ... TODO: delete after announcement
function earnHistorical ({ models }) {
return async function ({ name }) {
console.log('running', name)
// compute how much sn earned today
const [{ sum }] = await models.$queryRaw`
SELECT sum("ItemAct".sats)
FROM "ItemAct"
JOIN "Item" on "ItemAct"."itemId" = "Item".id
WHERE ("ItemAct".act in ('BOOST', 'STREAM')
OR ("ItemAct".act = 'VOTE' AND "Item"."userId" = "ItemAct"."userId"))`
// add in the job sats that weren't recorded from jobs
const fullSum = 200000 + sum
// calculate the total trust
const { sum: { trust } } = await models.user.aggregate({
sum: {
trust: true
}
})
// get earners { id, earnings }
const earners = await models.$queryRaw(`
SELECT id, FLOOR(${fullSum} * (trust/${trust}) * 1000) as earnings
FROM users
WHERE trust > 0`)
// for each earner, serialize earnings
// we do this for each earner because we don't need to serialize
// all earner updates together
earners.forEach(async earner => {
if (earner.earnings > 0) {
await serialize(models,
models.$executeRaw`SELECT earn(${earner.id}, ${earner.earnings})`)
}
})
console.log('done', name)
}
}
module.exports = { earn, earnHistorical }

View File

@ -6,7 +6,7 @@ const { checkInvoice, checkWithdrawal } = require('./wallet')
const { repin } = require('./repin') const { repin } = require('./repin')
const { trust } = require('./trust') const { trust } = require('./trust')
const { auction } = require('./auction') const { auction } = require('./auction')
const { earn } = require('./earn') const { earn, earnHistorical } = require('./earn')
const { ApolloClient, HttpLink, InMemoryCache } = require('@apollo/client') const { ApolloClient, HttpLink, InMemoryCache } = require('@apollo/client')
const { indexItem, indexAllItems } = require('./search') const { indexItem, indexAllItems } = require('./search')
const fetch = require('cross-fetch') const fetch = require('cross-fetch')
@ -45,6 +45,7 @@ async function work () {
await boss.work('indexAllItems', indexAllItems(args)) await boss.work('indexAllItems', indexAllItems(args))
await boss.work('auction', auction(args)) await boss.work('auction', auction(args))
await boss.work('earn', earn(args)) await boss.work('earn', earn(args))
await boss.work('earnHistorical', earnHistorical(args))
console.log('working jobs') console.log('working jobs')
} }