将模拟数据修改为真实服务器统计数据
This commit is contained in:
@@ -87,27 +87,41 @@ func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
dnsStats := s.dnsServer.GetStats()
|
||||
shieldStats := s.shieldManager.GetStats()
|
||||
|
||||
// 获取最常用查询类型
|
||||
topQueryType := "A"
|
||||
// 获取最常用查询类型(如果有)
|
||||
topQueryType := "-"
|
||||
maxCount := int64(0)
|
||||
for queryType, count := range dnsStats.QueryTypes {
|
||||
if count > maxCount {
|
||||
maxCount = count
|
||||
topQueryType = queryType
|
||||
if len(dnsStats.QueryTypes) > 0 {
|
||||
for queryType, count := range dnsStats.QueryTypes {
|
||||
if count > maxCount {
|
||||
maxCount = count
|
||||
topQueryType = queryType
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取活跃来源IP数量
|
||||
activeIPCount := len(dnsStats.SourceIPs)
|
||||
|
||||
// 构建响应数据,确保所有字段都反映服务器的真实状态
|
||||
stats := map[string]interface{}{
|
||||
"dns": dnsStats,
|
||||
"shield": shieldStats,
|
||||
"topQueryType": topQueryType,
|
||||
"activeIPs": activeIPCount,
|
||||
"avgResponseTime": dnsStats.AvgResponseTime,
|
||||
"cpuUsage": dnsStats.CpuUsage,
|
||||
"time": time.Now(),
|
||||
"dns": map[string]interface{}{
|
||||
"Queries": dnsStats.Queries,
|
||||
"Blocked": dnsStats.Blocked,
|
||||
"Allowed": dnsStats.Allowed,
|
||||
"Errors": dnsStats.Errors,
|
||||
"LastQuery": dnsStats.LastQuery,
|
||||
"AvgResponseTime": dnsStats.AvgResponseTime,
|
||||
"TotalResponseTime": dnsStats.TotalResponseTime,
|
||||
"QueryTypes": dnsStats.QueryTypes,
|
||||
"SourceIPs": dnsStats.SourceIPs,
|
||||
"CpuUsage": dnsStats.CpuUsage,
|
||||
},
|
||||
"shield": shieldStats,
|
||||
"topQueryType": topQueryType,
|
||||
"activeIPs": activeIPCount,
|
||||
"avgResponseTime": dnsStats.AvgResponseTime,
|
||||
"cpuUsage": dnsStats.CpuUsage,
|
||||
"time": time.Now(),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -580,12 +594,25 @@ func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
stats := s.dnsServer.GetStats()
|
||||
|
||||
// 使用服务器的实际启动时间计算准确的运行时间
|
||||
serverStartTime := s.dnsServer.GetStartTime()
|
||||
uptime := time.Since(serverStartTime)
|
||||
|
||||
// 构建包含所有真实服务器统计数据的响应
|
||||
status := map[string]interface{}{
|
||||
"status": "running",
|
||||
"queries": stats.Queries,
|
||||
"lastQuery": stats.LastQuery,
|
||||
"uptime": time.Since(stats.LastQuery),
|
||||
"timestamp": time.Now(),
|
||||
"status": "running",
|
||||
"queries": stats.Queries,
|
||||
"blocked": stats.Blocked,
|
||||
"allowed": stats.Allowed,
|
||||
"errors": stats.Errors,
|
||||
"lastQuery": stats.LastQuery,
|
||||
"avgResponseTime": stats.AvgResponseTime,
|
||||
"activeIPs": len(stats.SourceIPs),
|
||||
"startTime": serverStartTime,
|
||||
"uptime": uptime,
|
||||
"cpuUsage": stats.CpuUsage,
|
||||
"timestamp": time.Now(),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
Reference in New Issue
Block a user