| package controller |
|
|
| import ( |
| "net/http" |
| "os" |
| "runtime" |
| "time" |
|
|
| "github.com/QuantumNous/new-api/common" |
| "github.com/gin-gonic/gin" |
| ) |
|
|
| |
| type PerformanceStats struct { |
| |
| CacheStats common.DiskCacheStats `json:"cache_stats"` |
| |
| MemoryStats MemoryStats `json:"memory_stats"` |
| |
| DiskCacheInfo DiskCacheInfo `json:"disk_cache_info"` |
| |
| DiskSpaceInfo common.DiskSpaceInfo `json:"disk_space_info"` |
| |
| Config PerformanceConfig `json:"config"` |
| } |
|
|
| |
| type MemoryStats struct { |
| |
| Alloc uint64 `json:"alloc"` |
| |
| TotalAlloc uint64 `json:"total_alloc"` |
| |
| Sys uint64 `json:"sys"` |
| |
| NumGC uint32 `json:"num_gc"` |
| |
| NumGoroutine int `json:"num_goroutine"` |
| } |
|
|
| |
| type DiskCacheInfo struct { |
| |
| Path string `json:"path"` |
| |
| Exists bool `json:"exists"` |
| |
| FileCount int `json:"file_count"` |
| |
| TotalSize int64 `json:"total_size"` |
| } |
|
|
| |
| type PerformanceConfig struct { |
| |
| DiskCacheEnabled bool `json:"disk_cache_enabled"` |
| |
| DiskCacheThresholdMB int `json:"disk_cache_threshold_mb"` |
| |
| DiskCacheMaxSizeMB int `json:"disk_cache_max_size_mb"` |
| |
| DiskCachePath string `json:"disk_cache_path"` |
| |
| IsRunningInContainer bool `json:"is_running_in_container"` |
|
|
| |
| MonitorEnabled bool `json:"monitor_enabled"` |
| |
| MonitorCPUThreshold int `json:"monitor_cpu_threshold"` |
| |
| MonitorMemoryThreshold int `json:"monitor_memory_threshold"` |
| |
| MonitorDiskThreshold int `json:"monitor_disk_threshold"` |
| } |
|
|
| |
| func GetPerformanceStats(c *gin.Context) { |
| |
| |
| cacheStats := common.GetDiskCacheStats() |
|
|
| |
| var memStats runtime.MemStats |
| runtime.ReadMemStats(&memStats) |
|
|
| |
| diskCacheInfo := getDiskCacheInfo() |
|
|
| |
| diskConfig := common.GetDiskCacheConfig() |
| monitorConfig := common.GetPerformanceMonitorConfig() |
| config := PerformanceConfig{ |
| DiskCacheEnabled: diskConfig.Enabled, |
| DiskCacheThresholdMB: diskConfig.ThresholdMB, |
| DiskCacheMaxSizeMB: diskConfig.MaxSizeMB, |
| DiskCachePath: diskConfig.Path, |
| IsRunningInContainer: common.IsRunningInContainer(), |
| MonitorEnabled: monitorConfig.Enabled, |
| MonitorCPUThreshold: monitorConfig.CPUThreshold, |
| MonitorMemoryThreshold: monitorConfig.MemoryThreshold, |
| MonitorDiskThreshold: monitorConfig.DiskThreshold, |
| } |
|
|
| |
| |
| systemStatus := common.GetSystemStatus() |
| diskSpaceInfo := common.DiskSpaceInfo{ |
| UsedPercent: systemStatus.DiskUsage, |
| } |
| |
| |
| |
| |
| diskSpaceInfo = common.GetDiskSpaceInfo() |
|
|
| stats := PerformanceStats{ |
| CacheStats: cacheStats, |
| MemoryStats: MemoryStats{ |
| Alloc: memStats.Alloc, |
| TotalAlloc: memStats.TotalAlloc, |
| Sys: memStats.Sys, |
| NumGC: memStats.NumGC, |
| NumGoroutine: runtime.NumGoroutine(), |
| }, |
| DiskCacheInfo: diskCacheInfo, |
| DiskSpaceInfo: diskSpaceInfo, |
| Config: config, |
| } |
|
|
| c.JSON(http.StatusOK, gin.H{ |
| "success": true, |
| "data": stats, |
| }) |
| } |
|
|
| |
| func ClearDiskCache(c *gin.Context) { |
| |
| |
| err := common.CleanupOldDiskCacheFiles(10 * time.Minute) |
| if err != nil { |
| common.ApiError(c, err) |
| return |
| } |
|
|
| c.JSON(http.StatusOK, gin.H{ |
| "success": true, |
| "message": "不活跃的磁盘缓存已清理", |
| }) |
| } |
|
|
| |
| func ResetPerformanceStats(c *gin.Context) { |
| common.ResetDiskCacheStats() |
|
|
| c.JSON(http.StatusOK, gin.H{ |
| "success": true, |
| "message": "统计信息已重置", |
| }) |
| } |
|
|
| |
| func ForceGC(c *gin.Context) { |
| runtime.GC() |
|
|
| c.JSON(http.StatusOK, gin.H{ |
| "success": true, |
| "message": "GC 已执行", |
| }) |
| } |
|
|
| |
| func getDiskCacheInfo() DiskCacheInfo { |
| |
| dir := common.GetDiskCacheDir() |
|
|
| info := DiskCacheInfo{ |
| Path: dir, |
| Exists: false, |
| } |
|
|
| entries, err := os.ReadDir(dir) |
| if err != nil { |
| return info |
| } |
|
|
| info.Exists = true |
| info.FileCount = 0 |
| info.TotalSize = 0 |
|
|
| for _, entry := range entries { |
| if entry.IsDir() { |
| continue |
| } |
| info.FileCount++ |
| if fileInfo, err := entry.Info(); err == nil { |
| info.TotalSize += fileInfo.Size() |
| } |
| } |
|
|
| return info |
| } |
|
|