优化
This commit is contained in:
@@ -4,12 +4,14 @@ import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"kra/internal/service/dto"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/cpu"
|
||||
"github.com/shirou/gopsutil/v4/disk"
|
||||
"github.com/shirou/gopsutil/v4/mem"
|
||||
)
|
||||
|
||||
func (s *SystemConfigService) ServerInfo() (map[string]any, error) {
|
||||
func (s *SystemConfigService) ServerInfo() (*dto.ServerInfoResponse, error) {
|
||||
physicalCores, err := cpu.Counts(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -22,19 +24,19 @@ func (s *SystemConfigService) ServerInfo() (map[string]any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
diskInfo := make([]map[string]any, 0)
|
||||
diskInfo := make([]dto.ServerDiskResponse, 0)
|
||||
for _, mountPoint := range s.DiskMountPoints() {
|
||||
usage, usageErr := disk.Usage(mountPoint)
|
||||
if usageErr != nil {
|
||||
return nil, usageErr
|
||||
}
|
||||
diskInfo = append(diskInfo, map[string]any{"mountPoint": mountPoint, "usedMb": usage.Used / 1024 / 1024, "usedGb": usage.Used / 1024 / 1024 / 1024, "totalMb": usage.Total / 1024 / 1024, "totalGb": usage.Total / 1024 / 1024 / 1024, "usedPercent": int(usage.UsedPercent)})
|
||||
diskInfo = append(diskInfo, dto.ServerDiskResponse{MountPoint: mountPoint, UsedMB: usage.Used / 1024 / 1024, UsedGB: usage.Used / 1024 / 1024 / 1024, TotalMB: usage.Total / 1024 / 1024, TotalGB: usage.Total / 1024 / 1024 / 1024, UsedPercent: int(usage.UsedPercent)})
|
||||
}
|
||||
usedMB, totalMB := memory.Used/1024/1024, memory.Total/1024/1024
|
||||
return map[string]any{
|
||||
"os": map[string]any{"goos": runtime.GOOS, "numCpu": runtime.NumCPU(), "compiler": runtime.Compiler, "goVersion": runtime.Version(), "numGoroutine": runtime.NumGoroutine()},
|
||||
"cpu": map[string]any{"cpus": cpuPercent, "cores": physicalCores},
|
||||
"ram": map[string]any{"usedMb": usedMB, "totalMb": totalMB, "usedPercent": int(memory.UsedPercent)},
|
||||
"disk": diskInfo,
|
||||
return &dto.ServerInfoResponse{
|
||||
OS: dto.ServerOSResponse{GOOS: runtime.GOOS, NumCPU: runtime.NumCPU(), Compiler: runtime.Compiler, GoVersion: runtime.Version(), NumGoroutine: runtime.NumGoroutine()},
|
||||
CPU: dto.ServerCPUResponse{CPUs: cpuPercent, Cores: physicalCores},
|
||||
RAM: dto.ServerRAMResponse{UsedMB: usedMB, TotalMB: totalMB, UsedPercent: int(memory.UsedPercent)},
|
||||
Disk: diskInfo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user