-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-visualization.js
More file actions
96 lines (85 loc) · 2.51 KB
/
Copy pathbuild-visualization.js
File metadata and controls
96 lines (85 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import fetch from "node-fetch";
import fs from "fs";
import path from "path";
import { parseFile } from "fast-csv";
const CHAINS_API = "https://sourcify.dev/server/chains";
const CHAIN_STATS_DIR = "./chainStats";
const OUTPUT_DIR = "./docs";
const OUTPUT_FILE = path.join(OUTPUT_DIR, "data.json");
async function fetchChainNames() {
try {
const res = await fetch(CHAINS_API);
const chains = await res.json();
const nameMap = {};
for (const chain of chains) {
nameMap[String(chain.chainId)] = chain.name;
}
return nameMap;
} catch (e) {
console.warn("Could not fetch chain names:", e.message);
return {};
}
}
function readChainCsv(filePath) {
return new Promise((resolve, reject) => {
const dates = [];
const full = [];
const partial = [];
const total = [];
parseFile(filePath, { headers: true })
.on("data", (row) => {
if (!row.date) return;
dates.push(row.date);
full.push(Number(row.full_match) || 0);
partial.push(Number(row.partial_match) || 0);
total.push(Number(row.total) || 0);
})
.on("end", () => resolve({ dates, full, partial, total }))
.on("error", reject);
});
}
async function main() {
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
console.log("Fetching chain names from Sourcify API...");
const chainNames = await fetchChainNames();
const files = fs
.readdirSync(CHAIN_STATS_DIR)
.filter((f) => f.endsWith(".csv"))
.sort();
console.log(`Processing ${files.length} chain files...`);
const chains = {};
for (const file of files) {
const chainId = path.basename(file, ".csv");
try {
const data = await readChainCsv(path.join(CHAIN_STATS_DIR, file));
if (data.dates.length === 0) {
console.log(` Skipping empty: ${file}`);
continue;
}
chains[chainId] = {
name: chainNames[chainId] || `Chain ${chainId}`,
dates: data.dates,
full: data.full,
partial: data.partial,
total: data.total,
};
} catch (e) {
console.warn(` Error reading ${file}: ${e.message}`);
}
}
const output = {
lastUpdated: new Date().toISOString().split("T")[0],
chains,
};
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(output));
const kb = Math.round(fs.statSync(OUTPUT_FILE).size / 1024);
console.log(
`\nWritten ${OUTPUT_FILE} (${kb} KB, ${Object.keys(chains).length} chains)`
);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});