-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
178 lines (145 loc) · 4.93 KB
/
script.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const API_URL = 'https://long-snowflake-cf70.sd-api.workers.dev/metrics';
async function fetchMetrics(timeRange) {
try {
const response = await fetch(`${API_URL}?timeRange=${encodeURIComponent(timeRange)}`);
if (!response.ok) {
throw new Error(`Error fetching metrics: ${response.statusText}`);
}
const data = await response.json();
return data.results || data; // Adjust based on your API's response structure
} catch (error) {
console.error('Fetch Metrics Error:', error);
throw error;
}
}
function processData(metrics) {
const sourcesSet = new Set();
const targetsSet = new Set();
const dataMap = {};
metrics.forEach(metric => {
const {
source_region,
target_region,
avg_latency,
avg_tcp_bitrate,
avg_udp_bitrate
} = metric;
sourcesSet.add(source_region);
targetsSet.add(target_region);
if (!dataMap[source_region]) {
dataMap[source_region] = {};
}
dataMap[source_region][target_region] = {
avg_latency: avg_latency || null,
tcp_bitrate: avg_tcp_bitrate || null,
udp_bitrate: avg_udp_bitrate || null
};
});
const sources = Array.from(sourcesSet).sort();
const targets = Array.from(targetsSet).sort();
sources.forEach(source => {
if (!dataMap[source]) {
dataMap[source] = {};
}
targets.forEach(target => {
if (!dataMap[source][target]) {
dataMap[source][target] = { avg_latency: null, tcp_bitrate: null, udp_bitrate: null };
}
});
});
return {
sources,
targets,
dataMap
};
}
function formatBitrate(bitrate) {
if (bitrate === null) return '-';
if (bitrate >= 1000) {
return `${(bitrate / 1000).toFixed(2)} Gbps`; // Convert to Gbps if >= 1000 Mbps
}
return `${bitrate.toFixed(2)} Mbps`; // Keep as Mbps otherwise
}
function getLatencyClass(latency) {
if (latency === null) return '';
if (latency <= 100) return 'good';
if (latency <= 200) return 'average';
return 'poor';
}
function renderTable(sources, targets, dataMap) {
const container = document.getElementById('table-container');
container.innerHTML = ''; // Clear previous content
if (sources.length === 0 || targets.length === 0) {
container.innerHTML = '<p>No data available for the selected time range.</p>';
return;
}
const table = document.createElement('table');
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
const emptyCell = document.createElement('th');
emptyCell.innerText = 'Source \\ Target';
headerRow.appendChild(emptyCell);
targets.forEach(target => {
const th = document.createElement('th');
th.innerText = target;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement('tbody');
sources.forEach(source => {
const row = document.createElement('tr');
const sourceCell = document.createElement('th');
sourceCell.innerText = source;
row.appendChild(sourceCell);
targets.forEach(target => {
const cell = document.createElement('td');
const data = dataMap[source][target];
if (data) {
const { avg_latency, tcp_bitrate, udp_bitrate } = data;
const showAdvanced = document.getElementById('advanced').checked;
if (showAdvanced) {
const latencyText = avg_latency !== null ? `${avg_latency.toFixed(2)} ms` : '-';
const tcpText = formatBitrate(tcp_bitrate);
const udpText = formatBitrate(udp_bitrate);
cell.innerHTML = `<div>${latencyText}</div><div>TCP: ${tcpText}</div><div>UDP: ${udpText}</div>`;
} else {
const latencyText = avg_latency !== null ? `${avg_latency.toFixed(2)} ms` : '-';
cell.innerHTML = `<div>${latencyText}</div>`;
}
const latencyClass = getLatencyClass(avg_latency);
if (latencyClass) {
cell.classList.add(latencyClass);
}
} else {
cell.innerText = '-';
}
row.appendChild(cell);
});
tbody.appendChild(row);
});
table.appendChild(tbody);
container.appendChild(table);
}
async function updateDashboard() {
const timeRangeSelect = document.getElementById('timeRange');
const timeRange = timeRangeSelect.value;
const apiTimeRangeMap = {
'6h': '6h',
'12h': '12h',
'24h': '24h',
'3d': '3d'
};
const container = document.getElementById('table-container');
container.innerHTML = '<p class="loading">Loading data...</p>';
try {
const metrics = await fetchMetrics(apiTimeRangeMap[timeRange]);
const { sources, targets, dataMap } = processData(metrics);
renderTable(sources, targets, dataMap);
} catch (error) {
container.innerHTML = `<p class="error">Failed to load data: ${error.message}</p>`;
}
}
document.getElementById('timeRange').addEventListener('change', updateDashboard);
document.getElementById('advanced').addEventListener('change', updateDashboard);
updateDashboard();