package utils import ( "fmt" "html/template" "runtime" "strings" "time" "Moonshark/config" "Moonshark/metadata" ) // ComponentStats holds stats from various system components type ComponentStats struct { RouteCount int // Number of routes BytecodeBytes int64 // Total size of bytecode in bytes ModuleCount int // Number of loaded modules SessionStats map[string]uint64 // Session cache statistics } // SystemStats represents system statistics for debugging type SystemStats struct { Timestamp time.Time GoVersion string GoRoutines int Memory runtime.MemStats Components ComponentStats Version string Config *config.Config } // CollectSystemStats gathers basic system statistics func CollectSystemStats(cfg *config.Config) SystemStats { var stats SystemStats var mem runtime.MemStats stats.Timestamp = time.Now() stats.GoVersion = runtime.Version() stats.GoRoutines = runtime.NumGoroutine() stats.Version = metadata.Version stats.Config = cfg runtime.ReadMemStats(&mem) stats.Memory = mem return stats } // DebugStatsPage generates an HTML debug stats page func DebugStatsPage(stats SystemStats) string { const debugTemplate = `
| Version | {{.Version}} |
|---|
| Go Version | {{.GoVersion}} |
|---|---|
| Goroutines | {{.GoRoutines}} |
| Allocated | {{ByteCount .Memory.Alloc}} |
|---|---|
| Total Allocated | {{ByteCount .Memory.TotalAlloc}} |
| System Memory | {{ByteCount .Memory.Sys}} |
| GC Cycles | {{.Memory.NumGC}} |
| Active Sessions | {{index .Components.SessionStats "entries"}} |
|---|---|
| Cache Size | {{ByteCount (index .Components.SessionStats "bytes")}} |
| Max Cache Size | {{ByteCount (index .Components.SessionStats "max_bytes")}} |
| Cache Gets | {{index .Components.SessionStats "gets"}} |
| Cache Sets | {{index .Components.SessionStats "sets"}} |
| Cache Misses | {{index .Components.SessionStats "misses"}} |
| Interpreter | LuaJIT 2.1 (Lua 5.1) |
|---|---|
| Active Routes | {{.Components.RouteCount}} |
| Bytecode Size | {{ByteCount .Components.BytecodeBytes}} |
| Loaded Modules | {{.Components.ModuleCount}} |
| State Pool Size | {{.Config.Runner.PoolSize}} |
| Port | {{.Config.Server.Port}} |
|---|---|
| Pool Size | {{.Config.Runner.PoolSize}} |
| Debug Mode | {{.Config.Server.Debug}} |
| Log Level | {{.Config.Server.LogLevel}} |
| HTTP Logging | {{.Config.Server.HTTPLogging}} |
| Directories |
Routes: {{.Config.Dirs.Routes}}
Static: {{.Config.Dirs.Static}}
Override: {{.Config.Dirs.Override}}
Libs: {{range .Config.Dirs.Libs}}{{.}}, {{end}}
|