From 09c876c736ac9d3e04ced6793e724ed41911b9be Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Tue, 12 Mar 2024 15:59:15 +0100 Subject: [PATCH] Rename storage to readable as its ignored --- internal/readable/readable.go | 34 ++++++++++++++++++++++++++++++++++ services/system.services.go | 12 ++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 internal/readable/readable.go diff --git a/internal/readable/readable.go b/internal/readable/readable.go new file mode 100644 index 0000000..d9a1566 --- /dev/null +++ b/internal/readable/readable.go @@ -0,0 +1,34 @@ +package readable + +import "fmt" + +const ( + KiB uint64 = 1024 + MiB = KiB * 1024 + GiB = MiB * 1024 + TiB = GiB * 1024 + PiB = TiB * 1024 + EiB = PiB * 1024 +) + +func amountString(size uint64) (uint64, string) { + switch { + case size < MiB: + return KiB, "KiB" + case size < GiB: + return MiB, "MiB" + case size < TiB: + return GiB, "GiB" + case size < PiB: + return TiB, "TiB" + case size < EiB: + return PiB, "PiB" + default: + return EiB, "EiB" + } +} + +func ReadableSize(size uint64) string { + unit, unitStr := amountString(size) + return fmt.Sprintf("%.2f %s", float64(size)/float64(unit), unitStr) +} diff --git a/services/system.services.go b/services/system.services.go index ae77f0e..ab925fe 100644 --- a/services/system.services.go +++ b/services/system.services.go @@ -13,7 +13,7 @@ import ( "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/mem" - "gitlab.unjx.de/flohoss/godash/internal/storage" + "gitlab.unjx.de/flohoss/godash/internal/readable" ) func NewSystemService(sse *sse.Server) *SystemService { @@ -83,9 +83,9 @@ func staticRam() Ram { if err != nil { return result } - result.Total = storage.ReadableSize(r.Total) + result.Total = readable.ReadableSize(r.Total) if r.SwapTotal > 0 { - result.Swap = storage.ReadableSize(r.SwapTotal) + " swap" + result.Swap = readable.ReadableSize(r.SwapTotal) + " swap" } else { result.Swap = "no swap" } @@ -97,7 +97,7 @@ func (s *SystemService) liveRam() { if err != nil { return } - s.Live.Ram.Value = storage.ReadableSize(r.Used) + s.Live.Ram.Value = readable.ReadableSize(r.Used) s.Live.Ram.Percentage = math.RoundToEven(percent.PercentOfFloat(float64(r.Used), float64(r.Total))) } @@ -111,7 +111,7 @@ func staticDisk() Disk { if err != nil { return result } - result.Total = storage.ReadableSize(d.Total) + result.Total = readable.ReadableSize(d.Total) result.Partitions = strconv.Itoa(len(p)) + " partitions" return result } @@ -121,7 +121,7 @@ func (s *SystemService) liveDisk() { if err != nil { return } - s.Live.Disk.Value = storage.ReadableSize(d.Used) + s.Live.Disk.Value = readable.ReadableSize(d.Used) s.Live.Disk.Percentage = math.RoundToEven(percent.PercentOfFloat(float64(d.Used), float64(d.Total))) }