|
| 1 | +#!/usr/bin/env node |
| 2 | +// Renders the "Gittensor impact" README card for this repo: a dark-themed SVG |
| 3 | +// with 12-week sparklines (merged PRs, contributors, lines changed) and a |
| 4 | +// meter (emission share), styled with this repo's own brand tokens rather |
| 5 | +// than a generic third-party template. Replaces matthewevans/gittensor-impact-action's |
| 6 | +// rendering (its per-repo data-fetch approach inspired this, but the visual |
| 7 | +// design here is our own, matching apps/gittensory-ui/src/styles.css). |
| 8 | +// |
| 9 | +// Usage: node scripts/gittensor-impact-card.mjs <owner/repo> <out-file.svg> |
| 10 | + |
| 11 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 12 | +import { fileURLToPath } from "node:url"; |
| 13 | +import path from "node:path"; |
| 14 | + |
| 15 | +const WEEKS = 12; |
| 16 | +const THEME = { |
| 17 | + cardBg: "#0e100d", |
| 18 | + fg: "#f3f6f3", |
| 19 | + muted: "#949a93", |
| 20 | + accent: "#d5e43f", |
| 21 | + accentTrack: "#333821", |
| 22 | + border: "#2a2c29", |
| 23 | + radius: 24, |
| 24 | +}; |
| 25 | + |
| 26 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 27 | +const repoRoot = path.resolve(__dirname, ".."); |
| 28 | +const repoIconPath = path.join(repoRoot, "apps/gittensory-ui/public/brand/gittensory-icon-citron.svg"); |
| 29 | + |
| 30 | +function compact(n) { |
| 31 | + if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + "M"; |
| 32 | + if (n >= 1000) return (n / 1000).toFixed(0) + "k"; |
| 33 | + return String(n); |
| 34 | +} |
| 35 | + |
| 36 | +async function fetchJson(url) { |
| 37 | + const res = await fetch(url, { headers: { "User-Agent": "gittensor-impact-card/1.0" } }); |
| 38 | + if (!res.ok) throw new Error(`fetch failed: ${url} (${res.status})`); |
| 39 | + return res.json(); |
| 40 | +} |
| 41 | + |
| 42 | +function bucketWeekly(prs, now) { |
| 43 | + const weekMs = 7 * 24 * 60 * 60 * 1000; |
| 44 | + const bucketStart = new Date(now.getTime() - WEEKS * weekMs); |
| 45 | + const prBuckets = Array(WEEKS).fill(0); |
| 46 | + const locBuckets = Array(WEEKS).fill(0); |
| 47 | + const seenByBucket = Array.from({ length: WEEKS }, () => new Set()); |
| 48 | + const contributorBuckets = Array(WEEKS).fill(0); |
| 49 | + |
| 50 | + for (const pr of prs) { |
| 51 | + const t = new Date(pr.mergedAt); |
| 52 | + if (t < bucketStart || t > now) continue; |
| 53 | + const idx = Math.min(WEEKS - 1, Math.floor((t - bucketStart) / weekMs)); |
| 54 | + prBuckets[idx] += 1; |
| 55 | + locBuckets[idx] += (pr.additions || 0) + (pr.deletions || 0); |
| 56 | + seenByBucket[idx].add(pr.author); |
| 57 | + } |
| 58 | + |
| 59 | + const seenSoFar = new Set(); |
| 60 | + for (let i = 0; i < WEEKS; i++) { |
| 61 | + for (const a of seenByBucket[i]) seenSoFar.add(a); |
| 62 | + contributorBuckets[i] = seenSoFar.size; |
| 63 | + } |
| 64 | + return { prBuckets, locBuckets, contributorBuckets }; |
| 65 | +} |
| 66 | + |
| 67 | +function sparkline(x, y, w, h, values, mutedColor, accentColor, cardBg) { |
| 68 | + const max = Math.max(...values, 1); |
| 69 | + const min = Math.min(...values, 0); |
| 70 | + const range = max - min || 1; |
| 71 | + const n = values.length; |
| 72 | + const stepX = w / (n - 1); |
| 73 | + const pts = values.map((v, i) => [x + i * stepX, y + h - ((v - min) / range) * h]); |
| 74 | + const svgPath = pts.map(([px, py], i) => `${i === 0 ? "M" : "L"}${px.toFixed(1)},${py.toFixed(1)}`).join(" "); |
| 75 | + const [lastX, lastY] = pts[pts.length - 1]; |
| 76 | + return ` |
| 77 | +<path d="${svgPath}" fill="none" stroke="${mutedColor}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/> |
| 78 | +<circle cx="${lastX.toFixed(1)}" cy="${lastY.toFixed(1)}" r="6" fill="${cardBg}"/> |
| 79 | +<circle cx="${lastX.toFixed(1)}" cy="${lastY.toFixed(1)}" r="4" fill="${accentColor}"/>`; |
| 80 | +} |
| 81 | + |
| 82 | +function meter(x, y, w, h, value, max, accentColor, trackColor) { |
| 83 | + const fillW = Math.max(h, Math.min(value / max, 1) * w); |
| 84 | + return ` |
| 85 | +<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${h / 2}" fill="${trackColor}"/> |
| 86 | +<rect x="${x}" y="${y}" width="${fillW.toFixed(1)}" height="${h}" rx="${h / 2}" fill="${accentColor}"/>`; |
| 87 | +} |
| 88 | + |
| 89 | +function render({ repo, impact, buckets, gtLogoB64, repoIconB64 }) { |
| 90 | + const { cardBg, fg, muted, accent, accentTrack, border, radius } = THEME; |
| 91 | + const W = 1200, H = 420; |
| 92 | + const pad = 56; |
| 93 | + const cols = 4; |
| 94 | + const colW = (W - 2 * pad) / cols; |
| 95 | + const font = "'DM Sans', ui-sans-serif, system-ui, -apple-system, sans-serif"; |
| 96 | + const displayFont = "'Space Grotesk', ui-sans-serif, system-ui, -apple-system, sans-serif"; |
| 97 | + const sparkW = colW - 40; |
| 98 | + const sparkH = 56; |
| 99 | + const sparkY = 150; |
| 100 | + |
| 101 | + const stats = [ |
| 102 | + { type: "sparkline", series: buckets.prBuckets, value: impact.totalPRs.toLocaleString(), label: "merged PRs" }, |
| 103 | + { type: "sparkline", series: buckets.contributorBuckets, value: String(impact.totalContributors), label: "contributors" }, |
| 104 | + { type: "sparkline", series: buckets.locBuckets, value: compact(impact.totalLinesChanged), label: "lines changed" }, |
| 105 | + { type: "meter", raw: impact.emissionShare * 100, max: 100, value: `${(impact.emissionShare * 100).toFixed(1)}%`, label: "emission share" }, |
| 106 | + ]; |
| 107 | + |
| 108 | + let statsSvg = ""; |
| 109 | + stats.forEach((s, i) => { |
| 110 | + const x = pad + i * colW; |
| 111 | + if (s.type === "meter") { |
| 112 | + statsSvg += meter(x, sparkY + sparkH / 2 - 7, sparkW, 14, s.raw, s.max, accent, accentTrack); |
| 113 | + } else { |
| 114 | + statsSvg += sparkline(x, sparkY, sparkW, sparkH, s.series, muted, accent, cardBg); |
| 115 | + } |
| 116 | + statsSvg += ` |
| 117 | +<text x="${x}" y="${sparkY + sparkH + 78}" font-family="${font}" font-size="60" font-weight="700" fill="${fg}">${s.value}</text> |
| 118 | +<text x="${x}" y="${sparkY + sparkH + 112}" font-family="${font}" font-size="21" font-weight="500" fill="${muted}">${s.label}</text>`; |
| 119 | + }); |
| 120 | + |
| 121 | + const logoW = 48, logoH = 48 / (708 / 567); // gittensor.io/gt-logo.svg aspect ratio |
| 122 | + const repoIconSize = 26; |
| 123 | + const repoIconX = W - pad - repoIconSize; |
| 124 | + const repoIconY = 396 - 14 - (repoIconSize - 16); |
| 125 | + const repoTextX = repoIconX - 10; |
| 126 | + |
| 127 | + return `<svg xmlns="http://www.w3.org/2000/svg" width="600" height="210" viewBox="0 0 ${W} ${H}" role="img"> |
| 128 | +<rect x="1" y="1" width="${W - 2}" height="${H - 2}" rx="${radius}" fill="${cardBg}" stroke="${border}" stroke-width="1"/> |
| 129 | +<image href="data:image/svg+xml;base64,${gtLogoB64}" x="${pad}" y="${(80 - logoH / 2 - 4).toFixed(1)}" width="${logoW}" height="${logoH.toFixed(1)}"/> |
| 130 | +<text x="${pad + logoW + 14}" y="80" font-family="${displayFont}" font-size="22" font-weight="500" letter-spacing="0.08em" fill="${muted}">GITTENSOR IMPACT</text> |
| 131 | +<line x1="${pad}" y1="124" x2="${W - pad}" y2="124" stroke="${border}" stroke-width="1"/> |
| 132 | +${statsSvg} |
| 133 | +<text x="${pad}" y="396" font-family="${font}" font-size="19" font-weight="400" fill="${muted}">Updated weekly · gittensor.io</text> |
| 134 | +<image href="data:image/svg+xml;base64,${repoIconB64}" x="${repoIconX}" y="${repoIconY}" width="${repoIconSize}" height="${repoIconSize}"/> |
| 135 | +<text x="${repoTextX}" y="396" font-family="${displayFont}" font-size="20" font-weight="500" letter-spacing="-0.01em" fill="${fg}" text-anchor="end">${repo}</text> |
| 136 | +</svg>`; |
| 137 | +} |
| 138 | + |
| 139 | +async function main() { |
| 140 | + const [repo, outFile] = process.argv.slice(2); |
| 141 | + if (!repo || !outFile) { |
| 142 | + console.error("Usage: node scripts/gittensor-impact-card.mjs <owner/repo> <out-file.svg>"); |
| 143 | + process.exit(1); |
| 144 | + } |
| 145 | + const encoded = repo.replace("/", "%2F"); |
| 146 | + const [impact, prs, gtLogoSvg] = await Promise.all([ |
| 147 | + fetchJson(`https://api.gittensor.io/repos/${encoded}/impact`), |
| 148 | + fetchJson(`https://api.gittensor.io/repos/${encoded}/prs`), |
| 149 | + fetch("https://gittensor.io/gt-logo.svg").then((r) => r.text()), |
| 150 | + ]); |
| 151 | + const buckets = bucketWeekly(prs, new Date()); |
| 152 | + const gtLogoB64 = Buffer.from(gtLogoSvg).toString("base64"); |
| 153 | + const repoIconB64 = Buffer.from(readFileSync(repoIconPath)).toString("base64"); |
| 154 | + |
| 155 | + const svg = render({ repo, impact, buckets, gtLogoB64, repoIconB64 }); |
| 156 | + writeFileSync(outFile, svg); |
| 157 | + console.log(`Wrote ${outFile} (${svg.length} bytes)`); |
| 158 | +} |
| 159 | + |
| 160 | +main().catch((err) => { |
| 161 | + console.error(err); |
| 162 | + process.exit(1); |
| 163 | +}); |
0 commit comments