Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
727 lines (662 loc) · 30 KB
/
Copy pathindex.html
File metadata and controls
727 lines (662 loc) · 30 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网站状态监控</title>
<!-- Tailwind CSS v3 -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Font Awesome -->
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
<!-- Tailwind 配置 -->
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#3B82F6',
secondary: '#10B981',
warning: '#F59E0B',
danger: '#EF4444',
dark: '#1F2937',
light: '#F3F4F6'
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif']
},
animation: {
'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
}
}
}
}
</script>
<style type="text/tailwindcss">
@layer utilities {
.card-shadow {
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.card-hover {
transition: all 0.3s ease;
}
.card-hover:hover {
transform: translateY(-5px);
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
.status-online {
background-color: rgba(16, 185, 129, 0.1);
border-left: 4px solid #10B981;
}
.status-crowded {
background-color: rgba(245, 158, 11, 0.1);
border-left: 4px solid #F59E0B;
}
.status-offline {
background-color: rgba(239, 68, 68, 0.1);
border-left: 4px solid #EF4444;
}
.chart-container {
position: relative;
height: 180px;
width: 100%;
}
.modal-backdrop {
background-color: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(4px);
}
}
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<!-- 顶部导航栏 -->
<nav class="bg-white shadow-md">
<div class="container mx-auto px-4 py-3 flex justify-between items-center">
<div class="flex items-center space-x-2">
<i class="fa fa-line-chart text-primary text-2xl"></i>
<h1 class="text-xl font-bold text-gray-800">网站状态监控</h1>
</div>
<div class="text-sm text-gray-600" id="last-updated">
最后更新: 加载中...
</div>
</div>
</nav>
<!-- 主内容区 -->
<main class="container mx-auto px-4 py-6">
<!-- 总览卡片 -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
<div class="bg-white rounded-lg shadow-md p-6 card-shadow">
<h2 class="text-lg font-semibold text-gray-700 mb-2">今日累计访问量</h2>
<div class="flex items-end">
<span class="text-3xl font-bold text-primary" id="today-total-visits">0</span>
<span class="text-sm text-gray-500 ml-2 mb-1">次访问</span>
</div>
<div class="mt-4 text-sm text-gray-500 flex items-center">
<i class="fa fa-refresh mr-1"></i>
<span id="today-change">--</span>
</div>
</div>
<div class="bg-white rounded-lg shadow-md p-6 card-shadow">
<h2 class="text-lg font-semibold text-gray-700 mb-2">本月独立访客</h2>
<div class="flex items-end">
<span class="text-3xl font-bold text-secondary" id="monthly-unique-visitors">0</span>
<span class="text-sm text-gray-500 ml-2 mb-1">位访客</span>
</div>
<div class="mt-4 text-sm text-gray-500 flex items-center">
<i class="fa fa-refresh mr-1"></i>
<span id="monthly-change">--</span>
</div>
</div>
</div>
<!-- 网站状态卡片列表 -->
<h2 class="text-xl font-bold text-gray-800 mb-4">网站状态</h2>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6" id="website-cards">
<!-- 网站卡片将通过JavaScript动态生成 -->
<div class="animate-pulse bg-white rounded-lg shadow-md p-6 card-shadow">
<div class="flex justify-between items-start">
<div>
<h3 class="text-lg font-semibold text-gray-700">加载中...</h3>
<p class="text-sm text-gray-500">https://example.com</p>
</div>
<span class="px-3 py-1 rounded-full text-xs font-medium bg-gray-200 text-gray-700">加载中</span>
</div>
<div class="mt-4 h-40 bg-gray-200 rounded"></div>
</div>
</div>
</main>
<!-- 详情模态框 -->
<div id="detail-modal" class="fixed inset-0 z-50 flex items-center justify-center hidden">
<div class="modal-backdrop absolute inset-0" onclick="closeModal()"></div>
<div class="bg-white rounded-lg shadow-xl w-full max-w-4xl max-h-[90vh] overflow-y-auto relative z-10">
<div class="p-6 border-b border-gray-200">
<div class="flex justify-between items-center">
<h3 class="text-xl font-bold text-gray-800" id="modal-title">网站详情</h3>
<button onclick="closeModal()" class="text-gray-500 hover:text-gray-700">
<i class="fa fa-times text-xl"></i>
</button>
</div>
<p class="text-sm text-gray-500 mt-1" id="modal-url"></p>
</div>
<div class="p-6">
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
<div class="bg-gray-50 rounded-lg p-4">
<h4 class="text-sm font-medium text-gray-500">本月独立访客</h4>
<p class="text-2xl font-bold text-secondary" id="modal-monthly-unique">0</p>
</div>
<div class="bg-gray-50 rounded-lg p-4">
<h4 class="text-sm font-medium text-gray-500">今日独立访客</h4>
<p class="text-2xl font-bold text-primary" id="modal-today-unique">0</p>
</div>
<div class="bg-gray-50 rounded-lg p-4">
<h4 class="text-sm font-medium text-gray-500">今日累计访问量</h4>
<p class="text-2xl font-bold text-primary" id="modal-today-total">0</p>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<h4 class="text-lg font-semibold text-gray-700 mb-3">访问量趋势</h4>
<div class="bg-white rounded-lg shadow p-4">
<canvas id="visits-chart" height="250"></canvas>
</div>
</div>
<div>
<h4 class="text-lg font-semibold text-gray-700 mb-3">延迟统计</h4>
<div class="bg-white rounded-lg shadow p-4">
<canvas id="latency-chart" height="250"></canvas>
</div>
<div class="mt-4 grid grid-cols-2 gap-4">
<div class="bg-gray-50 rounded-lg p-4">
<h5 class="text-sm font-medium text-gray-500">平均延迟</h5>
<p class="text-xl font-bold text-gray-800" id="modal-avg-latency">0 ms</p>
</div>
<div class="bg-gray-50 rounded-lg p-4">
<h5 class="text-sm font-medium text-gray-500">最高延迟</h5>
<p class="text-xl font-bold text-warning" id="modal-max-latency">0 ms</p>
</div>
</div>
</div>
</div>
</div>
<div class="p-6 border-t border-gray-200 bg-gray-50 rounded-b-lg">
<button onclick="closeModal()" class="px-4 py-2 bg-gray-200 text-gray-800 rounded hover:bg-gray-300 transition">
关闭
</button>
</div>
</div>
</div>
<!-- API配置 -->
<script>
// API配置
const apiConfig = {
// 替换为您的Cloudflare Workers URL
baseUrl: 'https://status.oc99.top/api'
};
</script>
<!-- 主脚本 -->
<script>
// 全局变量
let visitsChart = null;
let latencyChart = null;
let websiteData = [];
let overviewData = null;
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
initializeApp();
});
// 初始化应用
async function initializeApp() {
try {
// 显示加载状态
showLoadingState();
// 并行加载数据
const [websitesResult, overviewResult] = await Promise.all([
fetchWebsiteList(),
fetchOverviewData()
]);
// 更新数据
websiteData = websitesResult;
overviewData = overviewResult;
// 更新UI
updateOverviewStats();
renderWebsiteCards();
updateLastUpdated();
// 设置定时刷新
setInterval(refreshData, 300000); // 每5分钟刷新一次
} catch (error) {
console.error('初始化应用失败:', error);
showError('加载数据失败,请稍后重试');
}
}
// 从API获取网站列表
async function fetchWebsiteList() {
try {
const response = await fetch(`${apiConfig.baseUrl}/websites`);
if (!response.ok) {
throw new Error(`API请求失败: ${response.status}`);
}
const data = await response.json();
if (!data.success) {
throw new Error(data.error || '获取网站列表失败');
}
return data.data;
} catch (error) {
console.error('获取网站列表失败:', error);
throw error;
}
}
// 从API获取总览数据
async function fetchOverviewData() {
try {
const response = await fetch(`${apiConfig.baseUrl}/overview`);
if (!response.ok) {
throw new Error(`API请求失败: ${response.status}`);
}
const data = await response.json();
if (!data.success) {
throw new Error(data.error || '获取总览数据失败');
}
return data.data;
} catch (error) {
console.error('获取总览数据失败:', error);
throw error;
}
}
// 从API获取网站详细统计数据
async function fetchWebsiteStats(websiteId) {
try {
const response = await fetch(`${apiConfig.baseUrl}/stats?website_id=${websiteId}`);
if (!response.ok) {
throw new Error(`API请求失败: ${response.status}`);
}
const data = await response.json();
if (!data.success) {
throw new Error(data.error || '获取网站统计数据失败');
}
return data.data;
} catch (error) {
console.error('获取网站统计数据失败:', error);
throw error;
}
}
// 显示加载状态
function showLoadingState() {
const container = document.getElementById('website-cards');
container.innerHTML = `
<div class="animate-pulse bg-white rounded-lg shadow-md p-6 card-shadow">
<div class="flex justify-between items-start">
<div>
<h3 class="text-lg font-semibold text-gray-700">加载中...</h3>
<p class="text-sm text-gray-500">https://example.com</p>
</div>
<span class="px-3 py-1 rounded-full text-xs font-medium bg-gray-200 text-gray-700">加载中</span>
</div>
<div class="mt-4 h-40 bg-gray-200 rounded"></div>
</div>
`;
}
// 更新总览统计数据
function updateOverviewStats() {
if (!overviewData) return;
// 更新今日总访问量
document.getElementById('today-total-visits').textContent = formatNumber(overviewData.today.totalVisits);
// 更新本月独立访客数
document.getElementById('monthly-unique-visitors').textContent = formatNumber(overviewData.month.uniqueVisitors);
// 更新环比变化
const todayChange = overviewData.today.change.toFixed(1);
const monthlyChange = overviewData.month.change.toFixed(1);
const todayChangeElement = document.getElementById('today-change');
todayChangeElement.textContent = `${todayChange > 0 ? '+' : ''}${todayChange}% 较昨日`;
todayChangeElement.className = `text-sm ml-2 mb-1 ${todayChange > 0 ? 'text-green-500' : 'text-red-500'}`;
const monthlyChangeElement = document.getElementById('monthly-change');
monthlyChangeElement.textContent = `${monthlyChange > 0 ? '+' : ''}${monthlyChange}% 较上月`;
monthlyChangeElement.className = `text-sm ml-2 mb-1 ${monthlyChange > 0 ? 'text-green-500' : 'text-red-500'}`;
}
// 渲染网站卡片
function renderWebsiteCards() {
const container = document.getElementById('website-cards');
container.innerHTML = '';
websiteData.forEach(website => {
const card = document.createElement('div');
card.className = `bg-white rounded-lg shadow-md p-6 card-shadow card-hover cursor-pointer`;
card.onclick = () => openWebsiteDetail(website);
// 根据状态设置卡片样式
let statusClass = '';
switch (website.status) {
case 'online':
statusClass = 'bg-green-100 text-green-800';
break;
case 'crowded':
statusClass = 'bg-yellow-100 text-yellow-800';
break;
case 'offline':
statusClass = 'bg-red-100 text-red-800';
break;
}
card.innerHTML = `
<div class="flex justify-between items-start">
<div>
<h3 class="text-lg font-semibold text-gray-700">${website.name}</h3>
<p class="text-sm text-gray-500 truncate" title="${website.url}">${website.url}</p>
</div>
<span class="px-3 py-1 rounded-full text-xs font-medium ${statusClass}">${website.statusText}</span>
</div>
<div class="mt-4 chart-container">
<canvas id="chart-${website.id}"></canvas>
</div>
`;
container.appendChild(card);
// 渲染卡片内的图表
renderCardChart(website);
});
}
// 渲染卡片内的图表
function renderCardChart(website) {
const ctx = document.getElementById(`chart-${website.id}`).getContext('2d');
// 准备数据
const labels = website.hourlyStats.map(stat => {
const hour = stat.timestamp.getHours();
return `${hour}:00`;
});
const visitData = website.hourlyStats.map(stat => stat.totalVisits);
// 根据延迟设置柱状图颜色
const backgroundColors = website.hourlyStats.map(stat => {
if (stat.latency >= 1000) {
return 'rgba(239, 68, 68, 0.7)'; // 红色 - 离线
} else if (stat.latency >= 500) {
return 'rgba(245, 158, 11, 0.7)'; // 黄色 - 拥挤
} else {
return 'rgba(16, 185, 129, 0.7)'; // 绿色 - 正常
}
});
// 创建图表
new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: '访问量',
data: visitData,
backgroundColor: backgroundColors,
borderColor: 'transparent',
borderWidth: 1,
borderRadius: 4,
barPercentage: 0.6,
categoryPercentage: 0.8
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
afterLabel: function(context) {
const index = context.dataIndex;
const latency = website.hourlyStats[index].latency;
return `延迟: ${latency}ms`;
}
}
}
},
scales: {
x: {
display: false
},
y: {
display: false,
min: 0
}
},
animation: {
duration: 1000
}
}
});
}
// 打开网站详情
async function openWebsiteDetail(website) {
try {
// 显示加载状态
document.getElementById('modal-title').textContent = '加载中...';
document.getElementById('modal-url').textContent = '';
document.getElementById('modal-monthly-unique').textContent = '加载中...';
document.getElementById('modal-today-unique').textContent = '加载中...';
document.getElementById('modal-today-total').textContent = '加载中...';
document.getElementById('modal-avg-latency').textContent = '加载中...';
document.getElementById('modal-max-latency').textContent = '加载中...';
// 显示模态框
document.getElementById('detail-modal').classList.remove('hidden');
document.body.style.overflow = 'hidden'; // 防止背景滚动
// 从API获取详细数据
const statsData = await fetchWebsiteStats(website.id);
// 填充模态框内容
document.getElementById('modal-title').textContent = statsData.website.name;
document.getElementById('modal-url').textContent = statsData.website.url;
document.getElementById('modal-monthly-unique').textContent = formatNumber(statsData.month.uniqueVisitors);
document.getElementById('modal-today-unique').textContent = formatNumber(statsData.today.uniqueVisitors);
document.getElementById('modal-today-total').textContent = formatNumber(statsData.today.totalVisits);
document.getElementById('modal-avg-latency').textContent = `${Math.round(statsData.latency.average)} ms`;
document.getElementById('modal-max-latency').textContent = `${Math.round(statsData.latency.maximum)} ms`;
// 准备图表数据
const chartData = {
website: statsData.website,
hourlyStats: statsData.hourlyStats.map(stat => ({
timestamp: new Date(stat.timestamp),
totalVisits: stat.total_visits,
uniqueVisitors: stat.unique_visitors,
latency: stat.latency
}))
};
// 渲染访问量趋势图
renderVisitsChart(chartData);
// 渲染延迟统计图
renderLatencyChart(chartData);
} catch (error) {
console.error('加载网站详情失败:', error);
alert('加载网站详情失败,请稍后重试');
closeModal();
}
}
// 渲染访问量趋势图
function renderVisitsChart(website) {
const ctx = document.getElementById('visits-chart').getContext('2d');
// 销毁旧图表
if (visitsChart) {
visitsChart.destroy();
}
// 准备数据
const labels = website.hourlyStats.map(stat => {
const hour = stat.timestamp.getHours();
return `${hour}:00`;
});
const totalVisitsData = website.hourlyStats.map(stat => stat.totalVisits);
const uniqueVisitorsData = website.hourlyStats.map(stat => stat.uniqueVisitors);
// 创建图表
visitsChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: '累计访问量',
data: totalVisitsData,
borderColor: '#3B82F6',
backgroundColor: 'rgba(59, 130, 246, 0.1)',
fill: true,
tension: 0.4,
borderWidth: 2,
pointRadius: 3,
pointBackgroundColor: '#3B82F6'
},
{
label: '独立访客',
data: uniqueVisitorsData,
borderColor: '#10B981',
backgroundColor: 'rgba(16, 185, 129, 0.1)',
fill: true,
tension: 0.4,
borderWidth: 2,
pointRadius: 3,
pointBackgroundColor: '#10B981'
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
labels: {
usePointStyle: true,
boxWidth: 6
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
beginAtZero: true,
grid: {
color: 'rgba(0, 0, 0, 0.05)'
}
}
}
}
});
}
// 渲染延迟统计图
function renderLatencyChart(website) {
const ctx = document.getElementById('latency-chart').getContext('2d');
// 销毁旧图表
if (latencyChart) {
latencyChart.destroy();
}
// 准备数据
const labels = website.hourlyStats.map(stat => {
const hour = stat.timestamp.getHours();
return `${hour}:00`;
});
const latencyData = website.hourlyStats.map(stat => stat.latency);
// 创建图表
latencyChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: '延迟 (ms)',
data: latencyData,
borderColor: '#F59E0B',
backgroundColor: 'rgba(245, 158, 11, 0.1)',
fill: true,
tension: 0.4,
borderWidth: 2,
pointRadius: 3,
pointBackgroundColor: function(context) {
const value = context.dataset.data[context.dataIndex];
if (value >= 1000) {
return '#EF4444'; // 红色 - 离线
} else if (value >= 500) {
return '#F59E0B'; // 黄色 - 拥挤
} else {
return '#10B981'; // 绿色 - 正常
}
}
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
labels: {
usePointStyle: true,
boxWidth: 6
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
beginAtZero: true,
grid: {
color: 'rgba(0, 0, 0, 0.05)'
},
ticks: {
callback: function(value) {
return value + ' ms';
}
}
}
}
}
});
}
// 关闭模态框
function closeModal() {
document.getElementById('detail-modal').classList.add('hidden');
document.body.style.overflow = ''; // 恢复背景滚动
}
// 刷新数据
async function refreshData() {
try {
// 并行加载数据
const [websitesResult, overviewResult] = await Promise.all([
fetchWebsiteList(),
fetchOverviewData()
]);
// 更新数据
websiteData = websitesResult;
overviewData = overviewResult;
// 更新UI
updateOverviewStats();
renderWebsiteCards();
updateLastUpdated();
} catch (error) {
console.error('刷新数据失败:', error);
}
}
// 更新最后更新时间
function updateLastUpdated() {
const now = new Date();
const timeString = now.toLocaleTimeString('zh-CN', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
document.getElementById('last-updated').textContent = `最后更新: ${timeString}`;
}
// 格式化数字 (添加千位分隔符)
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// 显示错误消息
function showError(message) {
const container = document.getElementById('website-cards');
container.innerHTML = `
<div class="col-span-full bg-red-50 border border-red-200 rounded-lg p-6">
<div class="flex items-center">
<i class="fa fa-exclamation-circle text-red-500 text-xl mr-3"></i>
<p class="text-red-700">${message}</p>
</div>
</div>
`;
}
</script>
</body>
</html>