From 4ce36476704ff2e2e9468bf46bc5996683f8d419 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 9 Feb 2026 00:51:31 +0100 Subject: [PATCH] db status data for first page --- service/server.go | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/service/server.go b/service/server.go index 279a1ef..b10c29a 100644 --- a/service/server.go +++ b/service/server.go @@ -9,6 +9,8 @@ import ( "time" "github.com/alireza0/s-ui/config" + "github.com/alireza0/s-ui/database" + "github.com/alireza0/s-ui/database/model" "github.com/alireza0/s-ui/logger" "github.com/sagernet/sing-box/common/tls" @@ -40,10 +42,11 @@ func (s *ServerService) GetStatus(request string) *map[string]interface{} { case "net": status["net"] = s.GetNetInfo() case "sys": - status["uptime"] = s.GetUptime() status["sys"] = s.GetSystemInfo() case "sbd": status["sbd"] = s.GetSingboxInfo() + case "db": + status["db"] = s.GetDatabaseInfo() } } return &status @@ -59,16 +62,6 @@ func (s *ServerService) GetCpuPercent() float64 { } } -func (s *ServerService) GetUptime() uint64 { - upTime, err := host.Uptime() - if err != nil { - logger.Warning("get uptime failed:", err) - return 0 - } else { - return upTime - } -} - func (s *ServerService) GetMemInfo() map[string]interface{} { info := make(map[string]interface{}, 0) memInfo, err := mem.VirtualMemory() @@ -192,6 +185,7 @@ func (s *ServerService) GetSystemInfo() map[string]interface{} { } info["ipv4"] = ipv4 info["ipv6"] = ipv6 + info["bootTime"], _ = host.BootTime() return info } @@ -259,3 +253,31 @@ func (s *ServerService) generateWireGuardKey(pk string) []string { } return []string{"PrivateKey: " + wgKeys.String(), "PublicKey: " + wgKeys.PublicKey().String()} } + +func (s *ServerService) GetDatabaseInfo() map[string]int64 { + info := make(map[string]int64, 0) + db := database.GetDB() + if db == nil { + return nil + } + + var clientsCount, inboundsCount, outboundsCount, servicesCount, endpointsCount, clientUp, clientDown int64 + + db.Model(&model.Client{}).Count(&clientsCount) + db.Model(&model.Inbound{}).Count(&inboundsCount) + db.Model(&model.Outbound{}).Count(&outboundsCount) + db.Model(&model.Service{}).Count(&servicesCount) + db.Model(&model.Endpoint{}).Count(&endpointsCount) + db.Model(&model.Client{}).Select("COALESCE(SUM(up),0)").Scan(&clientUp) + db.Model(&model.Client{}).Select("COALESCE(SUM(down),0)").Scan(&clientDown) + + info["clients"] = clientsCount + info["inbounds"] = inboundsCount + info["outbounds"] = outboundsCount + info["services"] = servicesCount + info["endpoints"] = endpointsCount + info["clientUp"] = clientUp + info["clientDown"] = clientDown + + return info +}