From 29b80f60e42e4d84a545b78c0a6aede1d5c29bb6 Mon Sep 17 00:00:00 2001 From: Srijan Jaiswal Date: Thu, 28 May 2026 01:28:37 +0530 Subject: [PATCH] fix: add timeBlocks aggregation to contributions endpoint for CommitTimeChart --- src/app/api/metrics/contributions/route.ts | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/app/api/metrics/contributions/route.ts b/src/app/api/metrics/contributions/route.ts index 007c123f..ae8a2b04 100644 --- a/src/app/api/metrics/contributions/route.ts +++ b/src/app/api/metrics/contributions/route.ts @@ -19,11 +19,19 @@ import { normalizeGitHubUsername } from "@/lib/validate-github-username"; export const dynamic = "force-dynamic"; +interface TimeBlocks { + morning: number; + afternoon: number; + evening: number; + night: number; +} + interface ContributionResponse { days: number; total: number; data: Record; commits: CommitItem[]; + timeBlocks: TimeBlocks; sources?: { github: Record; gitlab?: Record; @@ -149,6 +157,7 @@ async function fetchContributionsForAccount( } const commitsByDay: Record = {}; + const timeBlocks: TimeBlocks = { morning: 0, afternoon: 0, evening: 0, night: 0 }; for (const item of allItems) { const date = item.commit.author.date.slice(0, 10); commitsByDay[date] = (commitsByDay[date] ?? 0) + 1; @@ -159,9 +168,15 @@ async function fetchContributionsForAccount( repo: item.repository?.full_name ?? "unknown", url: item.html_url, }); + + const hour = new Date(item.commit.author.date).getHours(); + if (hour >= 6 && hour < 12) timeBlocks.morning++; + else if (hour >= 12 && hour < 18) timeBlocks.afternoon++; + else if (hour >= 18 && hour < 22) timeBlocks.evening++; + else timeBlocks.night++; } - return { days, total: totalCount, data: commitsByDay, commits: commitItems }; + return { days, total: totalCount, data: commitsByDay, commits: commitItems, timeBlocks }; } ); } @@ -238,6 +253,7 @@ async function fetchGitLabContributions( total: sumContributionDays(commitsByDay), data: commitsByDay, commits: [], + timeBlocks: { morning: 0, afternoon: 0, evening: 0, night: 0 }, }; } ); @@ -267,6 +283,7 @@ async function mergeGitLabContributions( total: combinedTotal, data: combinedData, commits: result.commits, + timeBlocks: result.timeBlocks, sources: { github: result.data, gitlab: gitlabResult.data, @@ -387,6 +404,12 @@ export async function GET(req: NextRequest) { commits: [...a.commits, ...b.commits].sort( (c, d) => d.date.localeCompare(c.date) || d.sha.localeCompare(c.sha) ), + timeBlocks: { + morning: a.timeBlocks.morning + b.timeBlocks.morning, + afternoon: a.timeBlocks.afternoon + b.timeBlocks.afternoon, + evening: a.timeBlocks.evening + b.timeBlocks.evening, + night: a.timeBlocks.night + b.timeBlocks.night, + }, })); if (!merged) {