[优化] 添加访问日志地区解析功能,更新相关数据结构和前端展示

This commit is contained in:
ryan
2026-03-15 15:21:00 +08:00
parent 640dd6c82c
commit 7c89ad8c7b
14 changed files with 278 additions and 34 deletions
+24
View File
@@ -7,6 +7,7 @@ type NodeAccessLog struct {
NodeID string `json:"node_id" gorm:"index;size:64;not null"`
LoggedAt time.Time `json:"logged_at" gorm:"index"`
RemoteAddr string `json:"remote_addr" gorm:"size:128"`
Region string `json:"region" gorm:"size:128"`
Host string `json:"host" gorm:"size:255"`
Path string `json:"path" gorm:"size:2048"`
StatusCode int `json:"status_code"`
@@ -14,6 +15,11 @@ type NodeAccessLog struct {
CreatedAt time.Time `json:"created_at"`
}
type NodeAccessLogRegionCount struct {
Region string `json:"region"`
Count int64 `json:"count"`
}
func ListNodeAccessLogs(nodeID string, since time.Time, offset int, limit int) (logs []*NodeAccessLog, err error) {
query := DB.Order("logged_at desc, id desc")
if nodeID != "" {
@@ -51,3 +57,21 @@ func CountNodeAccessLogs(nodeID string, since time.Time) (totalRecords int64, to
}
return totalRecords, totalIPs, nil
}
func ListNodeAccessLogRegionCounts(nodeID string, since time.Time, limit int) (items []*NodeAccessLogRegionCount, err error) {
query := DB.Model(&NodeAccessLog{}).
Select("region as region, count(*) as count").
Where("region <> ''")
if nodeID != "" {
query = query.Where("node_id = ?", nodeID)
}
if !since.IsZero() {
query = query.Where("logged_at >= ?", since)
}
query = query.Group("region").Order("count desc, region asc")
if limit > 0 {
query = query.Limit(limit)
}
err = query.Scan(&items).Error
return items, err
}