From 47210991f873fc61c91ff947cf33dea7bb9eef11 Mon Sep 17 00:00:00 2001 From: Luca Scalzotto Date: Thu, 4 Jan 2024 17:05:16 +0100 Subject: [PATCH] Fix week ordering between years (#5) --- CHANGELOG.md | 4 ++++ manifest.json | 2 +- src/content/stats.ts | 45 ++++++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 232e30f..7681641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.5.2 + +- Fix week ordering between years. + ## v1.5.1 - Add "Tijd voor tijd" to leave tasks. diff --git a/manifest.json b/manifest.json index 736ca91..b0f527d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "TimeChimp Billability Chart", "description": "Adds a billability chart on the TimeChimp hours page.", - "version": "1.5.1", + "version": "1.5.2", "manifest_version": 3, "permissions": [ "webRequest" diff --git a/src/content/stats.ts b/src/content/stats.ts index 3abd426..2a56dbd 100644 --- a/src/content/stats.ts +++ b/src/content/stats.ts @@ -1,5 +1,5 @@ import { Time } from '../TimeChimpApi'; -import { getWeek } from 'date-fns'; +import { getWeek, getYear } from 'date-fns'; const LEAVE_TASKS = [ 'Bijzonder verlof', @@ -8,9 +8,10 @@ const LEAVE_TASKS = [ 'Verlof', ]; -type TimesByWeek = Record; +type TimesByYearWeek = Record; interface Stats { + year: number; week: number; billableHours: number; nonBillableHours: number; @@ -29,37 +30,38 @@ export function calculateTimeStats( showWeeks: number, rollWeeks: number, ) { - const timesByWeek = groupTimesByWeek(times); - removeLeaveOnlyWeeks(timesByWeek); - const stats = calculateStatsPerWeek(timesByWeek); + const timesByYearWeek = groupTimesByYearWeek(times); + removeLeaveOnlyWeeks(timesByYearWeek); + const stats = calculateStatsPerWeek(timesByYearWeek); const rollingStats = calculateRollingStats(stats, showWeeks, rollWeeks); return rollingStats.reverse(); } -function groupTimesByWeek(times: Time[]) { - return times.reduce((acc, time) => { - const week = getWeek(new Date(time.date)); +function groupTimesByYearWeek(times: Time[]) { + return times.reduce((acc, time) => { + const date = new Date(time.date); + const yearWeek = `${getYear(date)}-${getWeek(date)}`; - if (!acc[week]) { - acc[week] = []; + if (!acc[yearWeek]) { + acc[yearWeek] = []; } - acc[week].push(time); + acc[yearWeek].push(time); return acc; - }, []); + }, {}); } -function removeLeaveOnlyWeeks(timesByWeek: TimesByWeek) { - Object.entries(timesByWeek).forEach(([weekStr, times]) => { +function removeLeaveOnlyWeeks(timesByYearWeek: TimesByYearWeek) { + Object.entries(timesByYearWeek).forEach(([yearWeekStr, times]) => { if (times.every((t) => LEAVE_TASKS.includes(t.taskName))) { - delete timesByWeek[Number(weekStr)]; + delete timesByYearWeek[yearWeekStr]; } }); } -function calculateStatsPerWeek(timesByWeek: TimesByWeek) { - return Object.entries(timesByWeek) - .map(([weekStr, times]) => { +function calculateStatsPerWeek(timesByYearWeek: TimesByYearWeek) { + return Object.entries(timesByYearWeek) + .map(([yearWeekStr, times]) => { const billableHours = sum( times.filter((t) => t.billable).map((t) => t.hours), ); @@ -73,7 +75,8 @@ function calculateStatsPerWeek(timesByWeek: TimesByWeek) { const totalHoursWithoutLeave = billableHours + nonBillableHours; return { - week: Number(weekStr), + year: Number(yearWeekStr.substring(0, 4)), + week: Number(yearWeekStr.substring(5)), billableHours, nonBillableHours, totalHours: sum(times.map((t) => t.hours)), @@ -88,7 +91,9 @@ function calculateStatsPerWeek(timesByWeek: TimesByWeek) { (100 * nonBillableHours) / totalHoursWithoutLeave, }; }) - .sort((a, b) => b.week - a.week); + .sort((a, b) => + a.year === b.year ? b.week - a.week : b.year - a.year, + ); } function calculateRollingStats(