initial: ZeroTier-like P2P mesh VPN server with multi-tenant Web UI
This commit is contained in:
111
internal/repository/log.go
Normal file
111
internal/repository/log.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"zeromesh/internal/database"
|
||||
"zeromesh/internal/model"
|
||||
)
|
||||
|
||||
type LogRepo struct {
|
||||
db *database.DB
|
||||
}
|
||||
|
||||
func NewLogRepo(db *database.DB) *LogRepo {
|
||||
return &LogRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *LogRepo) InsertApiLog(log *model.ApiLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
func (r *LogRepo) InsertErrorLog(log *model.ErrorLog) error {
|
||||
return r.db.Create(log).Error
|
||||
}
|
||||
|
||||
func (r *LogRepo) ListApiLogs(page, pageSize int, ip, path, method string, statusCode int) ([]model.ApiLog, int64, error) {
|
||||
q := r.db.Model(&model.ApiLog{})
|
||||
if ip != "" {
|
||||
q = q.Where("ip LIKE ?", "%"+ip+"%")
|
||||
}
|
||||
if path != "" {
|
||||
q = q.Where("path LIKE ?", "%"+path+"%")
|
||||
}
|
||||
if method != "" {
|
||||
q = q.Where("method = ?", method)
|
||||
}
|
||||
if statusCode > 0 {
|
||||
q = q.Where("status_code = ?", statusCode)
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var logs []model.ApiLog
|
||||
offset := (page - 1) * pageSize
|
||||
if err := q.Order("id DESC").Offset(offset).Limit(pageSize).Find(&logs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
func (r *LogRepo) ListErrorLogs(page, pageSize int, ip, path, method string, statusCode int) ([]model.ErrorLog, int64, error) {
|
||||
q := r.db.Model(&model.ErrorLog{})
|
||||
if ip != "" {
|
||||
q = q.Where("ip LIKE ?", "%"+ip+"%")
|
||||
}
|
||||
if path != "" {
|
||||
q = q.Where("path LIKE ?", "%"+path+"%")
|
||||
}
|
||||
if method != "" {
|
||||
q = q.Where("method = ?", method)
|
||||
}
|
||||
if statusCode > 0 {
|
||||
q = q.Where("status_code = ?", statusCode)
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var logs []model.ErrorLog
|
||||
offset := (page - 1) * pageSize
|
||||
if err := q.Order("id DESC").Offset(offset).Limit(pageSize).Find(&logs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
func (r *LogRepo) ListApiLogGrouped() ([]map[string]interface{}, error) {
|
||||
rows, err := r.db.Model(&model.ApiLog{}).
|
||||
Select("ip, method, path, COUNT(*) as count, SUM(latency_ms) as total_latency").
|
||||
Group("ip, method, path").
|
||||
Order("count DESC").
|
||||
Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var ip, method, path string
|
||||
var count int64
|
||||
var totalLatency int64
|
||||
if err := rows.Scan(&ip, &method, &path, &count, &totalLatency); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"ip": ip,
|
||||
"method": method,
|
||||
"path": path,
|
||||
"count": count,
|
||||
"total_latency": totalLatency,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *LogRepo) ClearApiLogs() error {
|
||||
return r.db.Where("1 = 1").Delete(&model.ApiLog{}).Error
|
||||
}
|
||||
|
||||
func (r *LogRepo) ClearErrorLogs() error {
|
||||
return r.db.Where("1 = 1").Delete(&model.ErrorLog{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user