Skip to content

Commit fe98bce

Browse files
committed
fix: inaccurate logtime per week in piscine overview
weeks did not properly start at 0:00 of each date and API locations were not truncated and did not allow overflowing weekly boundaries this implementation copies 42's Chronos system's behavior and is way more accurate.
1 parent 84743da commit fe98bce

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/handlers/piscine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const getCPiscineData = async function(prisma: PrismaClient, year: number
143143
// Get logtime for each week of the piscine for each user
144144
let logtimes: { [login: string]: CPiscineLogTimes } = {}
145145
for (const user of users) {
146-
const piscineBegin = user.cursus_users[0]?.begin_at;
146+
const piscineBegin = new Date(user.cursus_users[0]?.begin_at.setHours(0, 0, 0, 0));
147147
const weekTwo = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 7 * 1000);
148148
const weekThree = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 14 * 1000);
149149
const weekFour = new Date(piscineBegin.getTime() + 60 * 60 * 24 * 21 * 1000);

src/utils.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,21 @@ export const getAllCohorts = async function(prisma: PrismaClient): Promise<Cohor
304304
* @returns The amount of seconds spent behind a computer as an integer
305305
*/
306306
export const getTimeSpentBehindComputer = function(locations: Location[], lowerBound: Date, upperBound: Date): number {
307-
return locations.filter((l) => l.begin_at >= lowerBound && l.begin_at <= upperBound).reduce((acc, l) => acc + ((l.end_at ? l.end_at.getTime() : Date.now()) - l.begin_at.getTime()) / 1000, 0);
307+
// Filter locations based on their begin_at and end_at. Allow overflow of locations outside the bounds.
308+
const filtered = locations.filter((l) => {
309+
return (l.begin_at >= lowerBound && l.begin_at <= upperBound) ||
310+
(l.end_at && l.end_at >= lowerBound && l.end_at <= upperBound);
311+
});
312+
// Truncate the begin_ats and end_ats based on the lowerBound and upperBound
313+
filtered.forEach((l) => {
314+
if (l.begin_at < lowerBound) {
315+
l.begin_at = lowerBound;
316+
}
317+
if (l.end_at && l.end_at > upperBound) {
318+
l.end_at = upperBound;
319+
}
320+
});
321+
return filtered.reduce((acc, l) => acc + ((l.end_at ? l.end_at.getTime() : Date.now()) - l.begin_at.getTime()) / 1000, 0);
308322
};
309323

310324
export const getPiscineProjects = async function(prisma: PrismaClient, piscineProjectIdsOrdered: number[]): Promise<Project[]> {

0 commit comments

Comments
 (0)