优化结构

This commit is contained in:
Yvan
2026-08-28 09:19:54 +08:00
parent 03a4131f6e
commit 279692375b
57 changed files with 778 additions and 799 deletions
+9 -28
View File
@@ -1,42 +1,23 @@
package system
import (
"runtime"
"time"
"context"
"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() (*dto.ServerInfoResponse, error) {
physicalCores, err := cpu.Counts(false)
func (s *SystemConfigService) ServerInfo(ctx context.Context) (*dto.ServerInfoResponse, error) {
value, err := s.uc.ServerInfo(ctx)
if err != nil {
return nil, err
}
cpuPercent, err := cpu.Percent(200*time.Millisecond, true)
if err != nil {
return nil, err
diskInfo := make([]dto.ServerDiskResponse, 0, len(value.Disk))
for _, disk := range value.Disk {
diskInfo = append(diskInfo, dto.ServerDiskResponse{MountPoint: disk.MountPoint, UsedMB: disk.UsedMB, UsedGB: disk.UsedGB, TotalMB: disk.TotalMB, TotalGB: disk.TotalGB, UsedPercent: disk.UsedPercent})
}
memory, err := mem.VirtualMemory()
if err != nil {
return nil, err
}
diskInfo := make([]dto.ServerDiskResponse, 0)
for _, mountPoint := range s.DiskMountPoints() {
usage, usageErr := disk.Usage(mountPoint)
if usageErr != nil {
return nil, usageErr
}
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 &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)},
OS: dto.ServerOSResponse{GOOS: value.GOOS, NumCPU: value.NumCPU, Compiler: value.Compiler, GoVersion: value.GoVersion, NumGoroutine: value.NumGoroutine},
CPU: dto.ServerCPUResponse{CPUs: value.CPUs, Cores: value.Cores},
RAM: dto.ServerRAMResponse{UsedMB: value.UsedMB, TotalMB: value.TotalMB, UsedPercent: value.UsedPercent},
Disk: diskInfo,
}, nil
}