From d31c6c6be3a52bc52d06ee260ac30144eb85e332 Mon Sep 17 00:00:00 2001 From: Jonas Simoen Date: Thu, 6 Jun 2024 00:17:05 +0200 Subject: [PATCH] perf update --- api/index.ts | 6 +- src/controllers/Team.ts | 220 +++++++++++++++++++++------------------- src/controllers/Week.ts | 6 +- 3 files changed, 123 insertions(+), 109 deletions(-) diff --git a/api/index.ts b/api/index.ts index 33f9af8..915ff6e 100644 --- a/api/index.ts +++ b/api/index.ts @@ -53,9 +53,9 @@ server.register(_default); server.register(PublicRouter, { prefix: "/api" }); server.register(AdminRouter, { prefix: "/api" }); -server.get('/metrics/prisma', async (_req: any, res: any) => { - res.end(await prisma.$metrics.prometheus()) -}) +// server.get('/metrics/prisma', async (_req: any, res: any) => { +// res.end(await prisma.$metrics.prometheus()) +// }) server.setErrorHandler((err, req, rep) => { if (err instanceof HttpError) { diff --git a/src/controllers/Team.ts b/src/controllers/Team.ts index b487435..0ea2636 100644 --- a/src/controllers/Team.ts +++ b/src/controllers/Team.ts @@ -166,80 +166,71 @@ export const DeleteDropTeamHandler = async (req: any, rep: any) => { export const GetTeamHandler = async (req: any, rep: any) => { const weekId = await upcomingWeekId(); - const [playersWithMultipleSelections, team, transfers] = await Promise.all([ - prisma.player.findMany({ - where: { - selections: { - some: { - teamId: +req.params.id, - weekId, - } + const teamWithSelections = await prisma.team.findUnique({ + cacheStrategy: { + ttl: 30, + swr: 60, + }, + where: { + id: +req.params.id, + }, + select: { + budget: true, + created: true, + name: true, + points: true, + rank: true, + userId: true, + valid: true, + value: true, + weekId: true, + user: true, + selections: { + select: { + player: true, + captain: true, + starting: true, + value: true, + playerId: true, + weekId: true, + booster: true, + order: true, + }, + where: { + weekId, } }, - include: { - selections: { - where: { - teamId: +req.params.id, - weekId, - }, - select: { - captain: true, - starting: true, - value: true, - playerId: true, - weekId: true, - booster: true, - order: true, - }, + Transfer: { + where: { + weekId, } - }, - }), - - prisma.team.findFirst({ - cacheStrategy: { - ttl: 30, - swr: 60, - }, - where: { - id: +req.params.id - }, - include: { - user: true } - }), + } + }); - prisma.transfer.findMany({ - where: { - teamId: +req.params.id, - weekId, - } - }), - ]) - - rep.send({ - team: { ...team }, - players: playersWithMultipleSelections - .sort((a, b) => a.selections[0].order! - b.selections[0].order!) - .map(({ selections, ...rest }) => { - const {order, ...sel} = selections[0]; - return { - ...rest, - selection: sel - } - }), - transfers: transfers + const {selections, Transfer, user, ...team} = teamWithSelections!; + const invertedSelections = selections?.map((sel: any) => { + const {player, ...selection} = sel; + return { + ...player, + selection, + }; + }); + rep.send({ + team, + players: invertedSelections, + transfers: Transfer, + user: user, }); } export const GetPointsTeamHandler = async (req: any, rep: any) => { const [ - team, - players, + teamWithSelections, deadlineWeek, - transfers, weeklyData, globalData - ]: [any, any, any, any, any, any] = await Promise.all([ + ]: [any, any, any, any] = await Promise.all([ prisma.team.findUnique({ cacheStrategy: { ttl: 30, @@ -247,15 +238,31 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => { }, where: { id: +req.params.id, - } - }).catch((err: any) => rep.status(404)), - - prisma.player.findMany({ - cacheStrategy: { - ttl: 30, - swr: 60, }, - include: { + select: { + id: true, + badge: true, + budget: true, + created: true, + name: true, + points: true, + rank: true, + userId: true, + valid: true, + value: true, + weekId: true, + freeHit: true, + tripleCaptain: true, + fanFavourite: true, + superSubs: true, + hiddenGem: true, + goalRush: true, + user: true, + Transfer: { + where: { + weekId: +req.params.weekId, + } + }, selections: { select: { captain: true, @@ -267,29 +274,41 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => { played: true, endWinnerSelections: true, points: true, - order: true, - }, - where: { - teamId: +req.params.id, - weekId: +req.params.weekId, + player: { + select: { + id: true, + banned: true, + captain: true, + clubId: true, + externalId: true, + forename: true, + form: true, + injury: true, + name: true, + portraitUrl: true, + positionId: true, + setPieces: true, + short: true, + points: true, + star: true, + surname: true, + value: true, + pSelections: true, + stats: { + where: { + match: { + weekId: +req.params.weekId, + } + } + } + } + }, }, - }, - stats: { where: { - match: { - weekId: +req.params.weekId, - } - } - } - }, - where: { - selections: { - some: { - teamId: +req.params.id, weekId: +req.params.weekId, } } - }, + } }), prisma.week.findFirst({ @@ -301,44 +320,39 @@ export const GetPointsTeamHandler = async (req: any, rep: any) => { id: +req.params.weekId } }), - - prisma.transfer.findMany({ - cacheStrategy: { - ttl: 30, - swr: 60, - }, - where: { - teamId: +req.params.id, - weekId: +req.params.weekId, - } - }), - prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points) DESC) AS int) FROM "Selection" s WHERE "weekId" = ${+req.params.weekId} AND starting = 1 GROUP BY "teamId" ORDER BY rank ASC`, prisma.$queryRaw`SELECT "teamId", CAST(SUM(points) AS int) AS points, CAST(RANK() OVER(ORDER BY SUM(points) DESC) AS int) FROM "Selection" s WHERE starting = 1 GROUP BY "teamId" ORDER BY rank ASC`, ]); + + const {selections, Transfer, user, ...team} = teamWithSelections!; + const invertedSelections = selections?.map((sel: any) => { + const {player, ...selection} = sel; + return { + ...player, + selections: [selection], + }; + }); rep.send({ - players: players.sort((a: any, b: any) => a.selections[0].order! - b.selections[0].order!), + players: invertedSelections, team: { ...team, rank: globalData.find((teamData: any) => teamData.teamId === +req.params.id)?.rank || 0, points: globalData.find((teamData: any) => teamData.teamId === +req.params.id)?.points || 0, }, user: req.user, - transfers: transfers, + transfers: Transfer, weeks: { deadlineDate: deadlineWeek?.deadlineDate, deadlineWeek: deadlineWeek?.id, displayWeek: max([(deadlineWeek?.id || 0) - 1, 0]), }, - // todo: replace weekstat weekStat: [{ rank: weeklyData.find((teamData: any) => teamData.teamId === +req.params.id)?.rank || 0, points: weeklyData.find((teamData: any) => teamData.teamId === +req.params.id)?.points || 0, winner: weeklyData[0]?.points || 0, average: weeklyData.reduce((acc: number, curr: any) => curr.points + acc,0) / weeklyData.length, teamId: +req.params.id, - value: 69, weekId: +req.params.weekId }], }); diff --git a/src/controllers/Week.ts b/src/controllers/Week.ts index 9338de1..7e79da9 100644 --- a/src/controllers/Week.ts +++ b/src/controllers/Week.ts @@ -203,7 +203,7 @@ export const DeleteWeekHandler = async (req: any, rep: any) => { } export const GetDeadlineInfoHandler = async (req: any, rep: any) => { const [weeks, deadlineWeek, displayWeek] = await Promise.all([ - await prisma.week.findMany({ + prisma.week.findMany({ cacheStrategy: { ttl: 30, swr: 60, @@ -212,7 +212,7 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => { id: 'asc' } }), - await prisma.week.findFirst({ + prisma.week.findFirst({ cacheStrategy: { ttl: 30, swr: 60, @@ -226,7 +226,7 @@ export const GetDeadlineInfoHandler = async (req: any, rep: any) => { deadlineDate: 'asc', } }), - await prisma.week.findFirst({ + prisma.week.findFirst({ cacheStrategy: { ttl: 30, swr: 60,