Skip to content

Commit

Permalink
Use new billability excluded tasks instead of LEAVE_TASKS
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed May 22, 2024
1 parent bc3e7f4 commit 2053eb8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/TimeChimpApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface Time {
date: string;
hours: number;
billable: boolean;
taskName: string;
taskId: number;
}

export interface Company {
Expand Down
17 changes: 13 additions & 4 deletions src/content/add-billability-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ const GET_TIMES_WEEKS = SHOW_WEEKS + ROLLING_AVG_WEEKS * 2;
/**
* Adds a billability chart on basis of times for the given date from TimeChimp.
*/
export async function addBillabilityChart(date: Date, user: User) {
await doAddBillabilityChart(date, user).catch((e) =>
console.error(`Error when adding billability chart: ${e}`),
export async function addBillabilityChart(
date: Date,
user: User,
billabilityExcludedTasks: number[],
) {
await doAddBillabilityChart(date, user, billabilityExcludedTasks).catch(
(e) => console.error(`Error when adding billability chart: ${e}`),
);
}

async function doAddBillabilityChart(date: Date, user: User) {
async function doAddBillabilityChart(
date: Date,
user: User,
billabilityExcludedTasks: number[],
) {
const addTimePanel = document.querySelector('.col-md-4');
if (!addTimePanel?.querySelector('form[name="addTimeForm"]')) {
console.debug('Add time form not found, returning');
Expand Down Expand Up @@ -52,6 +60,7 @@ async function doAddBillabilityChart(date: Date, user: User) {
settings.relativeToContractHours ? user.contractHours : undefined,
SHOW_WEEKS,
ROLLING_AVG_WEEKS,
billabilityExcludedTasks,
);
createOrUpdateChart(
stats,
Expand Down
6 changes: 5 additions & 1 deletion src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ async function render(userName?: string) {
billabilityExcludedTasks = await getBillabilityExcludedTaskIds();
}

await addBillabilityChart(currentDate, currentUser);
await addBillabilityChart(
currentDate,
currentUser,
billabilityExcludedTasks,
);
}

/**
Expand Down
35 changes: 20 additions & 15 deletions src/content/stats.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Time } from '../TimeChimpApi';
import { getWeek, getYear } from 'date-fns';

const LEAVE_TASKS = [
'Bijzonder verlof',
'Feestdag',
'Ouderschapsverlof',
'Tijd voor tijd',
'Verlof',
];

type TimesByYearWeek = Record<string, Time[]>;

interface Stats {
Expand All @@ -31,10 +23,15 @@ export function calculateTimeStats(
contractHours: number | undefined,
showWeeks: number,
rollWeeks: number,
billabilityExcludedTasks: number[],
) {
const timesByYearWeek = groupTimesByYearWeek(times);
removeLeaveOnlyWeeks(timesByYearWeek);
const stats = calculateStatsPerWeek(timesByYearWeek, contractHours);
removeLeaveOnlyWeeks(timesByYearWeek, billabilityExcludedTasks);
const stats = calculateStatsPerWeek(
timesByYearWeek,
contractHours,
billabilityExcludedTasks,
);
const rollingStats = calculateRollingStats(stats, showWeeks, rollWeeks);
return rollingStats.reverse();
}
Expand All @@ -53,17 +50,21 @@ function groupTimesByYearWeek(times: Time[]) {
}, {});
}

function removeLeaveOnlyWeeks(timesByYearWeek: TimesByYearWeek) {
function removeLeaveOnlyWeeks(
timesByYearWeek: TimesByYearWeek,
billabilityExcludedTasks: number[],
) {
Object.entries(timesByYearWeek).forEach(([yearWeekStr, times]) => {
if (times.every((t) => LEAVE_TASKS.includes(t.taskName))) {
if (times.every((t) => billabilityExcludedTasks.includes(t.taskId))) {
delete timesByYearWeek[yearWeekStr];
}
});
}

function calculateStatsPerWeek(
timesByYearWeek: TimesByYearWeek,
contractHours?: number,
contractHours: number | undefined,
billabilityExcludedTasks: number[],
) {
return Object.entries(timesByYearWeek)
.map<Stats>(([yearWeekStr, times]) => {
Expand All @@ -76,7 +77,9 @@ function calculateStatsPerWeek(
const excludedLeaveHours = sum(
times
.filter(
(t) => !t.billable && LEAVE_TASKS.includes(t.taskName),
(t) =>
!t.billable &&
billabilityExcludedTasks.includes(t.taskId),
)
.map((t) => t.hours),
);
Expand All @@ -96,7 +99,9 @@ function calculateStatsPerWeek(
totalHours: sum(times.map((t) => t.hours)),
leaveHours: sum(
times
.filter((t) => LEAVE_TASKS.includes(t.taskName))
.filter((t) =>
billabilityExcludedTasks.includes(t.taskId),
)
.map((t) => t.hours),
),
billableHoursPercentage: calculateHoursPercentage(
Expand Down

0 comments on commit 2053eb8

Please sign in to comment.