feat: implement ChatGPT owner demotion service with a FastAPI backend and a basic frontend, alongside updated project metadata.
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"codex-pool/internal/database"
|
||||
)
|
||||
|
||||
// LogEntry 日志条目
|
||||
@@ -17,7 +19,7 @@ type LogEntry struct {
|
||||
Module string `json:"module,omitempty"`
|
||||
}
|
||||
|
||||
// 日志存储
|
||||
// 日志存储(内存缓存 + 数据库持久化)
|
||||
var (
|
||||
logs = make([]LogEntry, 0, 1000)
|
||||
logsMu sync.RWMutex
|
||||
@@ -80,6 +82,7 @@ func log(level, message, email, module string) {
|
||||
Module: module,
|
||||
}
|
||||
|
||||
// 1. 内存缓存
|
||||
logsMu.Lock()
|
||||
if len(logs) >= 1000 {
|
||||
logs = logs[100:]
|
||||
@@ -87,8 +90,16 @@ func log(level, message, email, module string) {
|
||||
logs = append(logs, entry)
|
||||
logsMu.Unlock()
|
||||
|
||||
// 2. 广播给监听器
|
||||
broadcast(entry)
|
||||
|
||||
// 3. 持久化到数据库(异步,避免阻塞)
|
||||
go func() {
|
||||
if database.Instance != nil {
|
||||
database.Instance.InsertLog(entry.Timestamp, level, message, email, module)
|
||||
}
|
||||
}()
|
||||
|
||||
// 打印到控制台 (带时间戳和颜色)
|
||||
timestamp := entry.Timestamp.Format("15:04:05")
|
||||
|
||||
@@ -259,11 +270,16 @@ func GetLogsByModule(module string, page, pageSize int) ([]LogEntry, int) {
|
||||
return filtered[start:end], total
|
||||
}
|
||||
|
||||
// ClearLogs 清空日志
|
||||
// ClearLogs 清空日志(内存 + 数据库)
|
||||
func ClearLogs() {
|
||||
logsMu.Lock()
|
||||
defer logsMu.Unlock()
|
||||
logs = make([]LogEntry, 0, 1000)
|
||||
|
||||
// 同时清空数据库
|
||||
if database.Instance != nil {
|
||||
database.Instance.ClearLogs()
|
||||
}
|
||||
}
|
||||
|
||||
// GetLogsByModuleAndLevel 按模块和级别筛选日志并分页(最新的在前)
|
||||
@@ -303,12 +319,12 @@ func GetLogsByModuleAndLevel(module, level string, page, pageSize int) ([]LogEnt
|
||||
return filtered[start:end], total
|
||||
}
|
||||
|
||||
// ClearLogsByModule 按模块清除日志
|
||||
// ClearLogsByModule 按模块清除日志(内存 + 数据库)
|
||||
func ClearLogsByModule(module string) int {
|
||||
logsMu.Lock()
|
||||
defer logsMu.Unlock()
|
||||
|
||||
// 过滤掉指定模块的日志
|
||||
// 过滤掉指定模块的日志(内存)
|
||||
var newLogs []LogEntry
|
||||
cleared := 0
|
||||
for _, log := range logs {
|
||||
@@ -319,5 +335,14 @@ func ClearLogsByModule(module string) int {
|
||||
}
|
||||
}
|
||||
logs = newLogs
|
||||
|
||||
// 同时清空数据库
|
||||
if database.Instance != nil {
|
||||
dbCleared, _ := database.Instance.ClearLogsByModule(module)
|
||||
if dbCleared > int64(cleared) {
|
||||
cleared = int(dbCleared)
|
||||
}
|
||||
}
|
||||
|
||||
return cleared
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user