Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,321 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
|
||||
const REPO_ROOT = path.resolve(SCRIPT_DIR, "../..");
|
||||
|
||||
export const COMMIT_HISTORY_DAYS = 60;
|
||||
export const COMMIT_HISTORY_TIME_ZONE = "Europe/Berlin";
|
||||
|
||||
const DEFAULTS = Object.freeze({
|
||||
days: COMMIT_HISTORY_DAYS,
|
||||
label: null,
|
||||
outputDir: "docs/metrics",
|
||||
ref: "origin/main",
|
||||
});
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage: node scripts/metrics/commit-history.mjs [options]
|
||||
|
||||
Options:
|
||||
--ref <git-ref> Revision to inspect (default: ${DEFAULTS.ref})
|
||||
--label <text> Ref label rendered in outputs (default: value of --ref)
|
||||
--days <number> Calendar days to render (default: ${DEFAULTS.days})
|
||||
--output-dir <path> Output directory (default: ${DEFAULTS.outputDir})
|
||||
--help Show this help
|
||||
|
||||
Outputs:
|
||||
commit-history.csv Daily commit counts
|
||||
commit-history.svg Standalone 60-day chart
|
||||
|
||||
All commits reachable from the selected revision are counted by committer date,
|
||||
including merge commits and commits introduced by merged branches. Calendar days
|
||||
are grouped in ${COMMIT_HISTORY_TIME_ZONE}; days without commits remain visible.`);
|
||||
}
|
||||
|
||||
export function parseArgs(argv) {
|
||||
const options = { ...DEFAULTS };
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const argument = argv[index];
|
||||
if (argument === "--help") {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
if (argument === "--ref" || argument === "--label" || argument === "--days" || argument === "--output-dir") {
|
||||
const value = argv[index + 1];
|
||||
if (!value || value.startsWith("--")) throw new Error(`Missing value for ${argument}`);
|
||||
if (argument === "--ref") options.ref = value;
|
||||
if (argument === "--label") options.label = value;
|
||||
if (argument === "--output-dir") options.outputDir = value;
|
||||
if (argument === "--days") {
|
||||
const days = Number(value);
|
||||
if (!Number.isInteger(days) || days < 1 || days > 365) {
|
||||
throw new Error("--days must be an integer between 1 and 365");
|
||||
}
|
||||
options.days = days;
|
||||
}
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
throw new Error(`Unknown argument: ${argument}`);
|
||||
}
|
||||
|
||||
return { ...options, label: options.label ?? options.ref };
|
||||
}
|
||||
|
||||
function git(args, options = {}) {
|
||||
return execFileSync("git", args, {
|
||||
cwd: options.repoRoot ?? REPO_ROOT,
|
||||
encoding: "utf8",
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
});
|
||||
}
|
||||
|
||||
export function toBerlinDateKey(timestamp) {
|
||||
const date = new Date(timestamp);
|
||||
if (Number.isNaN(date.getTime())) throw new Error(`Invalid Git timestamp: ${timestamp}`);
|
||||
|
||||
const parts = new Intl.DateTimeFormat("en-US", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
timeZone: COMMIT_HISTORY_TIME_ZONE,
|
||||
year: "numeric",
|
||||
}).formatToParts(date);
|
||||
const value = (type) => parts.find((part) => part.type === type)?.value;
|
||||
return `${value("year")}-${value("month")}-${value("day")}`;
|
||||
}
|
||||
|
||||
function addCalendarDays(dateKey, amount) {
|
||||
const [year, month, day] = dateKey.split("-").map(Number);
|
||||
const date = new Date(Date.UTC(year, month - 1, day + amount, 12));
|
||||
return date.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
export function collectCommitHistory(ref, { days = COMMIT_HISTORY_DAYS, repoRoot = REPO_ROOT, runGit = git } = {}) {
|
||||
const resolvedRef = runGit(["rev-parse", "--verify", `${ref}^{commit}`], { repoRoot }).trim();
|
||||
const tipTimestamp = runGit(["show", "-s", "--format=%cI", resolvedRef], { repoRoot }).trim();
|
||||
const endDate = toBerlinDateKey(tipTimestamp);
|
||||
const startDate = addCalendarDays(endDate, -(days - 1));
|
||||
const rows = [];
|
||||
const counts = new Map();
|
||||
|
||||
for (let offset = 0; offset < days; offset += 1) {
|
||||
const date = addCalendarDays(startDate, offset);
|
||||
const row = { count: 0, date };
|
||||
rows.push(row);
|
||||
counts.set(date, row);
|
||||
}
|
||||
|
||||
const log = runGit(["log", resolvedRef, "--format=%H%x09%cI"], { repoRoot }).trim();
|
||||
if (log) {
|
||||
for (const line of log.split("\n")) {
|
||||
const [sha, timestamp] = line.split("\t");
|
||||
if (!sha || !timestamp) throw new Error(`Malformed Git log entry: ${line}`);
|
||||
const row = counts.get(toBerlinDateKey(timestamp));
|
||||
if (row) row.count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return { days, endDate, resolvedRef, rows, startDate, tipTimestamp };
|
||||
}
|
||||
|
||||
function escapeXml(value) {
|
||||
return String(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function formatNumber(value) {
|
||||
return new Intl.NumberFormat("de-DE").format(Math.round(value));
|
||||
}
|
||||
|
||||
function formatDecimal(value) {
|
||||
return new Intl.NumberFormat("de-DE", {
|
||||
maximumFractionDigits: 1,
|
||||
minimumFractionDigits: 1,
|
||||
}).format(value);
|
||||
}
|
||||
|
||||
function formatDisplayDate(dateKey) {
|
||||
const [, month, day] = dateKey.split("-");
|
||||
return `${day}.${month}.`;
|
||||
}
|
||||
|
||||
function niceAxisMaximum(value) {
|
||||
if (value <= 0) return 1;
|
||||
const roughStep = value / 4;
|
||||
const magnitude = 10 ** Math.floor(Math.log10(roughStep));
|
||||
const normalized = roughStep / magnitude;
|
||||
const niceNormalized = normalized <= 1 ? 1 : normalized <= 2 ? 2 : normalized <= 2.5 ? 2.5 : normalized <= 4 ? 4 : normalized <= 5 ? 5 : 10;
|
||||
const step = niceNormalized * magnitude;
|
||||
return Math.ceil(value / step) * step;
|
||||
}
|
||||
|
||||
export function renderCsv(history, ref) {
|
||||
const metadata = [
|
||||
`# ref=${ref}`,
|
||||
`# resolved_tip=${history.resolvedRef}`,
|
||||
`# tip_timestamp=${history.tipTimestamp}`,
|
||||
"# metric=commits reachable from ref grouped by committer date",
|
||||
`# window_days=${history.days}`,
|
||||
`# timezone=${COMMIT_HISTORY_TIME_ZONE}`,
|
||||
"# included=merge commits and commits introduced by merged branches",
|
||||
"# excluded=commits reachable only from other branches such as loc-metrics",
|
||||
];
|
||||
const body = history.rows.map((row) => `${row.date},${row.count}`);
|
||||
return `${[...metadata, "date,commit_count", ...body].join("\n")}\n`;
|
||||
}
|
||||
|
||||
export function renderSvg(history, ref) {
|
||||
const width = 1600;
|
||||
const height = 900;
|
||||
const plot = { bottom: 730, left: 126, right: 1518, top: 320 };
|
||||
const plotWidth = plot.right - plot.left;
|
||||
const plotHeight = plot.bottom - plot.top;
|
||||
const rows = history.rows;
|
||||
if (rows.length === 0) throw new Error("Commit history requires at least one calendar day");
|
||||
|
||||
const latest = rows.at(-1);
|
||||
const latestIndex = rows.length - 1;
|
||||
const lastSeven = rows.slice(-7).reduce((sum, row) => sum + row.count, 0);
|
||||
const total = rows.reduce((sum, row) => sum + row.count, 0);
|
||||
const average = total / rows.length;
|
||||
const peakCount = Math.max(...rows.map((row) => row.count));
|
||||
const peakIndex = rows.findIndex((row) => row.count === peakCount);
|
||||
const peak = rows[peakIndex];
|
||||
const axisMaximum = niceAxisMaximum(peakCount * 1.02);
|
||||
const step = plotWidth / rows.length;
|
||||
const barWidth = Math.max(4, Math.min(28, step * 0.6));
|
||||
const x = (index) => plot.left + (index + 0.5) * step;
|
||||
const y = (value) => plot.bottom - (value / axisMaximum) * plotHeight;
|
||||
const barHeight = (value) => (value / axisMaximum) * plotHeight;
|
||||
const trendPoints = [];
|
||||
for (let index = 6; index < rows.length; index += 1) {
|
||||
const movingAverage = rows.slice(index - 6, index + 1).reduce((sum, row) => sum + row.count, 0) / 7;
|
||||
trendPoints.push(`${x(index).toFixed(1)},${y(movingAverage).toFixed(1)}`);
|
||||
}
|
||||
|
||||
const cards = [
|
||||
{ color: "#0F172A", label: "HEUTE", value: `${formatNumber(latest.count)} Commits` },
|
||||
{ color: "#0F766E", label: "LETZTE 7 TAGE", value: `${formatNumber(lastSeven)} Commits` },
|
||||
{ color: "#F59E0B", label: `Ø PRO TAG · ${history.days} TAGE`, value: `${formatDecimal(average)} Commits` },
|
||||
{ color: "#8B5CF6", label: `PEAK · ${formatDisplayDate(peak.date)}`, value: `${formatNumber(peak.count)} Commits` },
|
||||
];
|
||||
|
||||
const svg = [];
|
||||
svg.push(`<?xml version="1.0" encoding="UTF-8"?>`);
|
||||
svg.push(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-labelledby="title description">`
|
||||
);
|
||||
svg.push(`<title id="title">HiveGrid Commit-Aktivität auf ${escapeXml(ref)}</title>`);
|
||||
svg.push(
|
||||
`<desc id="description">Tägliche Anzahl aller von ${escapeXml(ref)} erreichbaren Commits für die letzten ${history.days} Kalendertage, gruppiert in der Zeitzone ${COMMIT_HISTORY_TIME_ZONE}, mit Sieben-Tage-Durchschnitt.</desc>`
|
||||
);
|
||||
svg.push(
|
||||
`<style>.text{font-family:Inter,ui-sans-serif,system-ui,sans-serif}.grid{stroke:#E2E8F0;stroke-width:1}.axis-label{fill:#64748B;font-size:15px}.bar{fill:#0F766E;fill-opacity:.82}</style>`
|
||||
);
|
||||
svg.push(`<rect width="${width}" height="${height}" fill="#F8FAFC"/>`);
|
||||
svg.push(`<text class="text" x="78" y="74" fill="#0F172A" font-size="36" font-weight="750">HiveGrid · Commit-Aktivität</text>`);
|
||||
svg.push(
|
||||
`<text class="text" x="78" y="110" fill="#64748B" font-size="18">${escapeXml(ref)} @ ${history.resolvedRef.slice(0, 9)} · letzte ${history.days} Kalendertage · ${COMMIT_HISTORY_TIME_ZONE} · heute laufend</text>`
|
||||
);
|
||||
|
||||
cards.forEach((card, index) => {
|
||||
const cardX = 78 + index * 366;
|
||||
svg.push(`<rect x="${cardX}" y="142" width="340" height="92" rx="14" fill="#FFFFFF" stroke="#E2E8F0"/>`);
|
||||
svg.push(`<rect x="${cardX}" y="142" width="6" height="92" rx="3" fill="${card.color}"/>`);
|
||||
svg.push(
|
||||
`<text class="text" x="${cardX + 26}" y="174" fill="#64748B" font-size="14" font-weight="650" letter-spacing="0.5">${escapeXml(card.label)}</text>`
|
||||
);
|
||||
svg.push(`<text class="text" x="${cardX + 26}" y="211" fill="#0F172A" font-size="29" font-weight="750">${escapeXml(card.value)}</text>`);
|
||||
});
|
||||
|
||||
for (let tick = 0; tick <= 4; tick += 1) {
|
||||
const value = (axisMaximum / 4) * tick;
|
||||
const tickY = y(value);
|
||||
svg.push(`<line class="grid" x1="${plot.left}" y1="${tickY}" x2="${plot.right}" y2="${tickY}"${tick === 0 ? ' stroke="#CBD5E1"' : ""}/>`);
|
||||
svg.push(`<text class="text axis-label" x="${plot.left - 18}" y="${tickY + 6}" text-anchor="end">${escapeXml(formatNumber(value))}</text>`);
|
||||
}
|
||||
|
||||
rows.forEach((row, index) => {
|
||||
const isLatest = index === latestIndex;
|
||||
const isPeak = index === peakIndex && !isLatest;
|
||||
const fill = isLatest ? "#0F172A" : isPeak ? "#8B5CF6" : null;
|
||||
const extra = fill ? ` fill="${fill}"${isPeak ? ' fill-opacity="0.9"' : ""}` : ' class="bar"';
|
||||
svg.push(
|
||||
`<rect${extra} x="${(x(index) - barWidth / 2).toFixed(1)}" y="${y(row.count).toFixed(1)}" width="${barWidth.toFixed(1)}" height="${barHeight(row.count).toFixed(1)}" rx="${Math.min(4, barWidth / 2).toFixed(1)}"/>`
|
||||
);
|
||||
});
|
||||
|
||||
if (trendPoints.length > 0) {
|
||||
svg.push(`<polyline points="${trendPoints.join(" ")}" fill="none" stroke="#F59E0B" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>`);
|
||||
const lastTrendPoint = trendPoints.at(-1).split(",");
|
||||
svg.push(`<circle cx="${lastTrendPoint[0]}" cy="${lastTrendPoint[1]}" r="6" fill="#F59E0B" stroke="#FFFFFF" stroke-width="3"/>`);
|
||||
}
|
||||
|
||||
const renderValueLabel = (index, value, color, stroke) => {
|
||||
const centerX = x(index);
|
||||
const labelY = Math.max(plot.top - 22, y(value) - 45);
|
||||
svg.push(`<rect x="${(centerX - 27).toFixed(1)}" y="${labelY.toFixed(1)}" width="54" height="30" rx="10" fill="#FFFFFF" stroke="${stroke}"/>`);
|
||||
svg.push(
|
||||
`<text class="text" x="${centerX.toFixed(1)}" y="${(labelY + 20).toFixed(1)}" fill="${color}" font-size="14" font-weight="750" text-anchor="middle">${escapeXml(formatNumber(value))}</text>`
|
||||
);
|
||||
};
|
||||
if (peakIndex !== latestIndex && Math.abs(x(peakIndex) - x(latestIndex)) >= 60) {
|
||||
renderValueLabel(peakIndex, peak.count, "#7C3AED", "#DDD6FE");
|
||||
}
|
||||
renderValueLabel(latestIndex, latest.count, "#0F172A", "#CBD5E1");
|
||||
|
||||
const tickCount = Math.min(7, rows.length);
|
||||
const tickIndexes = new Set();
|
||||
for (let tick = 0; tick < tickCount; tick += 1) {
|
||||
const index = tick === tickCount - 1 ? rows.length - 1 : Math.round((tick * rows.length) / Math.max(1, tickCount - 1));
|
||||
tickIndexes.add(Math.min(rows.length - 1, index));
|
||||
}
|
||||
for (const index of tickIndexes) {
|
||||
const tickX = x(index);
|
||||
svg.push(`<line x1="${tickX.toFixed(1)}" y1="${plot.bottom}" x2="${tickX.toFixed(1)}" y2="${plot.bottom + 8}" stroke="#94A3B8"/>`);
|
||||
svg.push(
|
||||
`<text class="text axis-label" x="${tickX.toFixed(1)}" y="${plot.bottom + 34}" text-anchor="middle">${formatDisplayDate(rows[index].date)}</text>`
|
||||
);
|
||||
}
|
||||
|
||||
svg.push(`<rect x="126" y="814" width="16" height="16" rx="4" fill="#0F766E" fill-opacity="0.82"/>`);
|
||||
svg.push(`<text class="text" x="151" y="828" fill="#475569" font-size="15">Commits pro Tag</text>`);
|
||||
svg.push(`<line x1="322" y1="822" x2="350" y2="822" stroke="#F59E0B" stroke-width="4" stroke-linecap="round"/>`);
|
||||
svg.push(`<text class="text" x="362" y="828" fill="#475569" font-size="15">7-Tage-Durchschnitt</text>`);
|
||||
svg.push(
|
||||
`<text class="text" x="1518" y="828" text-anchor="end" fill="#94A3B8" font-size="13">Alle von main erreichbaren Commits · Merge-Commits und Branch-Commits · Tage ohne Commits = 0</text>`
|
||||
);
|
||||
svg.push(`</svg>`);
|
||||
return `${svg.join("\n")}\n`;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const outputDir = path.resolve(REPO_ROOT, options.outputDir);
|
||||
const history = collectCommitHistory(options.ref, { days: options.days, repoRoot: REPO_ROOT });
|
||||
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(outputDir, "commit-history.csv"), renderCsv(history, options.label));
|
||||
fs.writeFileSync(path.join(outputDir, "commit-history.svg"), renderSvg(history, options.label));
|
||||
|
||||
const latest = history.rows.at(-1);
|
||||
const total = history.rows.reduce((sum, row) => sum + row.count, 0);
|
||||
console.log(`Commit history: ${history.days} calendar days from ${history.startDate} to ${history.endDate}`);
|
||||
console.log(`Latest day: ${formatNumber(latest.count)} commits; window total: ${formatNumber(total)}`);
|
||||
console.log(`Wrote ${path.relative(REPO_ROOT, outputDir)}/commit-history.{csv,svg}`);
|
||||
}
|
||||
|
||||
const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
||||
if (invokedDirectly) {
|
||||
try {
|
||||
main();
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { collectCommitHistory, COMMIT_HISTORY_DAYS, COMMIT_HISTORY_TIME_ZONE, parseArgs, renderCsv, renderSvg, toBerlinDateKey } from "./commit-history.mjs";
|
||||
|
||||
const TIP = "dddddddddddddddddddddddddddddddddddddddd";
|
||||
|
||||
function fixtureHistory() {
|
||||
const rows = Array.from({ length: COMMIT_HISTORY_DAYS }, (_, index) => ({
|
||||
count: index === 57 ? 151 : index === 59 ? 75 : index % 9,
|
||||
date: new Date(Date.UTC(2026, 5, 3 + index, 12)).toISOString().slice(0, 10),
|
||||
}));
|
||||
return {
|
||||
days: COMMIT_HISTORY_DAYS,
|
||||
endDate: "2026-08-01",
|
||||
resolvedRef: TIP,
|
||||
rows,
|
||||
startDate: "2026-06-03",
|
||||
tipTimestamp: "2026-08-01T14:17:15+02:00",
|
||||
};
|
||||
}
|
||||
|
||||
describe("commit-history", () => {
|
||||
it("defaults to a 60-day Berlin calendar window", () => {
|
||||
expect(parseArgs([])).toEqual({
|
||||
days: 60,
|
||||
label: "origin/main",
|
||||
outputDir: "docs/metrics",
|
||||
ref: "origin/main",
|
||||
});
|
||||
expect(COMMIT_HISTORY_TIME_ZONE).toBe("Europe/Berlin");
|
||||
expect(() => parseArgs(["--days", "0"])).toThrow("--days must be an integer between 1 and 365");
|
||||
});
|
||||
|
||||
it("groups offsets by their Berlin calendar date", () => {
|
||||
expect(toBerlinDateKey("2026-07-31T23:30:00Z")).toBe("2026-08-01");
|
||||
expect(toBerlinDateKey("2026-10-25T00:30:00Z")).toBe("2026-10-25");
|
||||
});
|
||||
|
||||
it("counts every reachable commit and preserves zero-commit days", () => {
|
||||
const calls = [];
|
||||
const runGit = vi.fn((args) => {
|
||||
calls.push(args);
|
||||
if (args[0] === "rev-parse") return `${TIP}\n`;
|
||||
if (args[0] === "show") return "2026-08-01T14:17:15+02:00\n";
|
||||
if (args[0] === "log") {
|
||||
return [
|
||||
`${TIP}\t2026-08-01T14:17:15+02:00`,
|
||||
`cccccccccccccccccccccccccccccccccccccccc\t2026-07-31T23:30:00Z`,
|
||||
`bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\t2026-07-30T18:00:00+02:00`,
|
||||
`aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\t2026-07-01T10:00:00+02:00`,
|
||||
].join("\n");
|
||||
}
|
||||
throw new Error(`Unexpected Git call: ${args.join(" ")}`);
|
||||
});
|
||||
|
||||
const history = collectCommitHistory("HEAD", { days: 3, runGit });
|
||||
|
||||
expect(history.rows).toEqual([
|
||||
{ count: 1, date: "2026-07-30" },
|
||||
{ count: 0, date: "2026-07-31" },
|
||||
{ count: 2, date: "2026-08-01" },
|
||||
]);
|
||||
const logCall = calls.find((args) => args[0] === "log");
|
||||
expect(logCall).not.toContain("--first-parent");
|
||||
expect(logCall).toContain(TIP);
|
||||
});
|
||||
|
||||
it("renders deterministic CSV metadata and the approved SVG metrics", () => {
|
||||
const history = fixtureHistory();
|
||||
const csv = renderCsv(history, "origin/main");
|
||||
const svg = renderSvg(history, "origin/main");
|
||||
|
||||
expect(renderCsv(history, "origin/main")).toBe(csv);
|
||||
expect(renderSvg(history, "origin/main")).toBe(svg);
|
||||
expect(csv).toContain("# window_days=60");
|
||||
expect(csv).toContain("# timezone=Europe/Berlin");
|
||||
expect(csv).toContain("2026-08-01,75");
|
||||
expect(svg).toContain("HiveGrid · Commit-Aktivität");
|
||||
expect(svg).toContain("Ø PRO TAG · 60 TAGE");
|
||||
expect(svg).toContain("PEAK · 30.07.");
|
||||
expect(svg).toContain(">151 Commits<");
|
||||
expect(svg).toContain(">75 Commits<");
|
||||
expect(svg).toContain("7-Tage-Durchschnitt");
|
||||
});
|
||||
});
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
const WORKFLOW_PATH = path.join(REPO_ROOT, ".gitea/workflows/loc-history.yml");
|
||||
const README_PATH = path.join(REPO_ROOT, "README.md");
|
||||
|
||||
function read(filePath) {
|
||||
return fs.readFileSync(filePath, "utf8");
|
||||
}
|
||||
|
||||
describe("repository metrics publication contract", () => {
|
||||
it("publishes only full first-parent main history with pinned actions", () => {
|
||||
const workflow = read(WORKFLOW_PATH);
|
||||
|
||||
expect(workflow).toMatch(/push:\n\s+branches:\n\s+- main/);
|
||||
expect(workflow).toContain("workflow_dispatch:");
|
||||
expect(workflow).toContain("actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5");
|
||||
expect(workflow).toContain("fetch-depth: 0");
|
||||
expect(workflow).toContain("persist-credentials: false");
|
||||
expect(workflow).toContain("actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020");
|
||||
expect(workflow).toContain("bash scripts/ci/persist-node-toolchain.sh");
|
||||
expect(workflow).toContain('if [ "$SOURCE_REF" != "refs/heads/main" ]');
|
||||
expect(workflow).toContain("--ref HEAD");
|
||||
expect(workflow).toContain("--label origin/main");
|
||||
expect(workflow).toContain("node scripts/metrics/commit-history.mjs");
|
||||
expect(workflow).toContain("--days 60");
|
||||
});
|
||||
|
||||
it("fails closed unless the built-in Actions token is repository-scoped and non-admin", () => {
|
||||
const workflow = read(WORKFLOW_PATH);
|
||||
|
||||
expect(workflow).toContain("LOC_METRICS_GITEA_TOKEN");
|
||||
// Releases sit in the repository scope: `code: write` alone returned 403.
|
||||
expect(workflow).toMatch(/^permissions:\n {2}contents: write$/m);
|
||||
expect(workflow).not.toMatch(/^ {2}code: write$/m);
|
||||
expect(workflow).toContain("The Actions token cannot read releases");
|
||||
expect(workflow.match(/LOC_METRICS_GITEA_TOKEN: \$\{\{ gitea\.token \}\}/g)).toHaveLength(2);
|
||||
expect(workflow).toContain("/api/v1/repos/toolctrl-labs/HiveGrid-Cloud");
|
||||
expect(workflow).toContain('repository.full_name !== "toolctrl-labs/HiveGrid-Cloud"');
|
||||
expect(workflow).toContain("repository.permissions?.push !== true");
|
||||
expect(workflow).toContain("repository.permissions?.admin === true");
|
||||
expect(workflow).toContain("AbortSignal.timeout(15_000)");
|
||||
expect(workflow).not.toContain("LOC_METRICS_BOT_LOGIN");
|
||||
expect(workflow).not.toContain("secrets.LOC_METRICS_GITEA_TOKEN");
|
||||
});
|
||||
|
||||
it("publishes all four artefacts as release assets instead of a long-lived branch", () => {
|
||||
const workflow = read(WORKFLOW_PATH);
|
||||
|
||||
expect(workflow).toContain('const TAG = "loc-metrics";');
|
||||
expect(workflow).toContain('const FILES = ["commit-history.csv", "commit-history.svg", "loc-history.csv", "loc-history.svg"];');
|
||||
expect(workflow).toContain("/releases/tags/${TAG}");
|
||||
expect(workflow).toContain("target_commitish: sourceSha");
|
||||
expect(workflow).toContain("/releases/${release.id}/assets");
|
||||
expect(workflow).toContain('method: "DELETE"');
|
||||
expect(workflow).toContain("AbortSignal.timeout(30_000)");
|
||||
expect(workflow).toContain("draft: false");
|
||||
});
|
||||
|
||||
it("keeps the repository history free of metrics commits", () => {
|
||||
const workflow = read(WORKFLOW_PATH);
|
||||
|
||||
// The branch mechanism is gone for good: no checkout, no commit, no push.
|
||||
expect(workflow).not.toContain("refs/heads/loc-metrics");
|
||||
expect(workflow).not.toContain("checkout --orphan");
|
||||
expect(workflow).not.toContain("git commit");
|
||||
expect(workflow).not.toMatch(/git[^\n]*push/);
|
||||
expect(workflow).not.toContain("GIT_ASKPASS");
|
||||
expect(workflow).not.toMatch(/https?:\/\/[^\n]*LOC_METRICS_GITEA_TOKEN/);
|
||||
});
|
||||
|
||||
it("embeds both live SVGs and links their CSVs plus the local generators", () => {
|
||||
const readme = read(README_PATH);
|
||||
const assetBase = "https://git.tool-ctrl.de/toolctrl-labs/HiveGrid-Cloud/releases/download/loc-metrics";
|
||||
|
||||
expect(readme).toContain(``);
|
||||
expect(readme).toContain(`${assetBase}/loc-history.csv`);
|
||||
expect(readme).toContain("scripts/metrics/loc-history.mjs");
|
||||
expect(readme).toContain("Fork-/Code-Import vom 04.06.2026");
|
||||
expect(readme).toContain(``);
|
||||
expect(readme).toContain(`${assetBase}/commit-history.csv`);
|
||||
expect(readme).toContain("scripts/metrics/commit-history.mjs");
|
||||
expect(readme).toContain("letzten 60 Kalendertage");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,552 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
|
||||
const REPO_ROOT = path.resolve(SCRIPT_DIR, "../..");
|
||||
|
||||
export const CODE_IMPORT_BASELINE = "1631a5d84e079f71ef377a7ca4f59bfad4d770b4";
|
||||
|
||||
const CODE_EXTENSIONS = new Set([
|
||||
".astro",
|
||||
".bash",
|
||||
".cjs",
|
||||
".css",
|
||||
".cts",
|
||||
".dbml",
|
||||
".gql",
|
||||
".graphql",
|
||||
".hcl",
|
||||
".htm",
|
||||
".html",
|
||||
".java",
|
||||
".js",
|
||||
".jsx",
|
||||
".kt",
|
||||
".kts",
|
||||
".less",
|
||||
".mjs",
|
||||
".mts",
|
||||
".prisma",
|
||||
".py",
|
||||
".rb",
|
||||
".rs",
|
||||
".sass",
|
||||
".scss",
|
||||
".sh",
|
||||
".sql",
|
||||
".svelte",
|
||||
".tf",
|
||||
".ts",
|
||||
".tsx",
|
||||
".vue",
|
||||
".zsh",
|
||||
]);
|
||||
|
||||
const CODE_FILENAMES = new Set(["Dockerfile", "Makefile"]);
|
||||
|
||||
const ROOT_EXCLUDED_SEGMENTS = new Set([".planning", "build", "data", "docs", "guides"]);
|
||||
|
||||
const EXCLUDED_SEGMENTS = new Set([
|
||||
".cache",
|
||||
".next",
|
||||
".react-router",
|
||||
".tc",
|
||||
".turbo",
|
||||
"__generated__",
|
||||
"coverage",
|
||||
"dist",
|
||||
"generated",
|
||||
"node_modules",
|
||||
"playwright-report",
|
||||
"test-results",
|
||||
"vendor",
|
||||
]);
|
||||
|
||||
const TEST_SEGMENTS = new Set(["__fixtures__", "__tests__", "e2e", "fixtures", "playwright", "test", "test-setup", "test-utils", "tests", "visual-baseline"]);
|
||||
|
||||
const EXACT_EXCLUSIONS = new Set([
|
||||
"apps/web/app/components/ui/icons/vendor.tsx",
|
||||
"packages/api-client-ts/openapi.json",
|
||||
"prisma/rls/tenant-policy-contract-isolation.sql",
|
||||
]);
|
||||
|
||||
const DEFAULTS = Object.freeze({
|
||||
baseline: CODE_IMPORT_BASELINE,
|
||||
label: null,
|
||||
outputDir: "docs/metrics",
|
||||
ref: "origin/main",
|
||||
});
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage: node scripts/metrics/loc-history.mjs [options]
|
||||
|
||||
Options:
|
||||
--ref <git-ref> Mainline to inspect (default: ${DEFAULTS.ref})
|
||||
--label <text> Ref label rendered in outputs (default: value of --ref)
|
||||
--baseline <commit> Fork/code-import baseline (default: ${DEFAULTS.baseline})
|
||||
--output-dir <path> Output directory (default: ${DEFAULTS.outputDir})
|
||||
--help Show this help
|
||||
|
||||
Outputs:
|
||||
loc-history.csv Mainline snapshots and LOC values
|
||||
loc-history.svg Standalone chart
|
||||
|
||||
LOC means non-empty physical lines in source-like files. Documentation, lockfiles,
|
||||
configuration/data formats, dependencies, build output, known generated source
|
||||
paths, and binary assets are excluded. Tests and database migrations are
|
||||
reported separately; committed migrations (including generated migration SQL)
|
||||
and packages/ui-subframe runtime source remain included.`);
|
||||
}
|
||||
|
||||
export function parseArgs(argv) {
|
||||
const options = { ...DEFAULTS };
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const argument = argv[index];
|
||||
if (argument === "--help") {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
if (argument === "--ref" || argument === "--label" || argument === "--baseline" || argument === "--output-dir") {
|
||||
const value = argv[index + 1];
|
||||
if (!value || value.startsWith("--")) {
|
||||
throw new Error(`Missing value for ${argument}`);
|
||||
}
|
||||
if (argument === "--ref") options.ref = value;
|
||||
if (argument === "--label") options.label = value;
|
||||
if (argument === "--baseline") options.baseline = value;
|
||||
if (argument === "--output-dir") options.outputDir = value;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
throw new Error(`Unknown argument: ${argument}`);
|
||||
}
|
||||
|
||||
return { ...options, label: options.label ?? options.ref };
|
||||
}
|
||||
|
||||
function git(args, options = {}) {
|
||||
return execFileSync("git", args, {
|
||||
cwd: options.repoRoot ?? REPO_ROOT,
|
||||
encoding: options.encoding === "buffer" ? undefined : (options.encoding ?? "utf8"),
|
||||
input: options.input,
|
||||
maxBuffer: 512 * 1024 * 1024,
|
||||
stdio: options.stdio,
|
||||
});
|
||||
}
|
||||
|
||||
export function collectSnapshots(ref, { baseline = CODE_IMPORT_BASELINE, repoRoot = REPO_ROOT, runGit = git } = {}) {
|
||||
const resolvedRef = runGit(["rev-parse", "--verify", `${ref}^{commit}`], { repoRoot }).trim();
|
||||
const resolvedBaseline = runGit(["rev-parse", "--verify", `${baseline}^{commit}`], { repoRoot }).trim();
|
||||
try {
|
||||
runGit(["merge-base", "--is-ancestor", resolvedBaseline, resolvedRef], { repoRoot, stdio: "ignore" });
|
||||
} catch {
|
||||
throw new Error(`LOC baseline ${resolvedBaseline} is not an ancestor of ${ref}`);
|
||||
}
|
||||
|
||||
const log = runGit(["log", "--first-parent", "--reverse", "--date=short", "--format=%H%x09%P%x09%cs%x09%cI%x09%s", resolvedRef], { repoRoot }).trim();
|
||||
|
||||
if (!log) throw new Error(`No commits found for ${ref}`);
|
||||
|
||||
const commits = log.split("\n").map((line, order) => {
|
||||
const [sha, rawParents, date, timestamp, ...subjectParts] = line.split("\t");
|
||||
return {
|
||||
sha,
|
||||
parents: rawParents ? rawParents.split(" ") : [],
|
||||
date,
|
||||
order,
|
||||
timestamp,
|
||||
subject: subjectParts.join("\t"),
|
||||
};
|
||||
});
|
||||
|
||||
const baselineCommit = commits.find((commit) => commit.sha === resolvedBaseline);
|
||||
if (!baselineCommit) {
|
||||
throw new Error(`LOC baseline ${resolvedBaseline} is not on the first-parent history of ${ref}`);
|
||||
}
|
||||
const baselineIndex = commits.indexOf(baselineCommit);
|
||||
const lastCommitPerDay = new Map();
|
||||
for (const commit of commits.slice(baselineIndex)) lastCommitPerDay.set(commit.date, commit);
|
||||
const anchorBySha = new Map([[baselineCommit.sha, "code-import"]]);
|
||||
|
||||
const snapshots = [baselineCommit, ...lastCommitPerDay.values()];
|
||||
const uniqueSnapshots = new Map(snapshots.map((snapshot) => [snapshot.sha, { ...snapshot, anchor: anchorBySha.get(snapshot.sha) ?? "" }]));
|
||||
|
||||
return [...uniqueSnapshots.values()].sort((left, right) => left.order - right.order);
|
||||
}
|
||||
|
||||
export function classifyPath(filePath) {
|
||||
const normalizedPath = filePath.replaceAll("\\", "/");
|
||||
const segments = normalizedPath.split("/");
|
||||
const filename = segments.at(-1) ?? "";
|
||||
const extension = path.posix.extname(filename).toLowerCase();
|
||||
|
||||
if (EXACT_EXCLUSIONS.has(normalizedPath)) return null;
|
||||
if (ROOT_EXCLUDED_SEGMENTS.has(segments[0])) return null;
|
||||
if (segments.some((segment) => EXCLUDED_SEGMENTS.has(segment))) return null;
|
||||
if (/\.generated\.[^.]+$/i.test(filename)) return null;
|
||||
if (/\.min\.(?:css|js)$/i.test(filename) || filename.endsWith(".map")) return null;
|
||||
|
||||
const isCodeFile = CODE_EXTENSIONS.has(extension) || CODE_FILENAMES.has(filename) || filename.toLowerCase().endsWith(".dockerfile");
|
||||
if (!isCodeFile) return null;
|
||||
|
||||
if (normalizedPath.startsWith("prisma/migrations/") && extension === ".sql") {
|
||||
return "migration";
|
||||
}
|
||||
|
||||
const isTest =
|
||||
segments.some((segment) => TEST_SEGMENTS.has(segment.toLowerCase())) ||
|
||||
/\.(?:test|spec|stories|visual-fixture)\.[^.]+$/i.test(filename) ||
|
||||
/^__test(?:[._-]|$)/i.test(filename) ||
|
||||
/^(?:playwright(?:-[^.]+)?\.config|test-(?:setup|utils?))\.[^.]+$/i.test(filename);
|
||||
|
||||
return isTest ? "test" : "product";
|
||||
}
|
||||
|
||||
function listSnapshotFiles(snapshot, repoRoot) {
|
||||
const tree = git(["ls-tree", "-r", "-z", "--full-tree", snapshot.sha], {
|
||||
encoding: "buffer",
|
||||
repoRoot,
|
||||
});
|
||||
const entries = tree.toString("utf8").split("\0").filter(Boolean);
|
||||
const files = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
const tabIndex = entry.indexOf("\t");
|
||||
if (tabIndex === -1) continue;
|
||||
const metadata = entry.slice(0, tabIndex).split(" ");
|
||||
const filePath = entry.slice(tabIndex + 1);
|
||||
const [, objectType, blob] = metadata;
|
||||
if (objectType !== "blob") continue;
|
||||
|
||||
const category = classifyPath(filePath);
|
||||
if (category) files.push({ blob, category });
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
function findByte(buffer, byte, fromIndex) {
|
||||
for (let index = fromIndex; index < buffer.length; index += 1) {
|
||||
if (buffer[index] === byte) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export function countNonEmptyLines(buffer) {
|
||||
if (buffer.includes(0)) return 0;
|
||||
const content = buffer.toString("utf8");
|
||||
if (!content) return 0;
|
||||
|
||||
let count = 0;
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
if (/\S/.test(line)) count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
function countBlobs(blobIds, repoRoot) {
|
||||
const counts = new Map();
|
||||
const uniqueBlobIds = [...new Set(blobIds)].sort();
|
||||
const batchSize = 250;
|
||||
|
||||
for (let start = 0; start < uniqueBlobIds.length; start += batchSize) {
|
||||
const batch = uniqueBlobIds.slice(start, start + batchSize);
|
||||
const output = git(["cat-file", "--batch"], {
|
||||
encoding: "buffer",
|
||||
input: `${batch.join("\n")}\n`,
|
||||
repoRoot,
|
||||
});
|
||||
let offset = 0;
|
||||
|
||||
for (const requestedBlob of batch) {
|
||||
const headerEnd = findByte(output, 10, offset);
|
||||
if (headerEnd === -1) throw new Error(`Malformed cat-file response for ${requestedBlob}`);
|
||||
|
||||
const header = output.subarray(offset, headerEnd).toString("utf8");
|
||||
const [resolvedBlob, objectType, rawSize] = header.split(" ");
|
||||
if (objectType !== "blob") throw new Error(`Expected blob for ${requestedBlob}, got: ${header}`);
|
||||
|
||||
const size = Number.parseInt(rawSize, 10);
|
||||
const contentStart = headerEnd + 1;
|
||||
const contentEnd = contentStart + size;
|
||||
counts.set(resolvedBlob, countNonEmptyLines(output.subarray(contentStart, contentEnd)));
|
||||
offset = contentEnd + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
function tallySnapshots(snapshots, repoRoot) {
|
||||
const filesBySnapshot = new Map();
|
||||
const allBlobs = [];
|
||||
|
||||
for (const snapshot of snapshots) {
|
||||
const files = listSnapshotFiles(snapshot, repoRoot);
|
||||
filesBySnapshot.set(snapshot.sha, files);
|
||||
for (const file of files) allBlobs.push(file.blob);
|
||||
}
|
||||
|
||||
const lineCounts = countBlobs(allBlobs, repoRoot);
|
||||
|
||||
return snapshots.map((snapshot) => {
|
||||
const loc = { product: 0, test: 0, migration: 0 };
|
||||
const files = { product: 0, test: 0, migration: 0 };
|
||||
|
||||
for (const file of filesBySnapshot.get(snapshot.sha) ?? []) {
|
||||
loc[file.category] += lineCounts.get(file.blob) ?? 0;
|
||||
files[file.category] += 1;
|
||||
}
|
||||
|
||||
return {
|
||||
...snapshot,
|
||||
loc,
|
||||
files,
|
||||
total: loc.product + loc.test + loc.migration,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function csvCell(value) {
|
||||
const stringValue = String(value);
|
||||
return /[",\n]/.test(stringValue) ? `"${stringValue.replaceAll('"', '""')}"` : stringValue;
|
||||
}
|
||||
|
||||
export function renderCsv(rows, ref) {
|
||||
const metadata = [
|
||||
`# ref=${ref}`,
|
||||
`# resolved_tip=${rows.at(-1).sha}`,
|
||||
"# metric=non-empty physical source lines",
|
||||
"# sampling=fork/code-import baseline and last first-parent commit on each active calendar day",
|
||||
"# exclusions=docs, planning, configuration/data formats, dependencies, build output, known generated source paths, binary assets, lockfiles",
|
||||
"# included=packages/ui-subframe runtime source; committed migration SQL (reported separately)",
|
||||
];
|
||||
const header = [
|
||||
"date",
|
||||
"timestamp",
|
||||
"commit",
|
||||
"anchor",
|
||||
"subject",
|
||||
"non_test_loc",
|
||||
"test_loc",
|
||||
"migration_loc",
|
||||
"total_loc",
|
||||
"non_test_files",
|
||||
"test_files",
|
||||
"migration_files",
|
||||
];
|
||||
const body = rows.map((row) =>
|
||||
[
|
||||
row.date,
|
||||
row.timestamp,
|
||||
row.sha,
|
||||
row.anchor,
|
||||
row.subject,
|
||||
row.loc.product,
|
||||
row.loc.test,
|
||||
row.loc.migration,
|
||||
row.total,
|
||||
row.files.product,
|
||||
row.files.test,
|
||||
row.files.migration,
|
||||
]
|
||||
.map(csvCell)
|
||||
.join(",")
|
||||
);
|
||||
return `${[...metadata, header.join(","), ...body].join("\n")}\n`;
|
||||
}
|
||||
|
||||
function escapeXml(value) {
|
||||
return String(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'");
|
||||
}
|
||||
|
||||
function formatNumber(value) {
|
||||
return new Intl.NumberFormat("de-DE").format(Math.round(value));
|
||||
}
|
||||
|
||||
function formatCompact(value) {
|
||||
return new Intl.NumberFormat("de-DE", {
|
||||
maximumFractionDigits: value >= 100_000 ? 0 : 1,
|
||||
notation: "compact",
|
||||
}).format(value);
|
||||
}
|
||||
|
||||
function formatDate(timestamp, options = {}) {
|
||||
return new Intl.DateTimeFormat("de-DE", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
timeZone: "Europe/Berlin",
|
||||
...options,
|
||||
}).format(new Date(timestamp));
|
||||
}
|
||||
|
||||
function niceAxisMaximum(value) {
|
||||
if (value <= 0) return 1;
|
||||
const roughStep = value / 5;
|
||||
const magnitude = 10 ** Math.floor(Math.log10(roughStep));
|
||||
const normalized = roughStep / magnitude;
|
||||
const niceNormalized = normalized <= 1 ? 1 : normalized <= 2 ? 2 : normalized <= 2.5 ? 2.5 : normalized <= 5 ? 5 : 10;
|
||||
const step = niceNormalized * magnitude;
|
||||
return Math.ceil(value / step) * step;
|
||||
}
|
||||
|
||||
export function renderSvg(rows, ref) {
|
||||
const width = 1600;
|
||||
const height = 900;
|
||||
const plot = { left: 126, top: 282, right: 1518, bottom: 774 };
|
||||
const plotWidth = plot.right - plot.left;
|
||||
const plotHeight = plot.bottom - plot.top;
|
||||
const minTime = Math.min(...rows.map((row) => Date.parse(row.timestamp)));
|
||||
const maxTime = Math.max(...rows.map((row) => Date.parse(row.timestamp)));
|
||||
const timeRange = Math.max(1, maxTime - minTime);
|
||||
const maxLoc = niceAxisMaximum(Math.max(...rows.map((row) => row.total)) * 1.04);
|
||||
const x = (row) => plot.left + ((Date.parse(row.timestamp) - minTime) / timeRange) * plotWidth;
|
||||
const y = (value) => plot.bottom - (value / maxLoc) * plotHeight;
|
||||
const point = (xValue, yValue) => `${xValue.toFixed(1)},${yValue.toFixed(1)}`;
|
||||
const stackedValue = (row, category) => {
|
||||
if (category === "product") return row.loc.product;
|
||||
if (category === "test") return row.loc.product + row.loc.test;
|
||||
return row.total;
|
||||
};
|
||||
const lowerValue = (row, category) => {
|
||||
if (category === "product") return 0;
|
||||
if (category === "test") return row.loc.product;
|
||||
return row.loc.product + row.loc.test;
|
||||
};
|
||||
const areaPath = (category) => {
|
||||
const upper = rows.map((row) => point(x(row), y(stackedValue(row, category))));
|
||||
const lower = [...rows].reverse().map((row) => point(x(row), y(lowerValue(row, category))));
|
||||
return `M ${upper.join(" L ")} L ${lower.join(" L ")} Z`;
|
||||
};
|
||||
const totalPath = rows.map((row) => point(x(row), y(row.total))).join(" L ");
|
||||
const first = rows[0];
|
||||
const latest = rows.at(-1);
|
||||
const colors = {
|
||||
product: "#0F766E",
|
||||
test: "#F59E0B",
|
||||
migration: "#8B5CF6",
|
||||
line: "#0F172A",
|
||||
};
|
||||
const yTicks = 5;
|
||||
const xTicks = 7;
|
||||
const growthBaseline = rows.find((row) => row.anchor === "code-import") ?? first;
|
||||
const growth = latest.total - growthBaseline.total;
|
||||
const growthPercent = growthBaseline.total === 0 ? null : (growth / growthBaseline.total) * 100;
|
||||
const cards = [
|
||||
{ label: "Gesamt", value: formatNumber(latest.total), color: colors.line },
|
||||
{ label: "App + Tooling", value: formatNumber(latest.loc.product), color: colors.product },
|
||||
{ label: "Tests", value: formatNumber(latest.loc.test), color: colors.test },
|
||||
{
|
||||
label: "Seit Code-Import",
|
||||
value: `${growth >= 0 ? "+" : ""}${formatCompact(growth)}${growthPercent === null ? "" : ` · ${growthPercent >= 0 ? "+" : ""}${growthPercent.toFixed(0)} %`}`,
|
||||
color: growth >= 0 ? colors.product : "#DC2626",
|
||||
},
|
||||
];
|
||||
|
||||
const svg = [];
|
||||
svg.push(`<?xml version="1.0" encoding="UTF-8"?>`);
|
||||
svg.push(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-labelledby="title description">`
|
||||
);
|
||||
svg.push(`<title id="title">HiveGrid LOC-Verlauf auf ${escapeXml(ref)}</title>`);
|
||||
svg.push(
|
||||
`<desc id="description">Mainline-Snapshots mit Nicht-Test-Code, Tests und Datenbankmigrationen. Dokumentation, Konfigurations- und Datendateien, Abhängigkeiten, Build-Ausgaben sowie bekannte generierte Quellpfade sind ausgeschlossen. Committete Migrationen werden einschließlich generierter Migrationen separat gezählt.</desc>`
|
||||
);
|
||||
svg.push(`<rect width="${width}" height="${height}" fill="#F8FAFC"/>`);
|
||||
svg.push(
|
||||
`<text x="78" y="74" fill="#0F172A" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="36" font-weight="750">HiveGrid · LOC-Verlauf</text>`
|
||||
);
|
||||
svg.push(
|
||||
`<text x="78" y="110" fill="#64748B" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="18">${escapeXml(ref)} @ ${latest.sha.slice(0, 9)} · letzter Mainline-Commit je aktivem Tag · nicht-leere Quellcodezeilen</text>`
|
||||
);
|
||||
|
||||
cards.forEach((card, index) => {
|
||||
const cardX = 78 + index * 366;
|
||||
svg.push(`<rect x="${cardX}" y="142" width="340" height="92" rx="14" fill="#FFFFFF" stroke="#E2E8F0"/>`);
|
||||
svg.push(`<rect x="${cardX}" y="142" width="6" height="92" rx="3" fill="${card.color}"/>`);
|
||||
svg.push(
|
||||
`<text x="${cardX + 26}" y="174" fill="#64748B" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="14" font-weight="650" letter-spacing="0.5">${escapeXml(card.label.toUpperCase())}</text>`
|
||||
);
|
||||
svg.push(
|
||||
`<text x="${cardX + 26}" y="211" fill="#0F172A" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="29" font-weight="750">${escapeXml(card.value)}</text>`
|
||||
);
|
||||
});
|
||||
|
||||
for (let tick = 0; tick <= yTicks; tick += 1) {
|
||||
const value = (maxLoc / yTicks) * tick;
|
||||
const tickY = y(value);
|
||||
svg.push(`<line x1="${plot.left}" y1="${tickY}" x2="${plot.right}" y2="${tickY}" stroke="${tick === 0 ? "#CBD5E1" : "#E2E8F0"}" stroke-width="1"/>`);
|
||||
svg.push(
|
||||
`<text x="${plot.left - 18}" y="${tickY + 6}" text-anchor="end" fill="#64748B" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="15">${escapeXml(formatCompact(value))}</text>`
|
||||
);
|
||||
}
|
||||
|
||||
for (let tick = 0; tick < xTicks; tick += 1) {
|
||||
const ratio = tick / (xTicks - 1);
|
||||
const timestamp = minTime + ratio * timeRange;
|
||||
const tickX = plot.left + ratio * plotWidth;
|
||||
svg.push(`<line x1="${tickX}" y1="${plot.bottom}" x2="${tickX}" y2="${plot.bottom + 8}" stroke="#94A3B8"/>`);
|
||||
svg.push(
|
||||
`<text x="${tickX}" y="${plot.bottom + 34}" text-anchor="middle" fill="#64748B" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="15">${escapeXml(formatDate(timestamp))}</text>`
|
||||
);
|
||||
}
|
||||
|
||||
svg.push(`<path d="${areaPath("product")}" fill="${colors.product}" fill-opacity="0.86"/>`);
|
||||
svg.push(`<path d="${areaPath("test")}" fill="${colors.test}" fill-opacity="0.82"/>`);
|
||||
svg.push(`<path d="${areaPath("migration")}" fill="${colors.migration}" fill-opacity="0.78"/>`);
|
||||
svg.push(`<path d="M ${totalPath}" fill="none" stroke="${colors.line}" stroke-width="3.5" stroke-linejoin="round" stroke-linecap="round"/>`);
|
||||
|
||||
const legend = [
|
||||
["App + Tooling", colors.product],
|
||||
["Tests", colors.test],
|
||||
["DB-Migrationen", colors.migration],
|
||||
["Gesamt", colors.line],
|
||||
];
|
||||
legend.forEach(([label, color], index) => {
|
||||
const legendX = plot.left + index * 220;
|
||||
svg.push(`<rect x="${legendX}" y="${plot.bottom + 72}" width="16" height="16" rx="4" fill="${color}"/>`);
|
||||
svg.push(
|
||||
`<text x="${legendX + 25}" y="${plot.bottom + 86}" fill="#475569" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="15">${escapeXml(label)}</text>`
|
||||
);
|
||||
});
|
||||
svg.push(
|
||||
`<text x="${plot.right}" y="${plot.bottom + 86}" text-anchor="end" fill="#94A3B8" font-family="Inter, ui-sans-serif, system-ui, sans-serif" font-size="13">Bekannte Generated-Pfade, Docs, Konfig/Datendateien, Assets und Build-Ausgaben ausgeschlossen</text>`
|
||||
);
|
||||
svg.push(`</svg>`);
|
||||
return `${svg.join("\n")}\n`;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const outputDir = path.resolve(REPO_ROOT, options.outputDir);
|
||||
const snapshots = collectSnapshots(options.ref, { baseline: options.baseline, repoRoot: REPO_ROOT });
|
||||
const rows = tallySnapshots(snapshots, REPO_ROOT);
|
||||
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(outputDir, "loc-history.csv"), renderCsv(rows, options.label));
|
||||
fs.writeFileSync(path.join(outputDir, "loc-history.svg"), renderSvg(rows, options.label));
|
||||
|
||||
const first = rows[0];
|
||||
const latest = rows.at(-1);
|
||||
console.log(`LOC history: ${rows.length} snapshots from ${first.date} to ${latest.date}`);
|
||||
console.log(`Latest ${options.ref} (${latest.sha.slice(0, 9)}): ${formatNumber(latest.total)} LOC`);
|
||||
console.log(` App + tooling: ${formatNumber(latest.loc.product)}`);
|
||||
console.log(` Tests: ${formatNumber(latest.loc.test)}`);
|
||||
console.log(` DB migrations: ${formatNumber(latest.loc.migration)}`);
|
||||
console.log(`Wrote ${path.relative(REPO_ROOT, outputDir)}/loc-history.{csv,svg}`);
|
||||
}
|
||||
|
||||
const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
||||
if (invokedDirectly) {
|
||||
try {
|
||||
main();
|
||||
} catch (error) {
|
||||
console.error(error instanceof Error ? error.message : error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { classifyPath, CODE_IMPORT_BASELINE, collectSnapshots, countNonEmptyLines, parseArgs, renderCsv, renderSvg } from "./loc-history.mjs";
|
||||
|
||||
const PRE_BASELINE = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const SAME_DAY_EARLY = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const SAME_DAY_LATEST = "cccccccccccccccccccccccccccccccccccccccc";
|
||||
const TIP = "dddddddddddddddddddddddddddddddddddddddd";
|
||||
|
||||
function fixtureLog() {
|
||||
return [
|
||||
`${PRE_BASELINE}\t\t2026-06-03\t2026-06-03T09:00:00+02:00\tpre-fork root`,
|
||||
`${CODE_IMPORT_BASELINE}\t${PRE_BASELINE}\t2026-06-04\t2026-06-04T10:00:00+02:00\tfork import`,
|
||||
`${SAME_DAY_EARLY}\t${CODE_IMPORT_BASELINE}\t2026-06-04\t2026-06-04T12:00:00+02:00\tfirst same-day change`,
|
||||
`${SAME_DAY_LATEST}\t${SAME_DAY_EARLY}\t2026-06-04\t2026-06-04T08:00:00+02:00\tlatest same-day state`,
|
||||
`${TIP}\t${SAME_DAY_LATEST}\t2026-06-05\t2026-06-05T09:00:00+02:00\tnext-day state`,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function snapshotRows() {
|
||||
return [
|
||||
{
|
||||
date: "2026-06-04",
|
||||
timestamp: "2026-06-04T10:00:00+02:00",
|
||||
sha: CODE_IMPORT_BASELINE,
|
||||
anchor: "code-import",
|
||||
subject: 'Import, "fork"',
|
||||
loc: { product: 100, test: 20, migration: 5 },
|
||||
files: { product: 10, test: 2, migration: 1 },
|
||||
total: 125,
|
||||
},
|
||||
{
|
||||
date: "2026-06-05",
|
||||
timestamp: "2026-06-05T10:00:00+02:00",
|
||||
sha: TIP,
|
||||
anchor: "",
|
||||
subject: "Add feature",
|
||||
loc: { product: 150, test: 30, migration: 10 },
|
||||
files: { product: 15, test: 3, migration: 2 },
|
||||
total: 190,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
describe("loc-history", () => {
|
||||
it("pins the fork baseline and samples the last first-parent commit per active day", () => {
|
||||
const calls = [];
|
||||
const runGit = vi.fn((args) => {
|
||||
calls.push(args);
|
||||
if (args[0] === "rev-parse" && args[2] === "HEAD^{commit}") return `${TIP}\n`;
|
||||
if (args[0] === "rev-parse" && args[2] === `${CODE_IMPORT_BASELINE}^{commit}`) return `${CODE_IMPORT_BASELINE}\n`;
|
||||
if (args[0] === "merge-base") return "";
|
||||
if (args[0] === "log") return fixtureLog();
|
||||
throw new Error(`Unexpected git call: ${args.join(" ")}`);
|
||||
});
|
||||
|
||||
const snapshots = collectSnapshots("HEAD", { runGit });
|
||||
|
||||
expect(CODE_IMPORT_BASELINE).toBe("1631a5d84e079f71ef377a7ca4f59bfad4d770b4");
|
||||
expect(snapshots.map((snapshot) => snapshot.sha)).toEqual([CODE_IMPORT_BASELINE, SAME_DAY_LATEST, TIP]);
|
||||
expect(snapshots[0]).toEqual(expect.objectContaining({ anchor: "code-import", subject: "fork import" }));
|
||||
const logCall = calls.find((args) => args[0] === "log");
|
||||
expect(logCall).toEqual(expect.arrayContaining(["--first-parent", "--reverse"]));
|
||||
});
|
||||
|
||||
it("fails closed when the pinned baseline is not an ancestor", () => {
|
||||
const runGit = vi.fn((args) => {
|
||||
if (args[0] === "rev-parse" && args[2] === "HEAD^{commit}") return `${TIP}\n`;
|
||||
if (args[0] === "rev-parse") return `${CODE_IMPORT_BASELINE}\n`;
|
||||
if (args[0] === "merge-base") throw new Error("not an ancestor");
|
||||
throw new Error(`Unexpected git call: ${args.join(" ")}`);
|
||||
});
|
||||
|
||||
expect(() => collectSnapshots("HEAD", { runGit })).toThrow(`LOC baseline ${CODE_IMPORT_BASELINE} is not an ancestor of HEAD`);
|
||||
expect(runGit.mock.calls.some(([args]) => args[0] === "log")).toBe(false);
|
||||
});
|
||||
|
||||
it("classifies source, tests, migrations, and excluded paths", () => {
|
||||
expect(classifyPath("apps/web/app/routes/projects.tsx")).toBe("product");
|
||||
expect(classifyPath("packages/ui-subframe/src/Button.tsx")).toBe("product");
|
||||
expect(classifyPath("Dockerfile")).toBe("product");
|
||||
expect(classifyPath("apps/web/app/routes/projects.test.tsx")).toBe("test");
|
||||
expect(classifyPath("apps\\web\\e2e\\login.ts")).toBe("test");
|
||||
expect(classifyPath("packages/ui-app/src/Button.stories.tsx")).toBe("test");
|
||||
expect(classifyPath("prisma/migrations/202607_tests/migration.sql")).toBe("migration");
|
||||
expect(classifyPath("docs/example.ts")).toBeNull();
|
||||
expect(classifyPath(".planning/example.ts")).toBeNull();
|
||||
expect(classifyPath("packages/db/generated/client.ts")).toBeNull();
|
||||
expect(classifyPath("packages/db/src/contract.generated.ts")).toBeNull();
|
||||
expect(classifyPath("apps/web/app/client.min.js")).toBeNull();
|
||||
expect(classifyPath("apps/web/app/data.json")).toBeNull();
|
||||
expect(classifyPath("apps/web/app/components/ui/icons/vendor.tsx")).toBeNull();
|
||||
});
|
||||
|
||||
it("counts only non-empty physical lines", () => {
|
||||
expect(countNonEmptyLines(Buffer.from("one\n\n \ntwo\n"))).toBe(2);
|
||||
expect(countNonEmptyLines(Buffer.from([0, 1, 2]))).toBe(0);
|
||||
});
|
||||
|
||||
it("parses a detached CI ref with a stable display label", () => {
|
||||
expect(parseArgs(["--ref", "HEAD", "--label", "origin/main", "--output-dir", "/tmp/loc"])).toEqual(
|
||||
expect.objectContaining({
|
||||
baseline: CODE_IMPORT_BASELINE,
|
||||
label: "origin/main",
|
||||
outputDir: "/tmp/loc",
|
||||
ref: "HEAD",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("renders deterministic CSV and SVG without a fork-import jump annotation", () => {
|
||||
const rows = snapshotRows();
|
||||
const csv = renderCsv(rows, "origin/main");
|
||||
const svg = renderSvg(rows, "origin/main");
|
||||
|
||||
expect(renderCsv(rows, "origin/main")).toBe(csv);
|
||||
expect(renderSvg(rows, "origin/main")).toBe(svg);
|
||||
expect(csv).toContain(`# resolved_tip=${TIP}`);
|
||||
expect(csv).toContain(`"Import, ""fork"""`);
|
||||
expect(svg).toContain("SEIT CODE-IMPORT");
|
||||
expect(svg).toContain("+65 · +52 %");
|
||||
expect(svg).not.toMatch(/GRÖSSTER SPRUNG|largest jump|\+490\.231 LOC/i);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user