Repository navigation
Expand file tree
/
Copy pathexport-rtt-csv.js
More file actions
121 lines (103 loc) · 3.55 KB
/
Copy pathexport-rtt-csv.js
File metadata and controls
121 lines (103 loc) · 3.55 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Export all RTT fallback observations from test-results to CSV
// Usage:
// node export-rtt-csv.js
//
// Writes rtt.csv in the project root with columns:
// file, site_url, original_url, domain, ip, ipinfo_country, cloud_country,
// category, detection_method, rtt, rtt_error
const fs = require('fs');
const path = require('path');
const TEST_RESULTS_DIR = path.join(__dirname, 'test-results');
const OUTPUT_CSV = path.join(__dirname, 'rtt.csv');
function listResultFiles(dir) {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((ent) => ent.isFile() && ent.name.endsWith('.json'))
.map((ent) => path.join(dir, ent.name))
.sort();
}
function csvEscape(value) {
if (value === null || value === undefined) return '';
const str = String(value);
if (/[",\n]/.test(str)) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}
function stripUrlQueryAndFragment(value) {
if (value === null || value === undefined) return '';
return String(value).replace(/[?#].*$/, '');
}
function main() {
if (!fs.existsSync(TEST_RESULTS_DIR)) {
console.error(`Directory not found: ${TEST_RESULTS_DIR}`);
process.exit(1);
}
const files = listResultFiles(TEST_RESULTS_DIR);
console.log(`Scanning ${files.length} file(s)`);
const rows = [];
// CSV header
rows.push([
'file',
'site_url',
'original_url',
'domain',
'ip',
'ipinfo_country',
'cloud_country',
'category',
'detection_method',
'rtt',
'rtt_error'
].join(','));
for (const filePath of files) {
const content = fs.readFileSync(filePath, 'utf8');
let data;
try {
data = JSON.parse(content);
} catch (e) {
console.warn(`Invalid JSON: ${filePath}; skipping`);
continue;
}
if (!Array.isArray(data.domainDetails)) continue;
const fileName = path.basename(filePath);
for (const detail of data.domainDetails) {
const cp = detail.cloud_provider;
const ipinfo = detail.ipinfo || {};
if (!cp || cp.detection_method !== 'rtt') {
continue;
}
const siteUrl = data.url || '';
// Query strings can contain public site keys or access tokens that
// are irrelevant to RTT analysis and unsafe to publish.
const originalUrl = stripUrlQueryAndFragment(detail.originalUrl);
const domain = ipinfo.domain || '';
const ip = ipinfo.ip || '';
const ipinfoCountry = ipinfo.country || '';
const cloudCountry = cp.country || '';
const category = detail.category || '';
const detectionMethod = cp.detection_method || '';
const rtt = typeof cp.rtt === 'number' ? cp.rtt : '';
const rttError = cp.rtt_error || '';
const row = [
csvEscape(fileName),
csvEscape(siteUrl),
csvEscape(originalUrl),
csvEscape(domain),
csvEscape(ip),
csvEscape(ipinfoCountry),
csvEscape(cloudCountry),
csvEscape(category),
csvEscape(detectionMethod),
csvEscape(rtt),
csvEscape(rttError)
].join(',');
rows.push(row);
}
}
fs.writeFileSync(OUTPUT_CSV, `${rows.join('\n')}\n`, 'utf8');
console.log(`Wrote RTT CSV: ${OUTPUT_CSV}`);
}
if (require.main === module) {
main();
}