Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/handlers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export interface CommonCoreData extends UserListData {
alumni: { [login: string]: boolean };
};

export const getCommonCoreCohortData = async function(prisma: PrismaClient, year: number, noCache: boolean = false): Promise<CommonCoreData> {
export const getCommonCoreCohortData = async function(prisma: PrismaClient, year: number | null, noCache: boolean = false): Promise<CommonCoreData> {
// Check if the data is already in the cache
const cacheKey = `core-${year}`;
// A null year means "all cohorts" combined
const cacheKey = year === null ? 'core-all' : `core-${year}`;
const cachedData = coreCache.get(cacheKey);
if (!noCache && cachedData) {
return cachedData as CommonCoreData;
Expand All @@ -45,10 +46,14 @@ export const getCommonCoreCohortData = async function(prisma: PrismaClient, year
cursus_users: {
some: {
cursus_id: 21,
begin_at: {
gte: new Date(`${year}-01-01`),
lt: new Date(`${year + 1}-01-01`),
},
// When a specific year is requested, only include cohorts that started that year.
// When year is null, include all cohorts.
...(year !== null ? {
begin_at: {
gte: new Date(`${year}-01-01`),
lt: new Date(`${year + 1}-01-01`),
},
} : {}),
},
},
// Include dropouts and alumni for the year overview
Expand Down Expand Up @@ -188,4 +193,7 @@ export const buildCommonCoreCache = async function(prisma: PrismaClient) {
console.debug(`Building cache for Cohort ${cohort.year}...`);
await getCommonCoreCohortData(prisma, cohort.year_num, true);
}
// Also build the combined "all cohorts" view
console.debug('Building cache for all cohorts combined...');
await getCommonCoreCohortData(prisma, null, true);
};
8 changes: 8 additions & 0 deletions src/routes/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export const setupCommonCoreRoutes = function(app: Express, prisma: PrismaClient
}
});

app.get('/core/all', passport.authenticate('session'), checkIfStudentOrStaff, async (req, res) => {
const cohorts: Cohort[] = await getAllCohorts(prisma);

const { users, stats, logtimes, dropouts, alumni, projects } = await getCommonCoreCohortData(prisma, null);

return res.render('core.njk', { subtitle: 'All cohorts', cohorts, users, stats, logtimes, dropouts, alumni, projects });
});

app.get('/core/:year', passport.authenticate('session'), checkIfStudentOrStaff, async (req, res) => {
if (!isSingularReqParamInt(req.params.year, /^\d{4}$/)) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions templates/core.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
{# if cohorts are defined in the data given to the template, display a cohort year selection #}
{% if cohorts %}
<select id="cohort-selector">
<option value="0">All cohorts</option>
<option value="0" {{ "selected" if not year }}>All cohorts</option>
{% for cohort in cohorts %}
{# add selected attribute if the current cohort is the one selected (check url against cohort.year) #}
<option value="{{ cohort.year_num | int }}" {{ "selected" if cohort.year == year }}>{{ cohort.year_num | int }} cohort ({{ cohort.user_count_active | int }} / {{ cohort.user_count | int }})</option>
{% endfor %}
</select>
<script>
document.getElementById('cohort-selector').addEventListener('change', function() {
window.location.href = '/core/' + (!parseInt(this.value) ? '' : this.value);
window.location.href = !parseInt(this.value) ? '/core/all' : '/core/' + this.value;
});
</script>
{% endif %}
Expand Down
Loading