Skip to content

Commit cebe9b2

Browse files
committed
fix: add caching for charts
1 parent 66cc2f1 commit cebe9b2

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/routes/admin/charts.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ import { PrismaClient } from '@prisma/client';
33
import { CURSUS_ID } from '../../env';
44
import { ChartConfiguration } from 'chart.js';
55
import { getBlocAtDate } from '../../utils';
6+
import NodeCache from 'node-cache';
7+
8+
const adminChartDataCache = new NodeCache({ stdTTL: 60 * 5, checkperiod: 60 * 5 });
69

710
export const setupAdminChartsRoutes = function(app: Express, prisma: PrismaClient): void {
811
app.get('/admin/charts/coalitions/users/distribution', async (req, res) => {
12+
// Check cache first
13+
const cachedData = adminChartDataCache.get<ChartConfiguration>('coalitionsUsersDistribution');
14+
if (cachedData) {
15+
return res.json(cachedData);
16+
}
17+
918
// Get the distribution of users per coalition
1019
const distribution = await prisma.intraCoalitionUser.groupBy({
1120
by: ['coalition_id'],
@@ -77,12 +86,23 @@ export const setupAdminChartsRoutes = function(app: Express, prisma: PrismaClien
7786
},
7887
},
7988
}
89+
90+
// Cache and return the data
91+
adminChartDataCache.set('coalitionsUsersDistribution', chartJSData);
92+
8093
return res.json(chartJSData);
8194
});
8295

8396
app.get('/admin/charts/coalitions/:coalitionId/scores/distribution', async (req, res) => {
8497
try {
8598
const coalitionId = parseInt(req.params.coalitionId);
99+
100+
// Check cache first
101+
const cachedData = adminChartDataCache.get<ChartConfiguration>(`coalitionScoresDistribution_${coalitionId}`);
102+
if (cachedData) {
103+
return res.json(cachedData);
104+
}
105+
86106
const coalition = await prisma.intraCoalition.findFirst({
87107
where: {
88108
id: coalitionId,
@@ -100,6 +120,7 @@ export const setupAdminChartsRoutes = function(app: Express, prisma: PrismaClien
100120
if (!currentBloc) {
101121
throw new Error('No current bloc found');
102122
}
123+
103124
const scores = await prisma.codamCoalitionScore.groupBy({
104125
by: ['user_id'],
105126
where: {
@@ -182,6 +203,9 @@ export const setupAdminChartsRoutes = function(app: Express, prisma: PrismaClien
182203
}
183204
};
184205

206+
// Cache and return the data
207+
adminChartDataCache.set(`coalitionScoresDistribution_${coalitionId}`, chartJSData);
208+
185209
return res.json(chartJSData);
186210
}
187211
catch (err) {

src/routes/charts.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ import { PrismaClient } from '@prisma/client';
22
import { ChartConfiguration } from 'chart.js';
33
import { Express } from 'express';
44
import { CoalitionScore, getBlocAtDate, getCoalitionScore } from '../utils';
5+
import NodeCache from 'node-cache';
6+
7+
const chartDataCache = new NodeCache({ stdTTL: 60 * 5, checkperiod: 60 * 5 });
58

69
export 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

Comments
 (0)