优化日志查询页面
This commit is contained in:
@@ -638,7 +638,7 @@ func (s *Server) GetStats() *Stats {
|
||||
}
|
||||
|
||||
// GetQueryLogs 获取查询日志
|
||||
func (s *Server) GetQueryLogs(limit, offset int) []QueryLog {
|
||||
func (s *Server) GetQueryLogs(limit, offset int, sortField, sortDirection string) []QueryLog {
|
||||
s.queryLogsMutex.RLock()
|
||||
defer s.queryLogsMutex.RUnlock()
|
||||
|
||||
@@ -650,17 +650,90 @@ func (s *Server) GetQueryLogs(limit, offset int) []QueryLog {
|
||||
limit = 100 // 默认返回100条日志
|
||||
}
|
||||
|
||||
// 创建日志副本用于排序
|
||||
logsCopy := make([]QueryLog, len(s.queryLogs))
|
||||
copy(logsCopy, s.queryLogs)
|
||||
|
||||
// 排序日志
|
||||
if sortField != "" {
|
||||
sort.Slice(logsCopy, func(i, j int) bool {
|
||||
var a, b interface{}
|
||||
switch sortField {
|
||||
case "time":
|
||||
a = logsCopy[i].Timestamp
|
||||
b = logsCopy[j].Timestamp
|
||||
case "clientIp":
|
||||
a = logsCopy[i].ClientIP
|
||||
b = logsCopy[j].ClientIP
|
||||
case "domain":
|
||||
a = logsCopy[i].Domain
|
||||
b = logsCopy[j].Domain
|
||||
case "responseTime":
|
||||
a = logsCopy[i].ResponseTime
|
||||
b = logsCopy[j].ResponseTime
|
||||
case "blockRule":
|
||||
a = logsCopy[i].BlockRule
|
||||
b = logsCopy[j].BlockRule
|
||||
default:
|
||||
// 默认按时间排序
|
||||
a = logsCopy[i].Timestamp
|
||||
b = logsCopy[j].Timestamp
|
||||
}
|
||||
|
||||
// 根据排序方向比较
|
||||
if sortDirection == "asc" {
|
||||
return compareValues(a, b) < 0
|
||||
}
|
||||
return compareValues(a, b) > 0
|
||||
})
|
||||
}
|
||||
|
||||
// 计算返回范围
|
||||
start := offset
|
||||
end := offset + limit
|
||||
if end > len(s.queryLogs) {
|
||||
end = len(s.queryLogs)
|
||||
if end > len(logsCopy) {
|
||||
end = len(logsCopy)
|
||||
}
|
||||
if start >= len(s.queryLogs) {
|
||||
if start >= len(logsCopy) {
|
||||
return []QueryLog{} // 没有数据,返回空切片
|
||||
}
|
||||
|
||||
return s.queryLogs[start:end]
|
||||
return logsCopy[start:end]
|
||||
}
|
||||
|
||||
// compareValues 比较两个值
|
||||
func compareValues(a, b interface{}) int {
|
||||
switch v1 := a.(type) {
|
||||
case time.Time:
|
||||
v2 := b.(time.Time)
|
||||
if v1.Before(v2) {
|
||||
return -1
|
||||
}
|
||||
if v1.After(v2) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case string:
|
||||
v2 := b.(string)
|
||||
if v1 < v2 {
|
||||
return -1
|
||||
}
|
||||
if v1 > v2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int64:
|
||||
v2 := b.(int64)
|
||||
if v1 < v2 {
|
||||
return -1
|
||||
}
|
||||
if v1 > v2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// GetQueryLogsCount 获取查询日志总数
|
||||
|
||||
Reference in New Issue
Block a user