make wot ranking live + wot ranked comments

This commit is contained in:
keyan 2022-02-01 16:11:45 -06:00
parent 15cf95fa01
commit dce189703c
2 changed files with 25 additions and 48 deletions

View File

@ -11,7 +11,7 @@ async function comments (models, id, sort) {
switch (sort) { switch (sort) {
case 'top': case 'top':
orderBy = 'ORDER BY x.sats DESC NULLS LAST' orderBy = 'ORDER BY x.sats DESC NULLS LAST'
join = LEFT_JOIN_SATS join = LEFT_JOIN_WEIGHTED_SATS
break break
case 'recent': case 'recent':
orderBy = 'ORDER BY "Item".created_at DESC' orderBy = 'ORDER BY "Item".created_at DESC'
@ -19,7 +19,7 @@ async function comments (models, id, sort) {
break break
default: default:
orderBy = ORDER_BY_SATS orderBy = ORDER_BY_SATS
join = LEFT_JOIN_SATS join = LEFT_JOIN_WEIGHTED_SATS
break break
} }
@ -107,26 +107,26 @@ export default {
if (decodedCursor.offset === 0) { if (decodedCursor.offset === 0) {
items = await models.$queryRaw(` items = await models.$queryRaw(`
${SELECT} ${SELECT}
FROM "Item" FROM "Item"
${timedLeftJoinSats(1)} ${timedLeftJoinWeightedSats(1)}
WHERE "parentId" IS NULL AND created_at <= $1 AND created_at > $3 WHERE "parentId" IS NULL AND created_at <= $1 AND created_at > $3
AND "pinId" IS NULL AND "pinId" IS NULL
${timedOrderBySats(1)} ${timedOrderBySats(1)}
OFFSET $2 OFFSET $2
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date() - 7)) LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date() - 7))
} }
if (decodedCursor.offset !== 0 || items?.length < LIMIT) { if (decodedCursor.offset !== 0 || items?.length < LIMIT) {
items = await models.$queryRaw(` items = await models.$queryRaw(`
${SELECT} ${SELECT}
FROM "Item" FROM "Item"
${timedLeftJoinSats(1)} ${timedLeftJoinWeightedSats(1)}
WHERE "parentId" IS NULL AND created_at <= $1 WHERE "parentId" IS NULL AND created_at <= $1
AND "pinId" IS NULL AND "pinId" IS NULL
${timedOrderBySats(1)} ${timedOrderBySats(1)}
OFFSET $2 OFFSET $2
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset) LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset)
} }
if (decodedCursor.offset === 0) { if (decodedCursor.offset === 0) {
@ -143,17 +143,6 @@ export default {
) rank_filter WHERE RANK = 1`) ) rank_filter WHERE RANK = 1`)
} }
break break
case 'wot':
items = await models.$queryRaw(`
${SELECT}
FROM "Item"
${timedLeftJoinWeightedSats(1)}
WHERE "parentId" IS NULL AND created_at <= $1
AND "pinId" IS NULL
${timedOrderBySats(1)}
OFFSET $2
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset)
break
case 'top': case 'top':
items = await models.$queryRaw(` items = await models.$queryRaw(`
${SELECT} ${SELECT}
@ -730,12 +719,18 @@ function timedLeftJoinWeightedSats (num) {
) x ON "Item".id = x.id` ) x ON "Item".id = x.id`
} }
const LEFT_JOIN_SATS = const LEFT_JOIN_WEIGHTED_SATS =
`LEFT JOIN (${LEFT_JOIN_SATS_SELECT} `LEFT JOIN (${LEFT_JOIN_SATS_SELECT}
FROM "Item" i FROM "Item" i
JOIN "ItemAct" ON i.id = "ItemAct"."itemId" JOIN "ItemAct" ON i.id = "ItemAct"."itemId"
GROUP BY i.id) x ON "Item".id = x.id` GROUP BY i.id) x ON "Item".id = x.id`
// const LEFT_JOIN_SATS =
// `LEFT JOIN (${LEFT_JOIN_SATS_SELECT}
// FROM "Item" i
// JOIN "ItemAct" ON i.id = "ItemAct"."itemId"
// GROUP BY i.id) x ON "Item".id = x.id`
/* NOTE: because many items will have the same rank, we need to tie break with a unique field so pagination works */ /* NOTE: because many items will have the same rank, we need to tie break with a unique field so pagination works */
function timedOrderBySats (num) { function timedOrderBySats (num) {
return `ORDER BY (GREATEST(x.sats-1, 0)/POWER(EXTRACT(EPOCH FROM ($${num} - "Item".created_at))/3600+2, 1.5) + return `ORDER BY (GREATEST(x.sats-1, 0)/POWER(EXTRACT(EPOCH FROM ($${num} - "Item".created_at))/3600+2, 1.5) +

View File

@ -1,18 +0,0 @@
import Layout from '../components/layout'
import Items from '../components/items'
import { getGetServerSideProps } from '../api/ssrApollo'
import { MORE_ITEMS } from '../fragments/items'
const variables = { sort: 'wot' }
export const getServerSideProps = getGetServerSideProps(MORE_ITEMS, variables)
export default function Index ({ data: { moreItems: { items, pins, cursor } } }) {
return (
<Layout>
<Items
items={items} pins={pins} cursor={cursor}
variables={variables} rank
/>
</Layout>
)
}