@@ -2,10 +2,19 @@ import { PrismaClient } from '@prisma/client';
22import { ChartConfiguration } from 'chart.js' ;
33import { Express } from 'express' ;
44import { CoalitionScore , getBlocAtDate , getCoalitionScore } from '../utils' ;
5+ import NodeCache from 'node-cache' ;
6+
7+ const chartDataCache = new NodeCache ( { stdTTL : 60 * 5 , checkperiod : 60 * 5 } ) ;
58
69export const setupChartRoutes = function ( app : Express , prisma : PrismaClient ) : void {
710 app . get ( '/charts/coalitions/scores/history' , async ( req , res ) => {
811 try {
12+ // Check cache first
13+ const cachedData = chartDataCache . get < ChartConfiguration > ( 'coalitionsScoresHistory' ) ;
14+ if ( cachedData ) {
15+ return res . json ( cachedData ) ;
16+ }
17+
918 const now = new Date ( ) ;
1019 const currentBloc = await getBlocAtDate ( prisma , now ) ;
1120 if ( ! currentBloc ) {
@@ -82,6 +91,9 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
8291 } ) ;
8392 }
8493
94+ // Cache and return the data
95+ chartDataCache . set ( 'coalitionsScoresHistory' , chartJSData ) ;
96+
8597 return res . json ( chartJSData ) ;
8698 }
8799 catch ( err ) {
@@ -92,12 +104,18 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
92104
93105 app . get ( '/charts/coalitions/:coalitionId/scores/history' , async ( req , res ) => {
94106 try {
107+ // Check cache first
108+ const coalitionId = parseInt ( req . params . coalitionId ) ;
109+ const cachedData = chartDataCache . get < ChartConfiguration > ( `coalitionsScoresHistory_${ coalitionId } ` ) ;
110+ if ( cachedData ) {
111+ return res . json ( cachedData ) ;
112+ }
113+
95114 const now = new Date ( ) ;
96115 const currentBloc = await getBlocAtDate ( prisma , now ) ;
97116 if ( ! currentBloc ) {
98117 throw new Error ( 'No season is currently ongoing' ) ;
99118 }
100- const coalitionId = parseInt ( req . params . coalitionId ) ;
101119 const coalition = await prisma . intraCoalition . findFirst ( {
102120 where : {
103121 id : coalitionId ,
@@ -200,6 +218,9 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
200218 }
201219 } ;
202220
221+ // Cache and return the data
222+ chartDataCache . set ( `coalitionsScoresHistory_${ coalitionId } ` , chartJSData ) ;
223+
203224 return res . json ( chartJSData ) ;
204225 }
205226 catch ( err ) {
@@ -232,6 +253,13 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
232253 if ( ! user || ! user . coalition_users || user . coalition_users . length === 0 ) {
233254 return res . status ( 404 ) . send ( 'User not found or not in a coalition' ) ;
234255 }
256+
257+ // Check cache first
258+ const cachedData = chartDataCache . get < ChartConfiguration > ( `coalitionsScoresHistory_${ user . id } ` ) ;
259+ if ( cachedData ) {
260+ return res . json ( cachedData ) ;
261+ }
262+
235263 const now = new Date ( ) ;
236264 const currentBloc = await getBlocAtDate ( prisma , now ) ;
237265 if ( ! currentBloc ) {
@@ -309,6 +337,9 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
309337 }
310338 } ;
311339
340+ // Cache and return the data
341+ chartDataCache . set ( `coalitionsScoresHistory_${ user . id } ` , chartJSData ) ;
342+
312343 return res . json ( chartJSData ) ;
313344 }
314345 catch ( err ) {
@@ -342,6 +373,12 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
342373 return res . status ( 404 ) . send ( 'User not found or not in a coalition' ) ;
343374 }
344375
376+ // Check cache first
377+ const cachedData = chartDataCache . get < ChartConfiguration > ( `coalitionsScoresHistorySplit_${ user . id } ` ) ;
378+ if ( cachedData ) {
379+ return res . json ( cachedData ) ;
380+ }
381+
345382 const now = new Date ( ) ;
346383 const currentBloc = await getBlocAtDate ( prisma , now ) ;
347384 if ( ! currentBloc ) {
@@ -436,6 +473,9 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
436473 }
437474 } ;
438475
476+ // Cache and return the data
477+ chartDataCache . set ( `coalitionsScoresHistorySplit_${ user . id } ` , chartJSData ) ;
478+
439479 return res . json ( chartJSData ) ;
440480 }
441481 catch ( err ) {
0 commit comments