improved earning: more detail, longer top tail
This commit is contained in:
parent
1b6a7e7f95
commit
08893d020c
|
@ -162,18 +162,20 @@ export default {
|
||||||
|
|
||||||
if (meFull.noteEarning) {
|
if (meFull.noteEarning) {
|
||||||
queries.push(
|
queries.push(
|
||||||
`SELECT id::text, created_at AS "sortTime", FLOOR(msats / 1000) as "earnedSats",
|
`SELECT min(id)::text, created_at AS "sortTime", FLOOR(sum(msats) / 1000) as "earnedSats",
|
||||||
'Earn' AS type
|
'Earn' AS type
|
||||||
FROM "Earn"
|
FROM "Earn"
|
||||||
WHERE "userId" = $1
|
WHERE "userId" = $1
|
||||||
AND created_at <= $2`
|
AND created_at <= $2
|
||||||
|
GROUP BY "userId", created_at`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we do all this crazy subquery stuff to make 'reward' islands
|
// we do all this crazy subquery stuff to make 'reward' islands
|
||||||
const notifications = await models.$queryRaw(
|
const notifications = await models.$queryRaw(
|
||||||
`SELECT MAX(id) AS id, MAX("sortTime") AS "sortTime", sum("earnedSats") AS "earnedSats", type
|
`SELECT MAX(id) AS id, MAX("sortTime") AS "sortTime", sum("earnedSats") AS "earnedSats", type,
|
||||||
|
MIN("sortTime") AS "minSortTime"
|
||||||
FROM
|
FROM
|
||||||
(SELECT *,
|
(SELECT *,
|
||||||
CASE
|
CASE
|
||||||
|
@ -214,6 +216,26 @@ export default {
|
||||||
JobChanged: {
|
JobChanged: {
|
||||||
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
||||||
},
|
},
|
||||||
|
Earn: {
|
||||||
|
sources: async (n, args, { me, models }) => {
|
||||||
|
const [sources] = await models.$queryRaw(`
|
||||||
|
SELECT
|
||||||
|
FLOOR(sum(msats) FILTER(WHERE type = 'POST') / 1000) AS posts,
|
||||||
|
FLOOR(sum(msats) FILTER(WHERE type = 'COMMENT') / 1000) AS comments,
|
||||||
|
FLOOR(sum(msats) FILTER(WHERE type = 'TIP_POST' OR type = 'TIP_COMMENT') / 1000) AS tips
|
||||||
|
FROM "Earn"
|
||||||
|
WHERE "userId" = $1 AND created_at <= $2 AND created_at >= $3
|
||||||
|
`, Number(me.id), new Date(n.sortTime), new Date(n.minSortTime))
|
||||||
|
sources.posts ||= 0
|
||||||
|
sources.comments ||= 0
|
||||||
|
sources.tips ||= 0
|
||||||
|
if (sources.posts + sources.comments + sources.tips > 0) {
|
||||||
|
return sources
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
Mention: {
|
Mention: {
|
||||||
mention: async (n, args, { models }) => true,
|
mention: async (n, args, { models }) => true,
|
||||||
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
||||||
|
|
|
@ -103,11 +103,12 @@ export default {
|
||||||
AND "ItemAct".created_at <= $2
|
AND "ItemAct".created_at <= $2
|
||||||
GROUP BY "Item".id)`)
|
GROUP BY "Item".id)`)
|
||||||
queries.push(
|
queries.push(
|
||||||
`(SELECT ('earn' || "Earn".id) as id, "Earn".id as "factId", NULL as bolt11,
|
`(SELECT ('earn' || min("Earn".id)) as id, min("Earn".id) as "factId", NULL as bolt11,
|
||||||
created_at as "createdAt", msats,
|
created_at as "createdAt", sum(msats),
|
||||||
0 as "msatsFee", NULL as status, 'earn' as type
|
0 as "msatsFee", NULL as status, 'earn' as type
|
||||||
FROM "Earn"
|
FROM "Earn"
|
||||||
WHERE "Earn"."userId" = $1 AND "Earn".created_at <= $2)`)
|
WHERE "Earn"."userId" = $1 AND "Earn".created_at <= $2
|
||||||
|
GROUP BY "userId", created_at)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (include.has('spent')) {
|
if (include.has('spent')) {
|
||||||
|
|
|
@ -32,9 +32,16 @@ export default gql`
|
||||||
sortTime: String!
|
sortTime: String!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EarnSources {
|
||||||
|
posts: Int!
|
||||||
|
comments: Int!
|
||||||
|
tips: Int!
|
||||||
|
}
|
||||||
|
|
||||||
type Earn {
|
type Earn {
|
||||||
earnedSats: Int!
|
earnedSats: Int!
|
||||||
sortTime: String!
|
sortTime: String!
|
||||||
|
sources: EarnSources
|
||||||
}
|
}
|
||||||
|
|
||||||
type InvoicePaid {
|
type InvoicePaid {
|
||||||
|
|
|
@ -74,8 +74,14 @@ function Notification ({ n }) {
|
||||||
<HandCoin className='align-self-center fill-boost mx-1' width={24} height={24} style={{ flex: '0 0 24px', transform: 'rotateY(180deg)' }} />
|
<HandCoin className='align-self-center fill-boost mx-1' width={24} height={24} style={{ flex: '0 0 24px', transform: 'rotateY(180deg)' }} />
|
||||||
<div className='ml-2'>
|
<div className='ml-2'>
|
||||||
<div className='font-weight-bold text-boost'>
|
<div className='font-weight-bold text-boost'>
|
||||||
you stacked {n.earnedSats} sats <small className='text-muted ml-1'>{timeSince(new Date(n.sortTime))}</small>
|
you stacked {n.earnedSats} sats in rewards<small className='text-muted ml-1'>{timeSince(new Date(n.sortTime))}</small>
|
||||||
</div>
|
</div>
|
||||||
|
{n.sources &&
|
||||||
|
<div style={{ fontSize: '80%', color: 'var(--theme-grey)' }}>
|
||||||
|
{n.sources.posts > 0 && <span>{n.sources.posts} sats for top posts</span>}
|
||||||
|
{n.sources.comments > 0 && <span>{n.sources.posts > 0 && ' \\ '}{n.sources.comments} sats for top comments</span>}
|
||||||
|
{n.sources.tips > 0 && <span>{(n.sources.comments > 0 || n.sources.posts > 0) && ' \\ '}{n.sources.tips} sats for tipping top content early</span>}
|
||||||
|
</div>}
|
||||||
<div className='pb-1' style={{ lineHeight: '140%' }}>
|
<div className='pb-1' style={{ lineHeight: '140%' }}>
|
||||||
SN distributes the sats it earns back to its best users daily. These sats come from <Link href='/~jobs' passHref><a>jobs</a></Link>, boost, and posting fees.
|
SN distributes the sats it earns back to its best users daily. These sats come from <Link href='/~jobs' passHref><a>jobs</a></Link>, boost, and posting fees.
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -31,6 +31,11 @@ export const NOTIFICATIONS = gql`
|
||||||
... on Earn {
|
... on Earn {
|
||||||
sortTime
|
sortTime
|
||||||
earnedSats
|
earnedSats
|
||||||
|
sources {
|
||||||
|
posts
|
||||||
|
comments
|
||||||
|
tips
|
||||||
|
}
|
||||||
}
|
}
|
||||||
... on Reply {
|
... on Reply {
|
||||||
sortTime
|
sortTime
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "EarnType" AS ENUM ('POST', 'COMMENT', 'TIP_COMMENT', 'TIP_POST');
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Earn" ADD COLUMN "rank" INTEGER,
|
||||||
|
ADD COLUMN "type" "EarnType",
|
||||||
|
ADD COLUMN "typeId" INTEGER;
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE INDEX "Earn.created_at_userId_index" ON "Earn"("created_at", "userId");
|
|
@ -0,0 +1,16 @@
|
||||||
|
CREATE OR REPLACE FUNCTION earn(user_id INTEGER, earn_msats INTEGER, created_at TIMESTAMP(3),
|
||||||
|
type "EarnType", type_id INTEGER, rank INTEGER)
|
||||||
|
RETURNS void AS $$
|
||||||
|
DECLARE
|
||||||
|
BEGIN
|
||||||
|
PERFORM ASSERT_SERIALIZED();
|
||||||
|
-- insert into earn
|
||||||
|
INSERT INTO "Earn" (msats, "userId", created_at, type, "typeId", rank)
|
||||||
|
VALUES (earn_msats, user_id, created_at, type, type_id, rank);
|
||||||
|
|
||||||
|
-- give the user the sats
|
||||||
|
UPDATE users
|
||||||
|
SET msats = msats + earn_msats, "stackedMsats" = "stackedMsats" + earn_msats
|
||||||
|
WHERE id = user_id;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
|
@ -88,6 +88,13 @@ model Upload {
|
||||||
@@index([userId])
|
@@index([userId])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum EarnType {
|
||||||
|
POST
|
||||||
|
COMMENT
|
||||||
|
TIP_COMMENT
|
||||||
|
TIP_POST
|
||||||
|
}
|
||||||
|
|
||||||
model Earn {
|
model Earn {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
createdAt DateTime @default(now()) @map(name: "created_at")
|
createdAt DateTime @default(now()) @map(name: "created_at")
|
||||||
|
@ -97,8 +104,13 @@ model Earn {
|
||||||
user User @relation(fields: [userId], references: [id])
|
user User @relation(fields: [userId], references: [id])
|
||||||
userId Int
|
userId Int
|
||||||
|
|
||||||
|
type EarnType?
|
||||||
|
typeId Int?
|
||||||
|
rank Int?
|
||||||
|
|
||||||
@@index([createdAt])
|
@@index([createdAt])
|
||||||
@@index([userId])
|
@@index([userId])
|
||||||
|
@@index([createdAt, userId])
|
||||||
}
|
}
|
||||||
|
|
||||||
model LnAuth {
|
model LnAuth {
|
||||||
|
|
|
@ -2,8 +2,7 @@ const serialize = require('../api/resolvers/serial')
|
||||||
|
|
||||||
const ITEM_EACH_REWARD = 3.0
|
const ITEM_EACH_REWARD = 3.0
|
||||||
const UPVOTE_EACH_REWARD = 6.0
|
const UPVOTE_EACH_REWARD = 6.0
|
||||||
const TOP_ITEMS = 21
|
const TOP_PERCENTILE = 21
|
||||||
const EARLY_MULTIPLIER_MAX = 100.0
|
|
||||||
|
|
||||||
// TODO: use a weekly trust measure or make trust decay
|
// TODO: use a weekly trust measure or make trust decay
|
||||||
function earn ({ models }) {
|
function earn ({ models }) {
|
||||||
|
@ -11,7 +10,7 @@ function earn ({ models }) {
|
||||||
console.log('running', name)
|
console.log('running', name)
|
||||||
|
|
||||||
// compute how much sn earned today
|
// compute how much sn earned today
|
||||||
const [{ sum }] = await models.$queryRaw`
|
let [{ sum }] = await models.$queryRaw`
|
||||||
SELECT sum("ItemAct".sats)
|
SELECT sum("ItemAct".sats)
|
||||||
FROM "ItemAct"
|
FROM "ItemAct"
|
||||||
JOIN "Item" on "ItemAct"."itemId" = "Item".id
|
JOIN "Item" on "ItemAct"."itemId" = "Item".id
|
||||||
|
@ -19,10 +18,13 @@ function earn ({ models }) {
|
||||||
OR ("ItemAct".act IN ('VOTE','POLL') AND "Item"."userId" = "ItemAct"."userId"))
|
OR ("ItemAct".act IN ('VOTE','POLL') AND "Item"."userId" = "ItemAct"."userId"))
|
||||||
AND "ItemAct".created_at > now_utc() - INTERVAL '1 day'`
|
AND "ItemAct".created_at > now_utc() - INTERVAL '1 day'`
|
||||||
|
|
||||||
|
// convert to msats
|
||||||
|
sum = sum * 1000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
How earnings work:
|
How earnings 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
|
||||||
1/3: top upvoters of top posts/comments, scored on:
|
1/3: top upvoters of top posts/comments, scored on:
|
||||||
- their trust
|
- their trust
|
||||||
- how much they tipped
|
- how much they tipped
|
||||||
|
@ -30,19 +32,27 @@ function earn ({ models }) {
|
||||||
- how the post/comment scored
|
- how the post/comment scored
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// get earners { id, earnings }
|
if (sum <= 0) {
|
||||||
|
console.log('done', name, 'no earning')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get earners { userId, id, type, rank, proportion }
|
||||||
const earners = await models.$queryRaw(`
|
const earners = await models.$queryRaw(`
|
||||||
WITH item_ratios AS (
|
WITH item_ratios AS (
|
||||||
SELECT *,
|
SELECT *,
|
||||||
"weightedVotes"/coalesce(NULLIF(sum("weightedVotes") OVER (PARTITION BY "parentId" IS NULL),0), ${TOP_ITEMS}) AS ratio
|
CASE WHEN "parentId" IS NULL THEN 'POST' ELSE 'COMMENT' END as type,
|
||||||
|
CASE WHEN "weightedVotes" > 0 THEN "weightedVotes"/(sum("weightedVotes") OVER (PARTITION BY "parentId" IS NULL)) ELSE 0 END AS ratio
|
||||||
FROM (
|
FROM (
|
||||||
SELECT *,
|
SELECT *,
|
||||||
ROW_NUMBER() OVER (PARTITION BY "parentId" IS NULL ORDER BY "weightedVotes" desc) AS r
|
NTILE(100) OVER (PARTITION BY "parentId" IS NULL ORDER BY "weightedVotes" desc) AS percentile,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY "parentId" IS NULL ORDER BY "weightedVotes" desc) AS rank
|
||||||
FROM
|
FROM
|
||||||
"Item"
|
"Item"
|
||||||
WHERE created_at >= now_utc() - interval '36 hours'
|
WHERE created_at >= now_utc() - interval '36 hours'
|
||||||
|
AND "weightedVotes" > 0
|
||||||
) x
|
) x
|
||||||
WHERE x.r <= ${TOP_ITEMS}
|
WHERE x.percentile <= ${TOP_PERCENTILE}
|
||||||
),
|
),
|
||||||
upvoters AS (
|
upvoters AS (
|
||||||
SELECT "ItemAct"."userId", item_ratios.id, item_ratios.ratio, item_ratios."parentId",
|
SELECT "ItemAct"."userId", item_ratios.id, item_ratios.ratio, item_ratios."parentId",
|
||||||
|
@ -54,36 +64,47 @@ function earn ({ models }) {
|
||||||
GROUP BY "ItemAct"."userId", item_ratios.id, item_ratios.ratio, item_ratios."parentId"
|
GROUP BY "ItemAct"."userId", item_ratios.id, item_ratios.ratio, item_ratios."parentId"
|
||||||
),
|
),
|
||||||
upvoter_ratios AS (
|
upvoter_ratios AS (
|
||||||
SELECT "userId", sum(early_multiplier*tipped_ratio*ratio*users.trust) as upvoting_score,
|
SELECT "userId", sum(early_multiplier*tipped_ratio*ratio*users.trust) as upvoter_ratio,
|
||||||
"parentId" IS NULL as "isPost"
|
"parentId" IS NULL as "isPost", CASE WHEN "parentId" IS NULL THEN 'TIP_POST' ELSE 'TIP_COMMENT' END as type
|
||||||
FROM (
|
FROM (
|
||||||
SELECT *,
|
SELECT *,
|
||||||
${EARLY_MULTIPLIER_MAX}/(ROW_NUMBER() OVER (partition by id order by acted_at asc)) AS early_multiplier,
|
1/(ROW_NUMBER() OVER (partition by id order by acted_at asc)) AS early_multiplier,
|
||||||
tipped::float/(sum(tipped) OVER (partition by id)) tipped_ratio
|
tipped::float/(sum(tipped) OVER (partition by id)) tipped_ratio
|
||||||
FROM upvoters
|
FROM upvoters
|
||||||
) u
|
) u
|
||||||
JOIN users on "userId" = users.id
|
JOIN users on "userId" = users.id
|
||||||
GROUP BY "userId", "parentId" IS NULL
|
GROUP BY "userId", "parentId" IS NULL
|
||||||
)
|
)
|
||||||
SELECT "userId" as id, FLOOR(sum(proportion)*${sum}*1000) as earnings
|
SELECT "userId", NULL as id, type, ROW_NUMBER() OVER (PARTITION BY "isPost" ORDER BY upvoter_ratio DESC) as rank,
|
||||||
FROM (
|
upvoter_ratio/(sum(upvoter_ratio) OVER (PARTITION BY "isPost"))/${UPVOTE_EACH_REWARD} as proportion
|
||||||
SELECT "userId",
|
|
||||||
upvoting_score/(sum(upvoting_score) OVER (PARTITION BY "isPost"))/${UPVOTE_EACH_REWARD} as proportion
|
|
||||||
FROM upvoter_ratios
|
FROM upvoter_ratios
|
||||||
|
WHERE upvoter_ratio > 0
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT "userId", ratio/${ITEM_EACH_REWARD} as proportion
|
SELECT "userId", id, type, rank, ratio/${ITEM_EACH_REWARD} as proportion
|
||||||
FROM item_ratios
|
FROM item_ratios`)
|
||||||
) a
|
|
||||||
GROUP BY "userId"
|
// in order to group earnings for users we use the same createdAt time for
|
||||||
HAVING FLOOR(sum(proportion)*${sum}) >= 1`)
|
// 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
|
||||||
|
|
||||||
// for each earner, serialize earnings
|
// for each earner, serialize earnings
|
||||||
// we do this for each earner because we don't need to serialize
|
// we do this for each earner because we don't need to serialize
|
||||||
// all earner updates together
|
// all earner updates together
|
||||||
earners.forEach(async earner => {
|
earners.forEach(async earner => {
|
||||||
if (earner.earnings > 0) {
|
const earnings = Math.floor(earner.proportion * sum)
|
||||||
|
total += earnings
|
||||||
|
if (total > sum) {
|
||||||
|
console.log('total exceeds sum', name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (earnings > 0) {
|
||||||
await serialize(models,
|
await serialize(models,
|
||||||
models.$executeRaw`SELECT earn(${earner.id}, ${earner.earnings})`)
|
models.$executeRaw`SELECT earn(${earner.userId}, ${earnings},
|
||||||
|
${now}, ${earner.type}, ${earner.id}, ${earner.rank})`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue