From 2d92131533d694d89bb35a7a7386f8285eaf64fc Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Mon, 7 Jun 2021 16:41:41 +0000 Subject: [PATCH] Added UsageStat and UsageSplit filling for in use devices --- go.mod | 1 + go.sum | 2 + internal/storage/storage.go | 79 ++++++++++++++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index df74328..1645bc4 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect + github.com/dariubs/percent v0.0.0-20200128140941-b7801cf1c7e2 // indirect github.com/dustin/go-humanize v1.0.0 // indirect github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-sql-driver/mysql v1.5.0 diff --git a/go.sum b/go.sum index 85d8c22..c25bdef 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY= github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/dariubs/percent v0.0.0-20200128140941-b7801cf1c7e2 h1:5EPE4Uk7ucthLTJAZqZxu6LZluox5/AqXUxJDpzgJjg= +github.com/dariubs/percent v0.0.0-20200128140941-b7801cf1c7e2/go.mod h1:NDZpkezJ8QqyIW/510MywB5T2KdC8v/0oTlEoPcMsRM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e8c5f67..fa13afa 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -3,7 +3,8 @@ package storage import ( "bufio" "fmt" - "strconv" + "os" + "path/filepath" "strings" "github.com/edgebox-iot/edgeboxctl/internal/utils" @@ -23,6 +24,7 @@ type Device struct { RO string `json:"ro"` Partitions []Partition `json:"partitions"` Status DeviceStatus `json:"status"` + UsageStat UsageStat `json:"usage_stat"` } // DeviceStatus : Struct representing possible storage device statuses (code + description) @@ -38,10 +40,18 @@ type MaybeDevice struct { } type UsageStat struct { - Total string `json:"total"` - Used string `json:"used"` - Free string `json:"free"` - Percent string `json:"percent"` + Total uint64 `json:"total"` + Used uint64 `json:"used"` + Free uint64 `json:"free"` + Percent string `json:"percent"` + UsageSplit UsageSplit `json:"usage_split"` +} + +type UsageSplit struct { + OS uint64 `json:"os"` + EdgeApps uint64 `json:"edgeapps"` + Buckets uint64 `json:"buckets"` + Others uint64 `json:"others"` } // Partition : Struct representing a partition / filesystem (Empty Mountpoint means it is not mounted) @@ -172,6 +182,8 @@ func getDevicesSpaceUsage(devices []Device) []Device { if device.InUse { + deviceUsageStat := UsageStat{} + for partitionIndex, partition := range device.Partitions { s, _ := disk.Usage(partition.Mountpoint) @@ -180,13 +192,68 @@ func getDevicesSpaceUsage(devices []Device) []Device { } partitionUsagePercent := fmt.Sprintf("%2.f%%", s.UsedPercent) - devices[deviceIndex].Partitions[partitionIndex].UsageStat = UsageStat{Total: strconv.FormatUint(s.Total, 10), Used: strconv.FormatUint(s.Used, 10), Free: strconv.FormatUint(s.Free, 10), Percent: partitionUsagePercent} + + edgeappsDirSize, err := getDirSize(utils.GetPath("edgeAppsPath")) + // TODO for later: Figure out to get correct paths for each partition... + // wsAppDataDirSize, err := getDirSize("/home/system/components/ws/appdata") + + edgeappsUsageSplit := (uint64)(0) + if partition.Mountpoint == "/" && err == nil { + // edgeappsUsageSplit = edgeappsDirSize + wsAppDataDirSize + edgeappsUsageSplit = edgeappsDirSize + deviceUsageStat.UsageSplit.EdgeApps += edgeappsUsageSplit + } + + bucketsUsageSplit := (uint64)(0) + othersUsageSplit := (uint64)(0) + osUsageSplit := (s.Used - othersUsageSplit - bucketsUsageSplit - edgeappsUsageSplit) + + partitionUsageSplit := UsageSplit{ + OS: osUsageSplit, + EdgeApps: edgeappsUsageSplit, + Buckets: bucketsUsageSplit, + Others: othersUsageSplit, + } + + deviceUsageStat.Total = deviceUsageStat.Total + s.Total + deviceUsageStat.Used = deviceUsageStat.Used + s.Used + deviceUsageStat.Free = deviceUsageStat.Free + s.Free + deviceUsageStat.UsageSplit.OS = deviceUsageStat.UsageSplit.OS + osUsageSplit + deviceUsageStat.UsageSplit.EdgeApps = deviceUsageStat.UsageSplit.EdgeApps + edgeappsUsageSplit + deviceUsageStat.UsageSplit.Buckets = deviceUsageStat.UsageSplit.Buckets + bucketsUsageSplit + deviceUsageStat.UsageSplit.Others = deviceUsageStat.UsageSplit.Others + othersUsageSplit + + devices[deviceIndex].Partitions[partitionIndex].UsageStat = UsageStat{ + Total: s.Total, + Used: s.Used, + Free: s.Free, + Percent: partitionUsagePercent, + UsageSplit: partitionUsageSplit, + } } + devices[deviceIndex].UsageStat = deviceUsageStat + totalDevicePercentUsage := fmt.Sprintf("%2.f%%", (float32(devices[deviceIndex].UsageStat.Used)/float32(devices[deviceIndex].UsageStat.Total))*100) + devices[deviceIndex].UsageStat.Percent = totalDevicePercentUsage + } } return devices } + +func getDirSize(path string) (uint64, error) { + var size uint64 + err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + size += (uint64)(info.Size()) + } + return err + }) + return size, err +}