refactor: organize service dto contracts

This commit is contained in:
Yvan
2026-08-21 14:00:19 +08:00
parent e81a641953
commit d91f0554fd
65 changed files with 865 additions and 696 deletions
+9 -7
View File
@@ -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() (*ServerInfoResponse, 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() (*ServerInfoResponse, error) {
if err != nil {
return nil, err
}
diskInfo := make([]ServerDiskResponse, 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, 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)})
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 &ServerInfoResponse{
OS: ServerOSResponse{GOOS: runtime.GOOS, NumCPU: runtime.NumCPU(), Compiler: runtime.Compiler, GoVersion: runtime.Version(), NumGoroutine: runtime.NumGoroutine()},
CPU: ServerCPUResponse{CPUs: cpuPercent, Cores: physicalCores},
RAM: ServerRAMResponse{UsedMB: usedMB, TotalMB: totalMB, UsedPercent: int(memory.UsedPercent)},
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
}