feat: 添加日志管理和敏感词过滤功能
- 新增日志管理模块:操作日志、登录日志、数据变更日志 - 新增敏感词过滤器 (sanitizer) - 新增日志异步写入管理器 - 新增日志定时清理服务 - 优化帖子相关服务和上传服务
This commit is contained in:
136
internal/repository/operation_log_repo.go
Normal file
136
internal/repository/operation_log_repo.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"carrot_bbs/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OperationLogRepository 操作日志仓储
|
||||
type OperationLogRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewOperationLogRepository 创建操作日志仓储
|
||||
func NewOperationLogRepository(db *gorm.DB) *OperationLogRepository {
|
||||
return &OperationLogRepository{db: db}
|
||||
}
|
||||
|
||||
// CreateOperationLog 创建单条操作日志
|
||||
func (r *OperationLogRepository) CreateOperationLog(ctx context.Context, log *model.OperationLog) error {
|
||||
return r.db.WithContext(ctx).Create(log).Error
|
||||
}
|
||||
|
||||
// BatchCreateOperationLogs 批量创建操作日志
|
||||
func (r *OperationLogRepository) BatchCreateOperationLogs(ctx context.Context, logs []*model.OperationLog) error {
|
||||
if len(logs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(logs).Error
|
||||
}
|
||||
|
||||
// GetOperationLogs 获取操作日志列表(分页)
|
||||
func (r *OperationLogRepository) GetOperationLogs(ctx context.Context, filters LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
query := r.db.WithContext(ctx).Model(&model.OperationLog{})
|
||||
|
||||
if filters.UserID != "" {
|
||||
query = query.Where("user_id = ?", filters.UserID)
|
||||
}
|
||||
if filters.Operation != "" {
|
||||
query = query.Where("operation = ?", filters.Operation)
|
||||
}
|
||||
if filters.TargetType != "" {
|
||||
query = query.Where("target_type = ?", filters.TargetType)
|
||||
}
|
||||
if filters.TargetID != "" {
|
||||
query = query.Where("target_id = ?", filters.TargetID)
|
||||
}
|
||||
if filters.Status != "" {
|
||||
query = query.Where("status = ?", filters.Status)
|
||||
}
|
||||
if filters.StartTime != "" {
|
||||
query = query.Where("occurred_at >= ?", filters.StartTime)
|
||||
}
|
||||
if filters.EndTime != "" {
|
||||
query = query.Where("occurred_at <= ?", filters.EndTime)
|
||||
}
|
||||
if filters.IP != "" {
|
||||
query = query.Where("ip = ?", filters.IP)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var logs []*model.OperationLog
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Order("occurred_at DESC").Offset(offset).Limit(pageSize).Find(&logs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
// GetOperationLogsByUser 获取指定用户的操作日志
|
||||
func (r *OperationLogRepository) GetOperationLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
return r.GetOperationLogs(ctx, LogFilter{UserID: userID}, page, pageSize)
|
||||
}
|
||||
|
||||
// GetOperationLogsByTimeRange 按时间范围获取操作日志
|
||||
func (r *OperationLogRepository) GetOperationLogsByTimeRange(ctx context.Context, startTime, endTime string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
||||
return r.GetOperationLogs(ctx, LogFilter{StartTime: startTime, EndTime: endTime}, page, pageSize)
|
||||
}
|
||||
|
||||
// DeleteOldLogs 删除旧的操作日志(用于定时清理)
|
||||
func (r *OperationLogRepository) DeleteOldLogs(ctx context.Context, beforeDate string) error {
|
||||
return r.db.WithContext(ctx).Where("created_at < ?", beforeDate).Delete(&model.OperationLog{}).Error
|
||||
}
|
||||
|
||||
// GetStatistics 获取操作日志统计
|
||||
func (r *OperationLogRepository) GetStatistics(ctx context.Context, startTime, endTime string) (*OperationLogStatistics, error) {
|
||||
stats := &OperationLogStatistics{}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.OperationLog{})
|
||||
if startTime != "" {
|
||||
query = query.Where("created_at >= ?", startTime)
|
||||
}
|
||||
if endTime != "" {
|
||||
query = query.Where("created_at <= ?", endTime)
|
||||
}
|
||||
|
||||
if err := query.Count(&stats.TotalCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := query.Where("status = ?", "success").Count(&stats.SuccessCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := query.Where("status = ?", "fail").Count(&stats.FailCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// LogFilter 日志过滤条件
|
||||
type LogFilter struct {
|
||||
UserID string
|
||||
Operation string
|
||||
TargetType string
|
||||
TargetID string
|
||||
Status string
|
||||
IP string
|
||||
StartTime string
|
||||
EndTime string
|
||||
}
|
||||
|
||||
// OperationLogStatistics 操作日志统计
|
||||
type OperationLogStatistics struct {
|
||||
TotalCount int64
|
||||
SuccessCount int64
|
||||
FailCount int64
|
||||
}
|
||||
Reference in New Issue
Block a user