-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (61 loc) · 1.96 KB
/
index.js
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
const SOURCIFY_STATS_URL = "https://repo.sourcify.dev/stats.json";
import fetch from "node-fetch";
import fs from "fs";
import csv from "fast-csv";
const downloadSourcifyStats = async () => {
const sourcifyStats = await (await fetch(SOURCIFY_STATS_URL)).json();
if (!sourcifyStats) {
throw new Error(
"Could not fetch sourcify stats from " + SOURCIFY_STATS_URL
);
}
return sourcifyStats;
};
downloadSourcifyStats()
.then((sourcifyStats) => {
// Separate file for each chain. Difficult to contain everything in a single .csv because the chains can change and columns will be malformed.
Object.keys(sourcifyStats).forEach((chainId) => {
const chainStats = {};
chainStats.full_match = sourcifyStats[chainId].full_match;
chainStats.partial_match = sourcifyStats[chainId]. partial_match;
chainStats.total = chainStats.full_match + chainStats.partial_match;
// Assign today's date in YYYY-MM-DD format to chains.date
chainStats.date = new Date().toISOString().split("T")[0];
appendCsv(chainId, chainStats);
});
})
.then(() => {
console.log("Done!");
});
const appendCsv = (chainId, rowObject) => {
return new Promise((resolve, reject) => {
const fileName = `./chainStats/${chainId}.csv`;
let writeHeaders = false;
try {
fs.accessSync(fileName, fs.constants.F_OK);
// file exists, don't write headers
} catch (err) {
writeHeaders = true;
}
const fsWriteStream = fs.createWriteStream(fileName, {
flags: "a",
}); // append flag.
const csvStream = csv.format({
headers: true,
writeHeaders: writeHeaders,
includeEndRowDelimiter: true,
});
csvStream
.pipe(fsWriteStream)
.on("finish", () => {
console.log("Finished writing to .csv");
resolve();
})
.on("error", () => {
console.error("Error writing to .csv");
reject();
});
csvStream.write(rowObject);
csvStream.end();
});
};