Added UsageStat and UsageSplit filling for in use devices

pull/13/head
Paulo Truta 2021-06-07 16:41:41 +00:00
parent a4736c3b4c
commit 2d92131533
3 changed files with 76 additions and 6 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.15
require ( require (
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect 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/dustin/go-humanize v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-sql-driver/mysql v1.5.0 github.com/go-sql-driver/mysql v1.5.0

2
go.sum
View File

@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 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 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= 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.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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=

View File

@ -3,7 +3,8 @@ package storage
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"strconv" "os"
"path/filepath"
"strings" "strings"
"github.com/edgebox-iot/edgeboxctl/internal/utils" "github.com/edgebox-iot/edgeboxctl/internal/utils"
@ -23,6 +24,7 @@ type Device struct {
RO string `json:"ro"` RO string `json:"ro"`
Partitions []Partition `json:"partitions"` Partitions []Partition `json:"partitions"`
Status DeviceStatus `json:"status"` Status DeviceStatus `json:"status"`
UsageStat UsageStat `json:"usage_stat"`
} }
// DeviceStatus : Struct representing possible storage device statuses (code + description) // DeviceStatus : Struct representing possible storage device statuses (code + description)
@ -38,10 +40,18 @@ type MaybeDevice struct {
} }
type UsageStat struct { type UsageStat struct {
Total string `json:"total"` Total uint64 `json:"total"`
Used string `json:"used"` Used uint64 `json:"used"`
Free string `json:"free"` Free uint64 `json:"free"`
Percent string `json:"percent"` 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) // 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 { if device.InUse {
deviceUsageStat := UsageStat{}
for partitionIndex, partition := range device.Partitions { for partitionIndex, partition := range device.Partitions {
s, _ := disk.Usage(partition.Mountpoint) s, _ := disk.Usage(partition.Mountpoint)
@ -180,13 +192,68 @@ func getDevicesSpaceUsage(devices []Device) []Device {
} }
partitionUsagePercent := fmt.Sprintf("%2.f%%", s.UsedPercent) 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 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
}