实现解析日志查询功能

This commit is contained in:
Alex Yang
2025-11-30 02:29:21 +08:00
parent 0f0039f2d4
commit 843350300f
23 changed files with 723 additions and 10663 deletions

View File

@@ -106,6 +106,10 @@ func (s *Server) Start() error {
mux.HandleFunc("/api/daily-stats", s.handleDailyStats)
mux.HandleFunc("/api/monthly-stats", s.handleMonthlyStats)
mux.HandleFunc("/api/query/type", s.handleQueryTypeStats)
// 日志统计相关接口
mux.HandleFunc("/api/logs/stats", s.handleLogsStats)
mux.HandleFunc("/api/logs/query", s.handleLogsQuery)
mux.HandleFunc("/api/logs/count", s.handleLogsCount)
// WebSocket端点
mux.HandleFunc("/ws/stats", s.handleWebSocketStats)
@@ -1217,6 +1221,60 @@ func checkURLExists(url string) bool {
return resp.StatusCode >= 200 && resp.StatusCode < 400
}
// handleLogsStats 处理日志统计请求
func (s *Server) handleLogsStats(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// 获取日志统计数据
logStats := s.dnsServer.GetQueryStats()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(logStats)
}
// handleLogsQuery 处理日志查询请求
func (s *Server) handleLogsQuery(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// 获取查询参数
limit := 100 // 默认返回100条日志
offset := 0
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
fmt.Sscanf(limitStr, "%d", &limit)
}
if offsetStr := r.URL.Query().Get("offset"); offsetStr != "" {
fmt.Sscanf(offsetStr, "%d", &offset)
}
// 获取日志数据
logs := s.dnsServer.GetQueryLogs(limit, offset)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(logs)
}
// handleLogsCount 处理日志总数请求
func (s *Server) handleLogsCount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// 获取日志总数
count := s.dnsServer.GetQueryLogsCount()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]int{"count": count})
}
// handleRestart 处理重启服务请求
func (s *Server) handleRestart(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {