Refactor Disk and Memory Size Formatting with Centralized Utility Function
- Extracted common size formatting logic to a new utility function `HumanSize` in utils package - Removed duplicate size formatting code from Disk and Memory configurations - Updated Disk and Memory modules to use the centralized size formatting utility - Uncommented and implemented disk usage calculation in Disk configuration - Improved code readability and maintainability by centralizing size conversion logic
This commit is contained in:
@@ -2,10 +2,11 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-wdd/log"
|
"agent-wdd/log"
|
||||||
|
"agent-wdd/utils"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -80,47 +81,31 @@ func DiskListGather() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (disk *Disk) calculateDiskUsage() {
|
func (disk *Disk) calculateDiskUsage() {
|
||||||
// var stat unix.Statfs_t
|
var stat unix.Statfs_t
|
||||||
// err := unix.Statfs(disk.Path, &stat)
|
err := unix.Statfs(disk.Path, &stat)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Error("disk syscall error: %v", err)
|
log.Error("disk syscall error: %v", err)
|
||||||
// disk.Size = "0B"
|
disk.Size = "0B"
|
||||||
// disk.Usage = "0B"
|
disk.Usage = "0B"
|
||||||
// disk.Percent = "0.00%"
|
disk.Percent = "0.00%"
|
||||||
// return
|
return
|
||||||
// }
|
|
||||||
|
|
||||||
// // 计算存储空间大小
|
|
||||||
// totalBytes := stat.Blocks * uint64(stat.Bsize)
|
|
||||||
// availBytes := stat.Bavail * uint64(stat.Bsize)
|
|
||||||
// usedBytes := totalBytes - availBytes
|
|
||||||
|
|
||||||
// // 格式化输出
|
|
||||||
// disk.Size = formatDiskSize(totalBytes)
|
|
||||||
// disk.Usage = formatDiskSize(usedBytes)
|
|
||||||
|
|
||||||
// if totalBytes == 0 {
|
|
||||||
// disk.Percent = "0.00%"
|
|
||||||
// } else {
|
|
||||||
// percent := float64(usedBytes) / float64(totalBytes) * 100
|
|
||||||
// disk.Percent = fmt.Sprintf("%.2f%%", percent)
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatDiskSize(bytes uint64) string {
|
|
||||||
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
|
||||||
var unitIndex int
|
|
||||||
size := float64(bytes)
|
|
||||||
|
|
||||||
for size >= 1000 && unitIndex < len(units)-1 {
|
|
||||||
size /= 1000
|
|
||||||
unitIndex++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if unitIndex == 0 {
|
// 计算存储空间大小
|
||||||
return strconv.FormatUint(bytes, 10) + units[unitIndex]
|
totalBytes := stat.Blocks * uint64(stat.Bsize)
|
||||||
|
availBytes := stat.Bavail * uint64(stat.Bsize)
|
||||||
|
usedBytes := totalBytes - availBytes
|
||||||
|
|
||||||
|
// 格式化输出
|
||||||
|
disk.Size = utils.HumanDiskSize(totalBytes)
|
||||||
|
disk.Usage = utils.HumanDiskSize(usedBytes)
|
||||||
|
|
||||||
|
if totalBytes == 0 {
|
||||||
|
disk.Percent = "0.00%"
|
||||||
|
} else {
|
||||||
|
percent := float64(usedBytes) / float64(totalBytes) * 100
|
||||||
|
disk.Percent = fmt.Sprintf("%.2f%%", percent)
|
||||||
}
|
}
|
||||||
return strconv.FormatFloat(size, 'f', 1, 64) + units[unitIndex]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DiskListSaveConfig() {
|
func DiskListSaveConfig() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-wdd/log"
|
"agent-wdd/log"
|
||||||
|
"agent-wdd/utils"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -27,7 +28,7 @@ func (mem *Memory) Gather() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
mem.Size = "0B"
|
mem.Size = "0B"
|
||||||
} else {
|
} else {
|
||||||
mem.Size = formatSize(kb * 1024)
|
mem.Size = utils.HumanSize(kb * 1024)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -127,26 +128,10 @@ func (swap *Swap) Gather() {
|
|||||||
swap.Size = "0B"
|
swap.Size = "0B"
|
||||||
} else {
|
} else {
|
||||||
swap.Open = true
|
swap.Open = true
|
||||||
swap.Size = formatSize(totalKB * 1024)
|
swap.Size = utils.HumanSize(totalKB * 1024)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatSize(bytes uint64) string {
|
|
||||||
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
|
||||||
var unitIndex int
|
|
||||||
size := float64(bytes)
|
|
||||||
|
|
||||||
for size >= 1024 && unitIndex < len(units)-1 {
|
|
||||||
size /= 1024
|
|
||||||
unitIndex++
|
|
||||||
}
|
|
||||||
|
|
||||||
if unitIndex == 0 {
|
|
||||||
return strconv.FormatUint(bytes, 10) + units[unitIndex]
|
|
||||||
}
|
|
||||||
return strconv.FormatFloat(size, 'f', 1, 64) + units[unitIndex]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (swap *Swap) SaveConfig() {
|
func (swap *Swap) SaveConfig() {
|
||||||
log.Info("Saving INFO => SWAP !")
|
log.Info("Saving INFO => SWAP !")
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ func downloadWithProgress(client *http.Client, url, dest string) (bool, string)
|
|||||||
size := resp.ContentLength
|
size := resp.ContentLength
|
||||||
var downloaded int64
|
var downloaded int64
|
||||||
|
|
||||||
|
// 打印下载信息
|
||||||
|
fmt.Printf("开始下载文件: %s 文件大小: %s\n", url, HumanSizeInt(size))
|
||||||
|
|
||||||
// 创建带进度跟踪的Reader
|
// 创建带进度跟踪的Reader
|
||||||
progressReader := &progressReader{
|
progressReader := &progressReader{
|
||||||
Reader: resp.Body,
|
Reader: resp.Body,
|
||||||
|
|||||||
40
agent-wdd/utils/FormatUtils.go
Normal file
40
agent-wdd/utils/FormatUtils.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func HumanSize(bytes uint64) string {
|
||||||
|
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
||||||
|
var unitIndex int
|
||||||
|
size := float64(bytes)
|
||||||
|
|
||||||
|
for size >= 1024 && unitIndex < len(units)-1 {
|
||||||
|
size /= 1024
|
||||||
|
unitIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if unitIndex == 0 {
|
||||||
|
return strconv.FormatUint(bytes, 10) + units[unitIndex]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strconv.FormatFloat(size, 'f', 1, 64) + units[unitIndex]
|
||||||
|
}
|
||||||
|
|
||||||
|
func HumanSizeInt(bytes int64) string {
|
||||||
|
return HumanSize(uint64(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func HumanDiskSize(bytes uint64) string {
|
||||||
|
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
||||||
|
var unitIndex int
|
||||||
|
size := float64(bytes)
|
||||||
|
|
||||||
|
for size >= 1000 && unitIndex < len(units)-1 {
|
||||||
|
size /= 1000
|
||||||
|
unitIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if unitIndex == 0 {
|
||||||
|
return strconv.FormatUint(bytes, 10) + units[unitIndex]
|
||||||
|
}
|
||||||
|
return strconv.FormatFloat(size, 'f', 1, 64) + units[unitIndex]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user