for top users constrain other stats

This commit is contained in:
keyan 2022-10-26 09:56:22 -05:00
parent ec133e8ea2
commit 6c9f4f1c3a
5 changed files with 74 additions and 65 deletions

View File

@ -3,11 +3,11 @@ import { decodeCursor, LIMIT, nextCursorEncoded } from '../../lib/cursor'
import { createMentions, getItem, SELECT, updateItem, filterClause } from './item' import { createMentions, getItem, SELECT, updateItem, filterClause } from './item'
import serialize from './serial' import serialize from './serial'
export function topClause (within) { export function within (table, within) {
let interval = ' AND "ItemAct".created_at >= $1 - INTERVAL ' let interval = ' AND "' + table + '".created_at >= $1 - INTERVAL '
switch (within) { switch (within) {
case 'forever': case 'day':
interval = '' interval += "'1 day'"
break break
case 'week': case 'week':
interval += "'7 days'" interval += "'7 days'"
@ -19,54 +19,25 @@ export function topClause (within) {
interval += "'1 year'" interval += "'1 year'"
break break
default: default:
interval += "'1 day'" interval = ''
break break
} }
return interval return interval
} }
export function earnWithin (within) { export function withinDate (within) {
let interval = ' AND "Earn".created_at >= $1 - INTERVAL '
switch (within) { switch (within) {
case 'forever': case 'day':
interval = '' return new Date(new Date().setDate(new Date().getDate() - 1))
break
case 'week': case 'week':
interval += "'7 days'" return new Date(new Date().setDate(new Date().getDate() - 7))
break
case 'month': case 'month':
interval += "'1 month'" return new Date(new Date().setDate(new Date().getDate() - 30))
break
case 'year': case 'year':
interval += "'1 year'" return new Date(new Date().setDate(new Date().getDate() - 365))
break
default: default:
interval += "'1 day'" return new Date(0)
break
} }
return interval
}
export function itemWithin (within) {
let interval = ' AND "Item".created_at >= $1 - INTERVAL '
switch (within) {
case 'forever':
interval = ''
break
case 'week':
interval += "'7 days'"
break
case 'month':
interval += "'1 month'"
break
case 'year':
interval += "'1 year'"
break
default:
interval += "'1 day'"
break
}
return interval
} }
async function authMethods (user, args, { models, me }) { async function authMethods (user, args, { models, me }) {
@ -125,7 +96,7 @@ export default {
FROM "ItemAct" FROM "ItemAct"
JOIN users on "ItemAct"."userId" = users.id JOIN users on "ItemAct"."userId" = users.id
WHERE "ItemAct".created_at <= $1 WHERE "ItemAct".created_at <= $1
${topClause(when)} ${within('ItemAct', when)}
GROUP BY users.id, users.name GROUP BY users.id, users.name
ORDER BY spent DESC NULLS LAST, users.created_at DESC ORDER BY spent DESC NULLS LAST, users.created_at DESC
OFFSET $2 OFFSET $2
@ -136,7 +107,7 @@ export default {
FROM users FROM users
JOIN "Item" on "Item"."userId" = users.id JOIN "Item" on "Item"."userId" = users.id
WHERE "Item".created_at <= $1 AND "Item"."parentId" IS NULL WHERE "Item".created_at <= $1 AND "Item"."parentId" IS NULL
${itemWithin(when)} ${within('Item', when)}
GROUP BY users.id GROUP BY users.id
ORDER BY nitems DESC NULLS LAST, users.created_at DESC ORDER BY nitems DESC NULLS LAST, users.created_at DESC
OFFSET $2 OFFSET $2
@ -147,7 +118,7 @@ export default {
FROM users FROM users
JOIN "Item" on "Item"."userId" = users.id JOIN "Item" on "Item"."userId" = users.id
WHERE "Item".created_at <= $1 AND "Item"."parentId" IS NOT NULL WHERE "Item".created_at <= $1 AND "Item"."parentId" IS NOT NULL
${itemWithin(when)} ${within('Item', when)}
GROUP BY users.id GROUP BY users.id
ORDER BY ncomments DESC NULLS LAST, users.created_at DESC ORDER BY ncomments DESC NULLS LAST, users.created_at DESC
OFFSET $2 OFFSET $2
@ -161,12 +132,12 @@ export default {
JOIN "Item" on "ItemAct"."itemId" = "Item".id JOIN "Item" on "ItemAct"."itemId" = "Item".id
JOIN users on "Item"."userId" = users.id JOIN users on "Item"."userId" = users.id
WHERE act <> 'BOOST' AND "ItemAct"."userId" <> users.id AND "ItemAct".created_at <= $1 WHERE act <> 'BOOST' AND "ItemAct"."userId" <> users.id AND "ItemAct".created_at <= $1
${topClause(when)}) ${within('ItemAct', when)})
UNION ALL UNION ALL
(SELECT users.*, "Earn".msats/1000 as amount (SELECT users.*, "Earn".msats/1000 as amount
FROM "Earn" FROM "Earn"
JOIN users on users.id = "Earn"."userId" JOIN users on users.id = "Earn"."userId"
WHERE "Earn".msats > 0 ${earnWithin(when)})) u WHERE "Earn".msats > 0 ${within('Earn', when)})) u
GROUP BY u.id, u.name, u.created_at, u."photoId" GROUP BY u.id, u.name, u.created_at, u."photoId"
ORDER BY stacked DESC NULLS LAST, created_at DESC ORDER BY stacked DESC NULLS LAST, created_at DESC
OFFSET $2 OFFSET $2
@ -303,26 +274,61 @@ export default {
User: { User: {
authMethods, authMethods,
nitems: async (user, args, { models }) => { nitems: async (user, { when }, { models }) => {
if (user.nitems) { if (user.nitems) {
return user.nitems return user.nitems
} }
return await models.item.count({ where: { userId: user.id, parentId: null } }) return await models.item.count({
where: {
userId: user.id,
parentId: null,
createdAt: {
gte: withinDate(when)
}
}
})
}, },
ncomments: async (user, args, { models }) => { ncomments: async (user, { when }, { models }) => {
if (user.ncomments) { if (user.ncomments) {
return user.ncomments return user.ncomments
} }
return await models.item.count({ where: { userId: user.id, parentId: { not: null } } })
return await models.item.count({
where: {
userId: user.id,
parentId: { not: null },
createdAt: {
gte: withinDate(when)
}
}
})
}, },
stacked: async (user, args, { models }) => { stacked: async (user, { when }, { models }) => {
if (user.stacked) { if (user.stacked) {
return user.stacked return user.stacked
} }
return Math.floor((user.stackedMsats || 0) / 1000) if (!when) {
// forever
return Math.floor((user.stackedMsats || 0) / 1000)
} else {
const [{ stacked }] = await models.$queryRaw(`
SELECT sum(amount) as stacked
FROM
((SELECT sum("ItemAct".sats) as amount
FROM "ItemAct"
JOIN "Item" on "ItemAct"."itemId" = "Item".id
WHERE act <> 'BOOST' AND "ItemAct"."userId" <> $2 AND "Item"."userId" = $2
AND "ItemAct".created_at >= $1)
UNION ALL
(SELECT sum("Earn".msats/1000) as amount
FROM "Earn"
WHERE "Earn".msats > 0 AND "Earn"."userId" = $2
AND "Earn".created_at >= $1)) u`, withinDate(when), Number(user.id))
return stacked || 0
}
}, },
spent: async (user, args, { models }) => { spent: async (user, { when }, { models }) => {
if (user.spent) { if (user.spent) {
return user.spent return user.spent
} }
@ -332,7 +338,10 @@ export default {
sats: true sats: true
}, },
where: { where: {
userId: user.id userId: user.id,
createdAt: {
gte: withinDate(when)
}
} }
}) })

View File

@ -40,10 +40,10 @@ export default gql`
id: ID! id: ID!
createdAt: String! createdAt: String!
name: String name: String
nitems: Int! nitems(when: String): Int!
ncomments: Int! ncomments(when: String): Int!
stacked: Int! stacked(when: String): Int!
spent: Int! spent(when: String): Int!
freePosts: Int! freePosts: Int!
freeComments: Int! freeComments: Int!
hasNewNotes: Boolean! hasNewNotes: Boolean!

View File

@ -48,7 +48,7 @@ export const MORE_FLAT_COMMENTS = gql`
export const TOP_COMMENTS = gql` export const TOP_COMMENTS = gql`
${COMMENT_FIELDS} ${COMMENT_FIELDS}
query topComments($sort: String, $cursor: String, $when: String) { query topComments($sort: String, $cursor: String, $when: String = "day") {
topComments(sort: $sort, cursor: $cursor, when: $when) { topComments(sort: $sort, cursor: $cursor, when: $when) {
cursor cursor
comments { comments {

View File

@ -73,7 +73,7 @@ export const ITEMS = gql`
export const TOP_ITEMS = gql` export const TOP_ITEMS = gql`
${ITEM_FIELDS} ${ITEM_FIELDS}
query topItems($sort: String, $cursor: String, $when: String) { query topItems($sort: String, $cursor: String, $when: String = "day") {
topItems(sort: $sort, cursor: $cursor, when: $when) { topItems(sort: $sort, cursor: $cursor, when: $when) {
cursor cursor
items { items {

View File

@ -152,15 +152,15 @@ export const USER_FIELDS = gql`
}` }`
export const TOP_USERS = gql` export const TOP_USERS = gql`
query TopUsers($cursor: String, $when: String, $sort: String) { query TopUsers($cursor: String, $when: String = "day", $sort: String) {
topUsers(cursor: $cursor, when: $when, sort: $sort) { topUsers(cursor: $cursor, when: $when, sort: $sort) {
users { users {
name name
photoId photoId
stacked stacked(when: $when)
spent spent(when: $when)
ncomments ncomments(when: $when)
nitems nitems(when: $when)
} }
cursor cursor
} }