Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions app/api/shifts/stats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export async function GET(request: Request) {
// Fetch shifts for the period, excluding shifts from iCloud syncs or presets that are hidden from stats
const result = await db
.select({
id: shifts.id,
title: shifts.title,
date: shifts.date,
startTime: shifts.startTime,
endTime: shifts.endTime,
isAllDay: shifts.isAllDay,
Expand Down Expand Up @@ -111,17 +113,57 @@ export async function GET(request: Request) {

// Group by title and calculate stats
const statsMap = new Map<string, { count: number; totalMinutes: number }>();
const dailyStats = new Map<
string,
{ count: number; totalMinutes: number }
>();
let minDuration = Infinity;
let maxDuration = 0;

result.forEach((shift) => {
const existing = statsMap.get(shift.title) || {
count: 0,
totalMinutes: 0,
};
existing.count++;
existing.totalMinutes += shift.isAllDay
const duration = shift.isAllDay
? 0
: calculateShiftDuration(shift.startTime, shift.endTime);
existing.totalMinutes += duration;
statsMap.set(shift.title, existing);

// Track min/max duration (exclude all-day shifts)
if (!shift.isAllDay && duration > 0) {
minDuration = Math.min(minDuration, duration);
maxDuration = Math.max(maxDuration, duration);
}

// Daily breakdown for trend analysis
const shiftDate = shift.date;
if (shiftDate) {
try {
// Handle both Date objects and Unix timestamps (in seconds for SQLite)
const date =
shiftDate instanceof Date
? shiftDate
: typeof shiftDate === "number"
? new Date(shiftDate * 1000) // Convert seconds to milliseconds
: new Date(shiftDate);

if (!isNaN(date.getTime())) {
const dateKey = date.toISOString().split("T")[0];
const dailyExisting = dailyStats.get(dateKey) || {
count: 0,
totalMinutes: 0,
};
dailyExisting.count++;
dailyExisting.totalMinutes += duration;
dailyStats.set(dateKey, dailyExisting);
}
} catch {
// Skip invalid dates
}
}
});

// Transform result to object format
Expand All @@ -136,18 +178,45 @@ export async function GET(request: Request) {
{} as Record<string, { count: number; totalMinutes: number }>
);

// Calculate total duration
// Calculate total duration and averages
const totalMinutes = Object.values(stats).reduce(
(sum, data) => sum + data.totalMinutes,
0
);

const totalShifts = result.length;
const avgMinutesPerShift = totalShifts > 0 ? totalMinutes / totalShifts : 0;

// Calculate days with shifts for accurate daily average
const daysWithShifts = dailyStats.size;
const avgShiftsPerDay =
daysWithShifts > 0 ? totalShifts / daysWithShifts : 0;
const avgMinutesPerDay =
daysWithShifts > 0 ? totalMinutes / daysWithShifts : 0;

// Convert daily stats to array for trend visualization
const trendData = Array.from(dailyStats.entries())
.map(([date, data]) => ({
date,
count: data.count,
totalMinutes: data.totalMinutes,
}))
.sort((a, b) => a.date.localeCompare(b.date));

return NextResponse.json({
period,
startDate: startDate.toISOString(),
endDate: endDate.toISOString(),
stats,
totalMinutes,
totalShifts,
avgMinutesPerShift: Math.round(avgMinutesPerShift),
avgShiftsPerDay: Math.round(avgShiftsPerDay * 10) / 10,
avgMinutesPerDay: Math.round(avgMinutesPerDay),
minDuration: minDuration === Infinity ? 0 : minDuration,
maxDuration,
daysWithShifts,
trendData,
});
} catch (error) {
console.error("Failed to fetch shift statistics:", error);
Expand Down
14 changes: 7 additions & 7 deletions components/calendar-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ export function CalendarGrid({
return (
<div className="grid grid-cols-7 gap-0 sm:gap-1.5 mb-6">
{[
t("calendar_grid.monday"),
t("calendar_grid.tuesday"),
t("calendar_grid.wednesday"),
t("calendar_grid.thursday"),
t("calendar_grid.friday"),
t("calendar_grid.saturday"),
t("calendar_grid.sunday"),
t("common.weekday.monday"),
t("common.weekday.tuesday"),
t("common.weekday.wednesday"),
t("common.weekday.thursday"),
t("common.weekday.friday"),
t("common.weekday.saturday"),
t("common.weekday.sunday"),
].map((day) => (
<div
key={day}
Expand Down
Loading