From 54cd169c93e224994251a15d9c2949ddfd16bc80 Mon Sep 17 00:00:00 2001 From: JonasBa Date: Thu, 10 Nov 2022 13:23:29 -0500 Subject: [PATCH 1/2] db(potato): faster getPotatoesGiven --- slack-service/app.js | 47 ++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/slack-service/app.js b/slack-service/app.js index 0d9117dc..e9691d6b 100644 --- a/slack-service/app.js +++ b/slack-service/app.js @@ -43,6 +43,8 @@ const newUTCDate = () => { return new Date(nowUtc) } +const currentUTCDateDay = () => { + /// Find the user in the DB that has a matching slack ID, if there is none return null async function getUserBySlackId(slackId) { return await prisma.users.findFirst({ @@ -82,27 +84,31 @@ async function addMessage(senderDbId, receiverDbId, potatoCount) { /// Gets the total potatoes given by the sender async function getTotalPotatoesGiven(senderId) { // Need to check if it not the same - const entry = await prisma.messages.findMany({ + const entries = await prisma.messages.findMany({ where: { sender_user_id: senderId, }, }); - - return entry + + return entries .map((t) => t.amount) .reduce((a, b) => a + b, 0); } /// Gets the total potatoes received by the sender -async function getTotalPotatoesReceived(senderId) { +async function getTotalPotatoesEverReceived(senderId) { // Need to check if it not the same - const entry = await prisma.messages.findMany({ + const entries = await prisma.messages.findMany({ where: { receiver_user_id: senderId, }, }); - return entry + if(!entries.length){ + return 0 + } + + return entries .map((t) => t.amount) .reduce((a, b) => a + b, 0); } @@ -110,22 +116,29 @@ async function getTotalPotatoesReceived(senderId) { /// Gets the potatoes given today by the sender async function getPotatoesGivenToday(senderId) { // Need to check if it not the same - const entry = await prisma.messages.findMany({ + const entries = await prisma.messages.findMany({ + select: { + amount: true, + }, + // You can only ever send 5 potato per day, so it takes at most 5 messages + // before you run out of potatoes. We can just take last 5 messages from today and discard the rest + take: maxPotato, + orderBy: [{ + created: 'desc' + }], where: { sender_user_id: senderId, + created: { + gte: newUTCDate().setHours(0, 0, 0, 0), + } }, }); - const datesAreOnSameDay = (first, second) => - first.getUTCFullYear() === second.getUTCFullYear() && - first.getUTCMonth() === second.getUTCMonth() && - first.getUTCDate() === second.getUTCDate(); + if(!entries.length) { + return 0; + } - const cur = newUTCDate(); - return entry - .filter((t) => datesAreOnSameDay(t.created, cur)) - .map((t) => t.amount) - .reduce((a, b) => a + b, 0); + return entries.map((t) => t.amount).reduce((a, b) => a + b, 0); } /// Get the full name using the userId from the Slack API @@ -311,7 +324,7 @@ app.event("app_home_opened", async ({ event, client, context }) => { const homePromises = [ getPotatoesGivenToday(userDbId), getTotalPotatoesGiven(userDbId), - getTotalPotatoesReceived(userDbId) + getTotalPotatoesEverReceived(userDbId) ] await Promise.all(homePromises).then(async ([potatoesGivenToday, totalPotatoesGiven, totalPotatoesReceived]) => { From 6d0417454f15810c56987730947d570a716cf019 Mon Sep 17 00:00:00 2001 From: JonasBa Date: Thu, 10 Nov 2022 20:51:26 -0500 Subject: [PATCH 2/2] fix: remove artifact --- slack-service/app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/slack-service/app.js b/slack-service/app.js index e9691d6b..776d11c0 100644 --- a/slack-service/app.js +++ b/slack-service/app.js @@ -43,8 +43,6 @@ const newUTCDate = () => { return new Date(nowUtc) } -const currentUTCDateDay = () => { - /// Find the user in the DB that has a matching slack ID, if there is none return null async function getUserBySlackId(slackId) { return await prisma.users.findFirst({