remove unecessary queries on resp critical path

This commit is contained in:
keyan 2022-05-01 20:01:33 -05:00
parent dc44764008
commit 798b055fb9
3 changed files with 25 additions and 24 deletions

View File

@ -11,6 +11,8 @@ export default {
throw new AuthenticationError('you must be logged in')
}
const meFull = await models.user.findUnique({ where: { id: me.id } })
/*
So that we can cursor over results, we union notifications together ...
this requires we have the same number of columns in all results
@ -72,7 +74,7 @@ export default {
`SELECT DISTINCT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL::BIGINT as "earnedSats",
'Reply' AS type
FROM "Item"
JOIN "Item" p ON ${me.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
JOIN "Item" p ON ${meFull.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
WHERE p."userId" = $1
AND "Item"."userId" <> $1 AND "Item".created_at <= $2`
)
@ -81,7 +83,7 @@ export default {
`(SELECT DISTINCT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL::BIGINT as "earnedSats",
'Reply' AS type
FROM "Item"
JOIN "Item" p ON ${me.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
JOIN "Item" p ON ${meFull.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
WHERE p."userId" = $1
AND "Item"."userId" <> $1 AND "Item".created_at <= $2
ORDER BY "sortTime" DESC
@ -100,7 +102,7 @@ export default {
LIMIT ${LIMIT}+$3)`
)
if (me.noteItemSats) {
if (meFull.noteItemSats) {
queries.push(
`(SELECT "Item".id::TEXT, MAX("ItemAct".created_at) AS "sortTime",
sum("ItemAct".sats) as "earnedSats", 'Votification' AS type
@ -116,7 +118,7 @@ export default {
)
}
if (me.noteMentions) {
if (meFull.noteMentions) {
queries.push(
`(SELECT "Item".id::TEXT, "Mention".created_at AS "sortTime", NULL as "earnedSats",
'Mention' AS type
@ -132,7 +134,7 @@ export default {
)
}
if (me.noteDeposits) {
if (meFull.noteDeposits) {
queries.push(
`(SELECT "Invoice".id::text, "Invoice"."confirmedAt" AS "sortTime", FLOOR("msatsReceived" / 1000) as "earnedSats",
'InvoicePaid' AS type
@ -145,7 +147,7 @@ export default {
)
}
if (me.noteInvites) {
if (meFull.noteInvites) {
queries.push(
`(SELECT "Invite".id, MAX(users.created_at) AS "sortTime", NULL as "earnedSats",
'Invitification' AS type
@ -165,16 +167,15 @@ export default {
OFFSET $3
LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset)
const { checkedNotesAt } = await models.user.findUnique({ where: { id: me.id } })
let earn
if (decodedCursor.offset === 0) {
if (me.noteEarning) {
if (meFull.noteEarning) {
const earnings = await models.$queryRaw(
`SELECT MAX("Earn".id)::text, MAX("Earn".created_at) AS "sortTime", FLOOR(SUM(msats) / 1000) as "earnedSats",
'Earn' AS type
FROM "Earn"
WHERE "Earn"."userId" = $1
AND created_at >= $2`, me.id, checkedNotesAt)
AND created_at >= $2`, me.id, meFull.checkedNotesAt)
if (earnings.length > 0 && earnings[0].earnedSats > 0) {
earn = earnings[0]
}
@ -184,7 +185,7 @@ export default {
}
return {
lastChecked: checkedNotesAt,
lastChecked: meFull.checkedNotesAt,
earn,
cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null,
notifications

View File

@ -177,7 +177,7 @@ export default {
const lastChecked = user.checkedNotesAt || new Date(0)
// check if any votes have been cast for them since checkedNotesAt
if (me.noteItemSats) {
if (user.noteItemSats) {
const votes = await models.$queryRaw(`
SELECT "ItemAct".id, "ItemAct".created_at
FROM "Item"
@ -185,7 +185,7 @@ export default {
WHERE "ItemAct"."userId" <> $1
AND "ItemAct".created_at > $2
AND "Item"."userId" = $1
LIMIT 1`, user.id, lastChecked)
LIMIT 1`, me.id, lastChecked)
if (votes.length > 0) {
return true
}
@ -195,16 +195,16 @@ export default {
const newReplies = await models.$queryRaw(`
SELECT "Item".id, "Item".created_at
FROM "Item"
JOIN "Item" p ON ${me.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
JOIN "Item" p ON ${user.noteAllDescendants ? '"Item".path <@ p.path' : '"Item"."parentId" = p.id'}
WHERE p."userId" = $1
AND "Item".created_at > $2 AND "Item"."userId" <> $1
LIMIT 1`, user.id, lastChecked)
LIMIT 1`, me.id, lastChecked)
if (newReplies.length > 0) {
return true
}
// check if they have any mentions since checkedNotesAt
if (me.noteMentions) {
if (user.noteMentions) {
const newMentions = await models.$queryRaw(`
SELECT "Item".id, "Item".created_at
FROM "Mention"
@ -212,7 +212,7 @@ export default {
WHERE "Mention"."userId" = $1
AND "Mention".created_at > $2
AND "Item"."userId" <> $1
LIMIT 1`, user.id, lastChecked)
LIMIT 1`, me.id, lastChecked)
if (newMentions.length > 0) {
return true
}
@ -226,7 +226,7 @@ export default {
maxBid: {
not: null
},
userId: user.id,
userId: me.id,
statusUpdatedAt: {
gt: lastChecked
}
@ -236,10 +236,10 @@ export default {
return true
}
if (me.noteEarning) {
if (user.noteEarning) {
const earn = await models.earn.findFirst({
where: {
userId: user.id,
userId: me.id,
createdAt: {
gt: lastChecked
},
@ -253,10 +253,10 @@ export default {
}
}
if (me.noteDeposits) {
if (user.noteDeposits) {
const invoice = await models.invoice.findFirst({
where: {
userId: user.id,
userId: me.id,
confirmedAt: {
gt: lastChecked
}
@ -268,13 +268,13 @@ export default {
}
// check if new invites have been redeemed
if (me.noteInvites) {
if (user.noteInvites) {
const newInvitees = await models.$queryRaw(`
SELECT "Invite".id
FROM users JOIN "Invite" on users."inviteId" = "Invite".id
WHERE "Invite"."userId" = $1
AND users.created_at > $2
LIMIT 1`, user.id, lastChecked)
LIMIT 1`, me.id, lastChecked)
if (newInvitees.length > 0) {
return true
}

View File

@ -23,7 +23,7 @@ export default async function getSSRApolloClient (req, me = null) {
context: {
models,
me: session
? await models.user.findUnique({ where: { id: session.user?.id } })
? session.user
: me,
lnd,
search