|
| 1 | +import { PrismaClient } from "@prisma/client/extension"; |
| 2 | +import { coreCache } from "./cache"; |
| 3 | +import { Logtimes, Stat, UserListData } from "./userlist"; |
| 4 | +import { getAllCohorts, getCommonCoreProjects, getTimeSpentBehindComputer, isCoreDropout } from "../utils"; |
| 5 | +import { User } from "@prisma/client"; |
| 6 | +import { SYNC_INTERVAL } from "../intra/base"; |
| 7 | +import { COMMON_CORE_PROJECTS_ORDER } from "../intra/projects"; |
| 8 | + |
| 9 | +export interface CommonCoreLogtimes extends Logtimes { |
| 10 | + pastWeek: number; |
| 11 | + pastMonth: number; |
| 12 | + pastQuarter: number; |
| 13 | +}; |
| 14 | + |
| 15 | +export interface CommonCoreStat extends Stat { |
| 16 | + // No additions |
| 17 | +}; |
| 18 | + |
| 19 | +export interface CommonCoreData extends UserListData { |
| 20 | + alumni: { [login: string]: boolean }; |
| 21 | +}; |
| 22 | + |
| 23 | +export const getCommonCoreCohortData = async function(prisma: PrismaClient, year: number, noCache: boolean = false): Promise<CommonCoreData> { |
| 24 | + // Check if the data is already in the cache |
| 25 | + const cacheKey = `core-${year}`; |
| 26 | + const cachedData = coreCache.get(cacheKey); |
| 27 | + if (!noCache && cachedData) { |
| 28 | + return cachedData as CommonCoreData; |
| 29 | + } |
| 30 | + |
| 31 | + // Initiate statistics array |
| 32 | + const stats: CommonCoreStat[] = []; |
| 33 | + |
| 34 | + // Find all users for the given year |
| 35 | + const users = await prisma.user.findMany({ |
| 36 | + where: { |
| 37 | + login: { |
| 38 | + not: { |
| 39 | + startsWith: '3b3-', |
| 40 | + }, |
| 41 | + }, |
| 42 | + kind: { |
| 43 | + not: "admin", |
| 44 | + }, |
| 45 | + cursus_users: { |
| 46 | + some: { |
| 47 | + cursus_id: 21, |
| 48 | + begin_at: { |
| 49 | + gte: new Date(`${year}-01-01`), |
| 50 | + lt: new Date(`${year + 1}-01-01`), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + // Include dropouts and alumni for the year overview |
| 55 | + }, |
| 56 | + include: { |
| 57 | + project_users: { |
| 58 | + where: { |
| 59 | + project: { |
| 60 | + cursus_id: 21, |
| 61 | + }, |
| 62 | + }, |
| 63 | + include: { |
| 64 | + project: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + cursus_users: { |
| 68 | + where: { |
| 69 | + cursus_id: 21, |
| 70 | + }, |
| 71 | + }, |
| 72 | + locations: { |
| 73 | + // Only the latest or current one |
| 74 | + take: 1, |
| 75 | + where: { |
| 76 | + primary: true, |
| 77 | + }, |
| 78 | + orderBy: [ |
| 79 | + { begin_at: 'desc' }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + }, |
| 83 | + orderBy: [ |
| 84 | + { usual_full_name: 'asc' } |
| 85 | + ], |
| 86 | + }); |
| 87 | + stats.push({ |
| 88 | + label: 'Total students', |
| 89 | + value: users.length, |
| 90 | + unit: null, |
| 91 | + }); |
| 92 | + |
| 93 | + // Check for each student if they are a dropout |
| 94 | + let dropouts: { [login: string]: boolean } = {}; |
| 95 | + for (const user of users) { |
| 96 | + dropouts[user.login] = isCoreDropout(user.cursus_users[0], user); |
| 97 | + } |
| 98 | + stats.push({ |
| 99 | + label: 'Dropouts', |
| 100 | + value: Object.values(dropouts).filter(isDropout => isDropout).length, |
| 101 | + unit: null, |
| 102 | + }); |
| 103 | + |
| 104 | + // Check for each student if they are alumni |
| 105 | + let alumni: { [login: string]: boolean } = {}; |
| 106 | + for (const user of users) { |
| 107 | + alumni[user.login] = user.alumnized_at !== null; |
| 108 | + } |
| 109 | + stats.push({ |
| 110 | + label: 'Alumni', |
| 111 | + value: Object.values(alumni).filter(isAlumnus => isAlumnus).length, |
| 112 | + unit: null, |
| 113 | + }); |
| 114 | + stats.push({ |
| 115 | + label: 'Remaining', |
| 116 | + value: users.length - (Object.values(dropouts).filter(isDropout => isDropout).length + Object.values(alumni).filter(isAlumnus => isAlumnus).length), |
| 117 | + unit: null, |
| 118 | + }); |
| 119 | + |
| 120 | + // Sort users first by dropout status, then by name |
| 121 | + users.sort((a: User, b: User) => { |
| 122 | + if (dropouts[a.login] && !dropouts[b.login]) { |
| 123 | + return 1; |
| 124 | + } |
| 125 | + if (!dropouts[a.login] && dropouts[b.login]) { |
| 126 | + return -1; |
| 127 | + } |
| 128 | + return a.first_name.localeCompare(b.first_name) || a.last_name.localeCompare(b.last_name); |
| 129 | + }); |
| 130 | + |
| 131 | + // Get total logtime for each user |
| 132 | + let logtimes: { [login: string]: CommonCoreLogtimes } = {}; |
| 133 | + for (const user of users) { |
| 134 | + const cursusStart = user.cursus_users[0].begin_at; |
| 135 | + const cursusEndOrNow = user.cursus_users[0].end_at || new Date(); |
| 136 | + const oneWeekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); |
| 137 | + const oneMonthAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); |
| 138 | + const threeMonthsAgo = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000); |
| 139 | + const locations = await prisma.location.findMany({ |
| 140 | + where: { |
| 141 | + user_id: user.id, |
| 142 | + begin_at: { |
| 143 | + gte: cursusStart, |
| 144 | + lte: cursusEndOrNow, |
| 145 | + }, |
| 146 | + }, |
| 147 | + orderBy: [ |
| 148 | + { begin_at: 'asc' }, |
| 149 | + ], |
| 150 | + }); |
| 151 | + logtimes[user.login] = { |
| 152 | + total: getTimeSpentBehindComputer(locations, cursusStart, cursusEndOrNow), |
| 153 | + pastWeek: getTimeSpentBehindComputer(locations, oneWeekAgo, cursusEndOrNow), |
| 154 | + pastMonth: getTimeSpentBehindComputer(locations, oneMonthAgo, cursusEndOrNow), |
| 155 | + pastQuarter: getTimeSpentBehindComputer(locations, threeMonthsAgo, cursusEndOrNow), |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + const projects = await getCommonCoreProjects(prisma, COMMON_CORE_PROJECTS_ORDER); |
| 160 | + for (const user of users) { |
| 161 | + // Remove any projects that are not part of the common core |
| 162 | + user.project_users = user.project_users.filter((pu: any) => COMMON_CORE_PROJECTS_ORDER.includes(pu.project_id)); |
| 163 | + |
| 164 | + // Order each user's projects based on the order of project ids defined in COMMON_CORE_PROJECTS_ORDER |
| 165 | + user.project_users.sort((a: any, b: any) => { |
| 166 | + return COMMON_CORE_PROJECTS_ORDER.indexOf(a.project_id) - COMMON_CORE_PROJECTS_ORDER.indexOf(b.project_id); |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + // Cache the data for the remaining time of the sync interval |
| 171 | + coreCache.set(cacheKey, { users, stats, logtimes, dropouts, alumni, projects }, SYNC_INTERVAL * 60 * 1000); |
| 172 | + |
| 173 | + return { |
| 174 | + users, |
| 175 | + stats, |
| 176 | + logtimes, |
| 177 | + dropouts, |
| 178 | + alumni, |
| 179 | + projects, |
| 180 | + } as CommonCoreData; |
| 181 | +} |
| 182 | + |
| 183 | +export const buildCommonCoreCache = async function(prisma: PrismaClient) { |
| 184 | + const cohorts = await getAllCohorts(prisma); |
| 185 | + for (const cohort of cohorts) { |
| 186 | + console.debug(`Building cache for Cohort ${cohort.year}...`); |
| 187 | + await getCommonCoreCohortData(prisma, cohort.year_num, true); |
| 188 | + } |
| 189 | +}; |
0 commit comments