|
| 1 | +import { PrismaClient } from '@prisma/client'; |
| 2 | +import { DISCO_PISCINE_AI_FUNDA_PROJECTS_ORDER, DISCO_PISCINE_AI_INTER_PROJECTS_ORDER, DISCO_PISCINE_CORE_PYTHON_PROJECTS_ORDER, DISCO_PISCINE_DEPR_PYTHON_PROJECTS_ORDER, DISCO_PISCINE_WEB_PRGM_ESS_PROJECTS_ORDER } from '../intra/projects'; |
| 3 | +import { getPiscineProjects, getAllDiscoPiscines, getTimeSpentBehindComputer, isDiscoPiscineDropout } from '../utils'; |
| 4 | +import { piscineCache } from './cache'; |
| 5 | +import { SYNC_INTERVAL } from '../intra/base'; |
| 6 | +import { DISCO_PISCINE_CURSUS_IDS } from '../intra/cursus'; |
| 7 | + |
| 8 | +export interface DiscoPiscineLogTimes { |
| 9 | + dayOne: number; |
| 10 | + dayTwo: number; |
| 11 | + dayThree: number; |
| 12 | + dayFour: number; |
| 13 | + dayFive: number; |
| 14 | + total: number; |
| 15 | +}; |
| 16 | + |
| 17 | +// TODO: define the types for the disco piscine data explicitly |
| 18 | +export interface DiscoPiscineData { |
| 19 | + users: any[]; |
| 20 | + logtimes: { [login: string]: DiscoPiscineLogTimes }; |
| 21 | + dropouts: { [login: string]: boolean }; |
| 22 | + projects: any[]; |
| 23 | +}; |
| 24 | + |
| 25 | +export const getDiscoPiscineData = async function(prisma: PrismaClient, year: number, week: number, cursus_id: number, noCache: boolean = false): Promise<DiscoPiscineData | null> { |
| 26 | + // Check if the data is already in the cache |
| 27 | + const cacheKey = `disco-${year}-${week}-${cursus_id}`; |
| 28 | + const cachedData = piscineCache.get(cacheKey); |
| 29 | + if (!noCache && cachedData) { |
| 30 | + return cachedData as DiscoPiscineData; |
| 31 | + } |
| 32 | + |
| 33 | + // Find all possible piscines from the database |
| 34 | + const discopiscines = await getAllDiscoPiscines(prisma); |
| 35 | + |
| 36 | + // Get the discovery piscine based on the year and week |
| 37 | + const discopiscine = discopiscines.find(p => p.year_num === year && p.week_num === week && p.cursus.id === cursus_id); |
| 38 | + if (!discopiscine) { |
| 39 | + console.log(`No discovery piscine found for year ${year}, week ${week} and cursus_id ${cursus_id}`); |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + // Get the project ids in order for the given discovery piscine cursus |
| 44 | + const piscineProjectIdsOrdered: number[] = []; |
| 45 | + switch (discopiscine.cursus.id) { |
| 46 | + case 77: |
| 47 | + piscineProjectIdsOrdered.push(...DISCO_PISCINE_AI_INTER_PROJECTS_ORDER); |
| 48 | + break; |
| 49 | + case 79: |
| 50 | + piscineProjectIdsOrdered.push(...DISCO_PISCINE_AI_FUNDA_PROJECTS_ORDER); |
| 51 | + break; |
| 52 | + case 80: |
| 53 | + piscineProjectIdsOrdered.push(...DISCO_PISCINE_CORE_PYTHON_PROJECTS_ORDER); |
| 54 | + break; |
| 55 | + case 69: |
| 56 | + piscineProjectIdsOrdered.push(...DISCO_PISCINE_DEPR_PYTHON_PROJECTS_ORDER); |
| 57 | + break; |
| 58 | + case 3: |
| 59 | + piscineProjectIdsOrdered.push(...DISCO_PISCINE_WEB_PRGM_ESS_PROJECTS_ORDER); |
| 60 | + break; |
| 61 | + default: |
| 62 | + console.warn(`Unknown discovery piscine cursus_id ${discopiscine.cursus.id}, no projects will be included.`); |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + // Find all users with a discovery piscine that matches the end_at of the discopiscine in question |
| 67 | + const users = await prisma.user.findMany({ |
| 68 | + where: { |
| 69 | + login: { |
| 70 | + not: { |
| 71 | + startsWith: '3b3-', |
| 72 | + }, |
| 73 | + }, |
| 74 | + kind: { |
| 75 | + not: "admin", |
| 76 | + }, |
| 77 | + cursus_users: { |
| 78 | + some: { |
| 79 | + cursus_id: discopiscine.cursus.id, |
| 80 | + end_at: { |
| 81 | + in: discopiscine.end_ats, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + include: { |
| 87 | + project_users: { |
| 88 | + where: { |
| 89 | + project_id: { |
| 90 | + in: piscineProjectIdsOrdered, |
| 91 | + }, |
| 92 | + }, |
| 93 | + include: { |
| 94 | + project: true, |
| 95 | + } |
| 96 | + }, |
| 97 | + cursus_users: { |
| 98 | + where: { |
| 99 | + cursus_id: discopiscine.cursus.id, |
| 100 | + end_at: { |
| 101 | + in: discopiscine.end_ats, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + locations: { |
| 106 | + // Only the latest or current one |
| 107 | + take: 1, |
| 108 | + where: { |
| 109 | + primary: true, |
| 110 | + }, |
| 111 | + orderBy: [ |
| 112 | + { begin_at: 'desc' }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + let dropouts: { [login: string]: boolean } = {}; |
| 119 | + for (const user of users) { |
| 120 | + dropouts[user.login] = isDiscoPiscineDropout(user.cursus_users[0]); |
| 121 | + } |
| 122 | + |
| 123 | + // Sort users first by dropout status, then by name |
| 124 | + users.sort((a, b) => { |
| 125 | + if (dropouts[a.login] && !dropouts[b.login]) { |
| 126 | + return 1; |
| 127 | + } |
| 128 | + if (!dropouts[a.login] && dropouts[b.login]) { |
| 129 | + return -1; |
| 130 | + } |
| 131 | + return a.first_name.localeCompare(b.first_name) || a.last_name.localeCompare(b.last_name); |
| 132 | + }); |
| 133 | + |
| 134 | + // Get logtime for each day of the discovery piscine for each user |
| 135 | + let logtimes: { [login: string]: DiscoPiscineLogTimes } = {} |
| 136 | + for (const user of users) { |
| 137 | + const piscineBegin = user.cursus_users[0]?.begin_at; |
| 138 | + const dayTwo = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 1 * 1000); |
| 139 | + const dayThree = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 2 * 1000); |
| 140 | + const dayFour = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 3 * 1000); |
| 141 | + const dayFive = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 4 * 1000); |
| 142 | + const piscineEnd = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 5 * 1000); |
| 143 | + |
| 144 | + const locationsDuringPiscine = await prisma.location.findMany({ |
| 145 | + where: { |
| 146 | + user_id: user.id, |
| 147 | + begin_at: { |
| 148 | + gte: piscineBegin, |
| 149 | + lte: piscineEnd, |
| 150 | + }, |
| 151 | + }, |
| 152 | + orderBy: [ |
| 153 | + { begin_at: 'asc' }, |
| 154 | + ], |
| 155 | + }); |
| 156 | + |
| 157 | + // Calculate seconds spent behind the computer on each day |
| 158 | + logtimes[user.login] = { |
| 159 | + dayOne: getTimeSpentBehindComputer(locationsDuringPiscine, piscineBegin, dayTwo), |
| 160 | + dayTwo: getTimeSpentBehindComputer(locationsDuringPiscine, dayTwo, dayThree), |
| 161 | + dayThree: getTimeSpentBehindComputer(locationsDuringPiscine, dayThree, dayFour), |
| 162 | + dayFour: getTimeSpentBehindComputer(locationsDuringPiscine, dayFour, dayFive), |
| 163 | + dayFive: getTimeSpentBehindComputer(locationsDuringPiscine, dayFive, piscineEnd), |
| 164 | + total: locationsDuringPiscine.reduce((acc, l) => acc + ((l.end_at ? l.end_at.getTime() : Date.now()) - l.begin_at.getTime()) / 1000, 0), |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + const projects = await getPiscineProjects(prisma, piscineProjectIdsOrdered); |
| 169 | + for (const user of users) { |
| 170 | + // Add the missing projects to the user |
| 171 | + for (const project_id of piscineProjectIdsOrdered) { |
| 172 | + if (!user.project_users.find((pu) => pu.project_id === project_id)) { |
| 173 | + user.project_users.push({ |
| 174 | + id: 0, |
| 175 | + project_id: project_id, |
| 176 | + user_id: user.id, |
| 177 | + final_mark: null, |
| 178 | + status: 'not_started', |
| 179 | + validated: false, |
| 180 | + current_team_id: null, |
| 181 | + created_at: new Date(), |
| 182 | + updated_at: new Date(), |
| 183 | + marked_at: null, |
| 184 | + project: projects.find((p) => p.id === project_id)!, |
| 185 | + }); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Remove any projects that are not part of the piscine |
| 190 | + // Sometimes a user did both piscines and has projects from both, so we need to filter them out |
| 191 | + user.project_users = user.project_users.filter((pu) => piscineProjectIdsOrdered.includes(pu.project_id)); |
| 192 | + |
| 193 | + // Order each user's projects based on the order of project ids defined in piscineProjectIdsOrdered |
| 194 | + user.project_users.sort((a, b) => { |
| 195 | + return piscineProjectIdsOrdered.indexOf(a.project_id) - piscineProjectIdsOrdered.indexOf(b.project_id); |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + // Cache the data for the remaining time of the sync interval |
| 200 | + piscineCache.set(cacheKey, { users, logtimes, dropouts, projects }, SYNC_INTERVAL * 60 * 1000); |
| 201 | + |
| 202 | + return { users, logtimes, dropouts, projects }; |
| 203 | +}; |
| 204 | + |
| 205 | +export const buildDiscoPiscineCache = async function(prisma: PrismaClient) { |
| 206 | + const discoPiscines = await getAllDiscoPiscines(prisma); |
| 207 | + for (const discoPiscine of discoPiscines) { |
| 208 | + console.debug(`Building cache for Discovery Piscine ${discoPiscine.year} week ${discoPiscine.week} with cursus_id ${discoPiscine.cursus.id}...`); |
| 209 | + await getDiscoPiscineData(prisma, discoPiscine.year_num, discoPiscine.week_num, discoPiscine.cursus.id, true); |
| 210 | + } |
| 211 | +}; |
0 commit comments