Skip to content
Merged
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
25 changes: 24 additions & 1 deletion src/app/api/metrics/contributions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>;
commits: CommitItem[];
timeBlocks: TimeBlocks;
sources?: {
github: Record<string, number>;
gitlab?: Record<string, number>;
Expand Down Expand Up @@ -149,6 +157,7 @@ async function fetchContributionsForAccount(
}

const commitsByDay: Record<string, number> = {};
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;
Expand All @@ -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 };
}
);
}
Expand Down Expand Up @@ -238,6 +253,7 @@ async function fetchGitLabContributions(
total: sumContributionDays(commitsByDay),
data: commitsByDay,
commits: [],
timeBlocks: { morning: 0, afternoon: 0, evening: 0, night: 0 },
};
}
);
Expand Down Expand Up @@ -267,6 +283,7 @@ async function mergeGitLabContributions(
total: combinedTotal,
data: combinedData,
commits: result.commits,
timeBlocks: result.timeBlocks,
sources: {
github: result.data,
gitlab: gitlabResult.data,
Expand Down Expand Up @@ -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) {
Expand Down
Loading