-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats2csv.js
59 lines (48 loc) · 1.73 KB
/
stats2csv.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
const fetch = (...args) => import("node-fetch").then(module => module.default(...args));
const json2csv = require('json2csv').parse;
// Get API server from command-line arguments
const apiServer = process.argv[2];
if (!apiServer) {
console.error("Please provide the API server as a command-line argument.");
process.exit(1);
}
// Construct the full URL
const apiUrl = `https://${apiServer}`;
// Function to fetch data and output CSV to console
async function fetchDataAndOutputCSV() {
try {
// Fetch JSON data from the URL
const response = await fetch(apiUrl);
const data = await response.json();
// Extracting access counters and error counters
const accessCounters = data.__stats__.access_counters;
const errorCounters = data.__stats__.error_counters;
// Preparing data for CSV
const csvData = Object.keys(accessCounters).map(url => {
const access = accessCounters[url];
const error = errorCounters[url] || {
errRate: 0,
total: access.count,
errorCount: 0,
succRate: 100
};
return {
URL: url,
Count: access.count,
Percent: access.percent,
"Error Count": error.errorCount,
"Error Rate": error.errRate,
Total: error.total,
"Succ Rate": error.succRate
};
});
// Convert JSON data to CSV
const csv = json2csv(csvData);
// Output CSV to console
console.log(csv);
} catch (error) {
console.error("Error fetching data or generating CSV:", error);
}
}
// Run the function
fetchDataAndOutputCSV();