重构:文件系统模块化架构,优化应用启动流程
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"go-desk/internal/common"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
@@ -72,19 +74,6 @@ func GetMemoryInfo() (map[string]interface{}, error) {
|
||||
return nil, fmt.Errorf("获取内存信息失败: %v", err)
|
||||
}
|
||||
|
||||
formatBytes := func(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"total": memInfo.Total,
|
||||
"used": memInfo.Used,
|
||||
@@ -92,10 +81,10 @@ func GetMemoryInfo() (map[string]interface{}, error) {
|
||||
"available": memInfo.Available,
|
||||
"usage": fmt.Sprintf("%.2f%%", memInfo.UsedPercent),
|
||||
"usage_raw": memInfo.UsedPercent,
|
||||
"total_str": formatBytes(memInfo.Total),
|
||||
"used_str": formatBytes(memInfo.Used),
|
||||
"free_str": formatBytes(memInfo.Free),
|
||||
"available_str": formatBytes(memInfo.Available),
|
||||
"total_str": common.FormatBytes(memInfo.Total),
|
||||
"used_str": common.FormatBytes(memInfo.Used),
|
||||
"free_str": common.FormatBytes(memInfo.Free),
|
||||
"available_str": common.FormatBytes(memInfo.Available),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -107,18 +96,6 @@ func GetDiskInfo() ([]map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
var result []map[string]interface{}
|
||||
formatBytes := func(bytes uint64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
for _, partition := range partitions {
|
||||
usage, err := disk.Usage(partition.Mountpoint)
|
||||
@@ -135,9 +112,9 @@ func GetDiskInfo() ([]map[string]interface{}, error) {
|
||||
"free": usage.Free,
|
||||
"usage": fmt.Sprintf("%.2f%%", usage.UsedPercent),
|
||||
"usage_raw": usage.UsedPercent,
|
||||
"total_str": formatBytes(usage.Total),
|
||||
"used_str": formatBytes(usage.Used),
|
||||
"free_str": formatBytes(usage.Free),
|
||||
"total_str": common.FormatBytes(usage.Total),
|
||||
"used_str": common.FormatBytes(usage.Used),
|
||||
"free_str": common.FormatBytes(usage.Free),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user