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

View File

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

View File

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