Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
289 lines (260 loc) · 10.3 KB
/
Copy pathscript.js
File metadata and controls
289 lines (260 loc) · 10.3 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* BGP Monitor — Dashboard Script */
const COLORS = {
A: '#00D4AA', B: '#FF6B6B', C: '#FBBF24',
D: '#A78BFA', P: '#60A5FA'
};
const TYPE_LABELS = {
A: '单对等体撤回', B: '全网故障',
C: '路由振荡', D: '永久消失', P: '部分断连'
};
let dailyChart = null, typeChart = null;
let eventsCache = null;
let eventPage = 0;
const EVENT_PAGE_SIZE = 10;
async function loadEvents() {
try {
const res = await fetch('data/events_summary.json');
if (res.ok) eventsCache = await res.json();
} catch(e) {}
}
async function loadData() {
try {
const res = await fetch('data/report.json');
if (!res.ok) throw new Error('HTTP ' + res.status);
return await res.json();
} catch(e) {
console.error('加载数据失败:', e);
document.body.innerHTML = `<div style="max-width:600px;margin:120px auto;text-align:center;padding:40px;background:#131A2B;border-radius:12px;border:1px solid #1F2937;">
<div style="font-size:48px;margin-bottom:16px;">⚠</div>
<h2 style="margin-bottom:8px;">数据加载失败</h2>
<p style="color:#6B7280;">请确保 data/report.json 文件存在,并已推送到 GitHub Pages。</p>
<p style="color:#6B7280;font-size:13px;margin-top:8px;">${e.message}</p>
</div>`;
return null;
}
}
function renderStats(data) {
const byType = data.by_type || {};
document.getElementById('statEvents').textContent = data.summary.total_events.toLocaleString();
document.getElementById('statFullOutage').textContent = (byType.B || 0).toLocaleString();
document.getElementById('statFlapping').textContent = (byType.C || 0).toLocaleString();
document.getElementById('statPermanent').textContent = (byType.D || 0).toLocaleString();
}
function renderDailyChart(data) {
const ctx = document.getElementById('dailyChart').getContext('2d');
const trend = data.daily_trend || [];
if (dailyChart) { dailyChart.destroy(); }
dailyChart = new Chart(ctx, {
type: 'line',
data: {
labels: trend.map(d => d.date.slice(5)),
datasets: [{
label: '事件数',
data: trend.map(d => d.count),
borderColor: '#00D4AA',
backgroundColor: 'rgba(0,212,170,0.08)',
fill: true,
tension: 0.3,
pointRadius: 3,
pointHoverRadius: 6,
borderWidth: 2,
}]
},
options: {
responsive: true, maintainAspectRatio: false,
plugins: { legend: { display: false } },
scales: {
x: { ticks: { color: '#6B7280', font: { size: 11, family: 'JetBrains Mono' } },
grid: { color: 'rgba(31,41,55,0.5)' } },
y: { ticks: { color: '#6B7280', font: { size: 11, family: 'JetBrains Mono' } },
grid: { color: 'rgba(31,41,55,0.5)' },
beginAtZero: true }
},
interaction: { mode: 'index', intersect: false }
}
});
}
function renderTypeChart(data) {
const ctx = document.getElementById('typeChart').getContext('2d');
const byType = data.by_type || {};
const labels = Object.keys(byType).map(k => TYPE_LABELS[k] || k);
const values = Object.values(byType);
const colors = Object.keys(byType).map(k => COLORS[k] || '#6B7280');
if (typeChart) { typeChart.destroy(); }
typeChart = new Chart(ctx, {
type: 'doughnut',
data: { labels, datasets: [{ data: values, backgroundColor: colors, borderWidth: 0 }] },
options: {
responsive: true, maintainAspectRatio: false, cutout: '65%',
plugins: {
legend: {
position: 'bottom',
labels: { color: '#E5E7EB', font: { size: 11 }, padding: 12, usePointStyle: true }
}
}
}
});
}
function renderTopPrefixes(data) {
const tbody = document.getElementById('prefixBody');
tbody.innerHTML = '';
const prefixes = data.top_prefixes || [];
if (prefixes.length === 0) {
tbody.innerHTML = '<tr><td colspan="3" style="text-align:center;color:#6B7280;">暂无数据</td></tr>';
return;
}
prefixes.slice(0, 50).forEach((p, i) => {
const tr = document.createElement('tr');
tr.innerHTML = `<td>${i+1}</td>
<td class="prefix-link" data-prefix="${p.prefix}">${p.prefix}</td>
<td>${(p.count || 0).toLocaleString()}</td>`;
tr.querySelector('.prefix-link').addEventListener('click', () => {
document.getElementById('searchInput').value = p.prefix;
doSearch();
});
tbody.appendChild(tr);
});
}
function renderEventCards(eventsCache, append = false) {
const container = document.getElementById('eventsContainer');
const loadMore = document.getElementById('loadMoreBtn');
if (!container || !eventsCache) return;
const allCards = (eventsCache.events || []).filter(e => ['B','C','D'].includes(e.type));
const total = allCards.length;
if (!append) {
eventPage = 0;
container.innerHTML = '';
}
const start = eventPage * EVENT_PAGE_SIZE;
const end = Math.min(start + EVENT_PAGE_SIZE, total);
const pageItems = allCards.slice(start, end);
pageItems.forEach(ev => {
const startTime = ev.start ? new Date(ev.start * 1000).toISOString().slice(5, 16).replace('T', ' ') : '?';
const duration = ev.end && ev.start ? Math.round((ev.end - ev.start) / 60) : '?';
const typeNames = {B:'全网故障', C:'路由振荡', D:'永久消失'};
const prefixColor = ev.type === 'B' ? '#FF6B6B' : ev.type === 'C' ? '#FBBF24' : '#A78BFA';
const link = document.createElement('a');
link.href = `event.html?id=${ev.id || ''}&prefix=${encodeURIComponent(ev.prefix || '')}&vps=${ev.vps || ''}`;
link.style.textDecoration = 'none';
link.style.color = 'inherit';
link.style.display = 'block';
link.innerHTML = `<div class="event-card" style="cursor:pointer;">
<div class="event-card-header">
<span class="event-card-prefix" style="color:${prefixColor}">${ev.prefix}</span>
<span class="event-type-badge event-type-${ev.type}">${ev.type} ${typeNames[ev.type] || ''}</span>
</div>
<div class="event-card-reason">${ev.reason || ''}</div>
<div class="event-card-meta">
<span>${startTime}</span>
<span>${duration}m</span>
<span>${ev.peers || '?'} 对等体</span>
</div>
</div>`;
container.appendChild(link);
});
eventPage++;
if (loadMore) {
if (end >= total) {
loadMore.style.display = 'none';
} else {
loadMore.style.display = 'block';
}
}
}
async function doSearch() {
const input = document.getElementById('searchInput').value.trim();
const resultDiv = document.getElementById('searchResult');
if (!input) {
resultDiv.innerHTML = '<div class="search-empty">输入前缀后点击查询</div>';
return;
}
resultDiv.innerHTML = '<div class="search-empty">查询中...</div>';
const filePrefix = input.replace('/', '_');
try {
const res = await fetch(`data/prefixes/${filePrefix}.json`);
if (!res.ok) throw new Error('未找到');
const data = await res.json();
const events = data.events || [];
if (events.length === 0) {
resultDiv.innerHTML = '<div class="search-empty">该前缀没有记录事件</div>';
return;
}
resultDiv.innerHTML = `<div style="font-weight:600;margin-bottom:8px;font-family:JetBrains Mono;color:var(--accent);">${input} — ${events.length} 条事件</div>`;
events.slice(0, 20).forEach(ev => {
const div = document.createElement('div');
div.className = 'event-item';
div.innerHTML = `<span class="event-time">${ev.time || ''}</span>
<span class="event-reason">${ev.reason || ''}</span>`;
resultDiv.appendChild(div);
});
if (events.length > 20) {
resultDiv.innerHTML += `<div class="search-empty" style="margin-top:8px;">...还有 ${events.length - 20} 条</div>`;
}
} catch(e) {
resultDiv.innerHTML = `<div class="search-empty">未找到前缀"${input}"的相关记录</div>`;
}
}
function showEventList(events, typeFilter) {
const resultDiv = document.getElementById('searchResult');
const title = typeFilter ? `类型 ${typeFilter} 事件 (前 ${events.length} 条)` : '最新事件 (前 50 条)';
let html = `<div style="font-weight:600;margin-bottom:8px;">${title}</div>`;
events.forEach((ev, i) => {
const start = ev.start ? new Date(ev.start * 1000).toISOString().slice(5, 16).replace('T', ' ') : '?';
const badge = `<span class="event-type-badge event-type-${ev.type}">${ev.type}</span>`;
html += `<div class="event-item">
<span class="event-time">${start}</span>
${badge}
<span class="event-reason">${ev.prefix}</span>
<span style="font-family:JetBrains Mono;font-size:11px;color:#6B7280;">${ev.reason ? ev.reason.slice(0, 30) : ''}</span>
</div>`;
});
resultDiv.innerHTML = html;
}
async function init() {
const data = await loadData();
if (!data) return;
document.getElementById('updateTime').textContent = '更新于 ' + (data.generated_at || '—');
renderStats(data);
try { renderDailyChart(data); } catch(e) { console.log('图表不可用:', e.message); }
try { renderTypeChart(data); } catch(e) { console.log('图表不可用:', e.message); }
renderTopPrefixes(data);
try { await loadEvents(); } catch(e) {}
try { renderEventCards(eventsCache); } catch(e) { console.log(e); }
// Load more button
const loadMore = document.getElementById('loadMoreBtn');
if (loadMore) {
loadMore.addEventListener('click', () => {
renderEventCards(eventsCache, true);
});
}
// Click stat cards
document.querySelectorAll('.stat-card').forEach((card, i) => {
card.style.cursor = 'pointer';
card.title = '点击查看详情';
card.addEventListener('click', () => {
const types = ['events', 'B', 'C', 'D'];
const et = types[i];
if (et === 'events' && eventsCache) {
showEventList(eventsCache.events.slice(0, 50));
} else if (eventsCache) {
const filtered = eventsCache.events.filter(e => e.type === et).slice(0, 50);
showEventList(filtered, et);
}
});
});
document.getElementById('searchBtn').addEventListener('click', doSearch);
document.getElementById('searchInput').addEventListener('keydown', e => {
if (e.key === 'Enter') doSearch();
});
// Animation
document.querySelectorAll('.stat-card').forEach((el, i) => {
el.style.opacity = '0';
el.style.transform = 'translateY(12px)';
setTimeout(() => {
el.style.transition = 'all 0.4s ease';
el.style.opacity = '1';
el.style.transform = 'translateY(0)';
}, i * 80);
});
}
document.addEventListener('DOMContentLoaded', init);