实现了日志查询功能
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
// 全局变量
|
||||
let currentPage = 1;
|
||||
let totalPages = 1;
|
||||
let logsPerPage = 20;
|
||||
let logsPerPage = 30; // 默认显示30条记录
|
||||
let currentFilter = '';
|
||||
let currentSearch = '';
|
||||
let logsChart = null;
|
||||
@@ -23,6 +23,27 @@ function initLogsPage() {
|
||||
|
||||
// 绑定事件
|
||||
bindLogsEvents();
|
||||
|
||||
// 建立WebSocket连接,用于实时更新统计数据和图表
|
||||
connectLogsWebSocket();
|
||||
|
||||
// 在页面卸载时清理资源
|
||||
window.addEventListener('beforeunload', cleanupLogsResources);
|
||||
}
|
||||
|
||||
// 清理资源
|
||||
function cleanupLogsResources() {
|
||||
// 清除WebSocket连接
|
||||
if (wsConnection) {
|
||||
wsConnection.close();
|
||||
wsConnection = null;
|
||||
}
|
||||
|
||||
// 清除重连计时器
|
||||
if (wsReconnectTimer) {
|
||||
clearTimeout(wsReconnectTimer);
|
||||
wsReconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定事件
|
||||
@@ -59,6 +80,16 @@ function bindLogsEvents() {
|
||||
});
|
||||
}
|
||||
|
||||
// 自定义记录数量
|
||||
const perPageSelect = document.getElementById('logs-per-page');
|
||||
if (perPageSelect) {
|
||||
perPageSelect.addEventListener('change', () => {
|
||||
logsPerPage = parseInt(perPageSelect.value);
|
||||
currentPage = 1;
|
||||
loadLogs();
|
||||
});
|
||||
}
|
||||
|
||||
// 分页按钮
|
||||
const prevBtn = document.getElementById('logs-prev-page');
|
||||
const nextBtn = document.getElementById('logs-next-page');
|
||||
@@ -349,11 +380,103 @@ function updateLogsChart(range) {
|
||||
});
|
||||
}
|
||||
|
||||
// 定期更新日志数据
|
||||
// 建立WebSocket连接
|
||||
function connectLogsWebSocket() {
|
||||
try {
|
||||
// 构建WebSocket URL
|
||||
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${wsProtocol}//${window.location.host}/ws/stats`;
|
||||
|
||||
console.log('正在连接WebSocket:', wsUrl);
|
||||
|
||||
// 创建WebSocket连接
|
||||
wsConnection = new WebSocket(wsUrl);
|
||||
|
||||
// 连接打开事件
|
||||
wsConnection.onopen = function() {
|
||||
console.log('WebSocket连接已建立');
|
||||
};
|
||||
|
||||
// 接收消息事件
|
||||
wsConnection.onmessage = function(event) {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
|
||||
if (data.type === 'initial_data' || data.type === 'stats_update') {
|
||||
console.log('收到实时数据更新');
|
||||
// 只更新统计数据,不更新日志详情
|
||||
updateLogsStatsFromWebSocket(data.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('处理WebSocket消息失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 连接关闭事件
|
||||
wsConnection.onclose = function(event) {
|
||||
console.warn('WebSocket连接已关闭,代码:', event.code);
|
||||
wsConnection = null;
|
||||
|
||||
// 设置重连
|
||||
setupLogsReconnect();
|
||||
};
|
||||
|
||||
// 连接错误事件
|
||||
wsConnection.onerror = function(error) {
|
||||
console.error('WebSocket连接错误:', error);
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('创建WebSocket连接失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置重连逻辑
|
||||
function setupLogsReconnect() {
|
||||
if (wsReconnectTimer) {
|
||||
return; // 已经有重连计时器在运行
|
||||
}
|
||||
|
||||
const reconnectDelay = 5000; // 5秒后重连
|
||||
console.log(`将在${reconnectDelay}ms后尝试重新连接WebSocket`);
|
||||
|
||||
wsReconnectTimer = setTimeout(() => {
|
||||
connectLogsWebSocket();
|
||||
}, reconnectDelay);
|
||||
}
|
||||
|
||||
// 从WebSocket更新日志统计数据
|
||||
function updateLogsStatsFromWebSocket(stats) {
|
||||
try {
|
||||
// 更新统计卡片
|
||||
if (stats.dns) {
|
||||
// 适配不同的数据结构
|
||||
const totalQueries = stats.dns.Queries || 0;
|
||||
const blockedQueries = stats.dns.Blocked || 0;
|
||||
const allowedQueries = stats.dns.Allowed || 0;
|
||||
const errorQueries = stats.dns.Errors || 0;
|
||||
const avgResponseTime = stats.dns.AvgResponseTime || 0;
|
||||
const activeIPs = stats.activeIPs || Object.keys(stats.dns.SourceIPs || {}).length;
|
||||
|
||||
// 更新统计卡片
|
||||
document.getElementById('logs-total-queries').textContent = totalQueries;
|
||||
document.getElementById('logs-avg-response-time').textContent = avgResponseTime.toFixed(2) + 'ms';
|
||||
document.getElementById('logs-active-ips').textContent = activeIPs;
|
||||
|
||||
// 计算屏蔽率
|
||||
const blockRate = totalQueries > 0 ? (blockedQueries / totalQueries * 100).toFixed(1) : '0';
|
||||
document.getElementById('logs-block-rate').textContent = blockRate + '%';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('从WebSocket更新日志统计数据失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 定期更新日志统计数据(备用方案)
|
||||
setInterval(() => {
|
||||
// 只有在查询日志页面时才更新
|
||||
if (window.location.hash === '#logs') {
|
||||
loadLogsStats();
|
||||
loadLogs();
|
||||
// 不自动更新日志详情,只更新统计数据
|
||||
}
|
||||
}, 30000); // 每30秒更新一次
|
||||
|
||||
Reference in New Issue
Block a user