2026-11-26 17:43:53 +03:00

113 lines
3.3 KiB
JavaScript

import { languageForFile } from "./languages.mjs";
const WEEKDAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const DAY_MS = 24 * 60 * 60 * 1000;
/**
* @param {import("./log.mjs").Commit[]} commits
* @param {{ year?: number, sinceDays?: number }} window
* @returns {import("./log.mjs").Commit[]}
*/
export function filterByWindow(commits, window) {
if (window.year) {
const start = new Date(Date.UTC(window.year, 0, 1));
const end = new Date(Date.UTC(window.year + 1, 0, 1));
return commits.filter((c) => c.date >= start && c.date < end);
}
if (window.sinceDays) {
const cutoff = new Date(Date.now() - window.sinceDays * DAY_MS);
return commits.filter((c) => c.date >= cutoff);
}
return commits;
}
function dateKey(date) {
return date.toISOString().slice(0, 10); // YYYY-MM-DD, UTC
}
/**
* @param {import("./log.mjs").Commit[]} commits
*/
export function computeStats(commits) {
if (commits.length === 0) {
return {
totalCommits: 0,
activeDays: 0,
longestStreak: 0,
busiestWeekday: null,
busiestHour: null,
topContributors: [],
topLanguages: [],
firstDate: null,
lastDate: null,
};
}
const sorted = [...commits].sort((a, b) => a.date - b.date);
const dayCounts = new Map();
const weekdayCounts = new Array(7).fill(0);
const hourCounts = new Array(24).fill(0);
const contributorCounts = new Map();
const languageCounts = new Map();
for (const commit of sorted) {
const key = dateKey(commit.date);
dayCounts.set(key, (dayCounts.get(key) || 0) + 1);
weekdayCounts[commit.date.getUTCDay()] += 1;
hourCounts[commit.date.getUTCHours()] += 1;
const who = commit.authorName || commit.authorEmail || "unknown";
contributorCounts.set(who, (contributorCounts.get(who) || 0) + 1);
const seenLanguages = new Set();
for (const file of commit.files) {
const lang = languageForFile(file);
if (lang && !seenLanguages.has(lang)) {
seenLanguages.add(lang);
languageCounts.set(lang, (languageCounts.get(lang) || 0) + 1);
}
}
}
const activeDayKeys = [...dayCounts.keys()].sort();
let longestStreak = 0;
let currentRun = 0;
let previousKey = null;
for (const key of activeDayKeys) {
if (previousKey !== null) {
const gapDays = Math.round((Date.parse(key) - Date.parse(previousKey)) / DAY_MS);
currentRun = gapDays === 1 ? currentRun + 1 : 1;
} else {
currentRun = 1;
}
longestStreak = Math.max(longestStreak, currentRun);
previousKey = key;
}
const busiestWeekdayIndex = weekdayCounts.indexOf(Math.max(...weekdayCounts));
const busiestHour = hourCounts.indexOf(Math.max(...hourCounts));
const topContributors = [...contributorCounts.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, 3)
.map(([name, count]) => ({ name, count }));
const topLanguages = [...languageCounts.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(([language, commitsTouched]) => ({ language, commitsTouched }));
return {
totalCommits: sorted.length,
activeDays: dayCounts.size,
longestStreak,
busiestWeekday: WEEKDAY_NAMES[busiestWeekdayIndex],
busiestHour,
topContributors,
topLanguages,
firstDate: sorted[0].date,
lastDate: sorted[sorted.length - 1].date,
};
}