Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/scripts/metrics/loc-history.mjs
T

553 lines
20 KiB
JavaScript

#!/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("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&apos;");
}
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);
}
}