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:
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