Fix logic for resource usage -- include disk and reset to 0 when stopped

This commit is contained in:
Dane Everitt 2019-08-17 16:10:48 -07:00
parent c8e6e29abc
commit 9a4f1672f7
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 21 additions and 9 deletions

View File

@ -308,7 +308,10 @@ func (d *DockerEnvironment) EnableResourcePolling() error {
s.Resources.CpuAbsolute = s.Resources.CalculateAbsoluteCpu(pCpu, pSystem, &v.CPUStats) s.Resources.CpuAbsolute = s.Resources.CalculateAbsoluteCpu(pCpu, pSystem, &v.CPUStats)
s.Resources.Memory = v.MemoryStats.Usage s.Resources.Memory = v.MemoryStats.Usage
s.Resources.MemoryLimit = v.MemoryStats.Limit s.Resources.MemoryLimit = v.MemoryStats.Limit
s.Resources.Disk = 0
// Why you ask? This already has the logic for caching disk space in use and then
// also handles pushing that value to the resources object automatically.
s.Filesystem.HasSpaceAvailable()
for _, nw := range v.Networks { for _, nw := range v.Networks {
s.Resources.Network.RxBytes += nw.RxBytes s.Resources.Network.RxBytes += nw.RxBytes
@ -326,7 +329,14 @@ func (d *DockerEnvironment) DisableResourcePolling() error {
return nil return nil
} }
return d.stats.Close() err := d.stats.Close()
d.Server.Resources.CpuAbsolute = 0
d.Server.Resources.Memory = 0
d.Server.Resources.Network.TxBytes = 0
d.Server.Resources.Network.RxBytes = 0
return err
} }
// Creates a new container for the server using all of the data that is currently // Creates a new container for the server using all of the data that is currently

View File

@ -130,13 +130,15 @@ func (fs *Filesystem) HasSpaceAvailable() bool {
if size, err := fs.DirectorySize("/"); err != nil { if size, err := fs.DirectorySize("/"); err != nil {
zap.S().Warnw("failed to determine directory size", zap.String("server", fs.Server.Uuid), zap.Error(err)) zap.S().Warnw("failed to determine directory size", zap.String("server", fs.Server.Uuid), zap.Error(err))
} else { } else {
fs.Server.Cache.Set("disk_used", size, time.Minute*5) fs.Server.Cache.Set("disk_used", size, time.Second * 60)
} }
} }
// Determine if their folder size, in bytes, is smaller than the amount of space they've // Determine if their folder size, in bytes, is smaller than the amount of space they've
// been allocated. // been allocated.
return (size / 1024.0 / 1024.0) <= space fs.Server.Resources.Disk = size
return (size / 1000.0 / 1000.0) <= space
} }
// Determines the directory size of a given location by running parallel tasks to iterate // Determines the directory size of a given location by running parallel tasks to iterate

View File

@ -20,7 +20,7 @@ type ResourceUsage struct {
CpuAbsolute float64 `json:"cpu_absolute"` CpuAbsolute float64 `json:"cpu_absolute"`
// The current disk space being used by the server. This is cached to prevent slow lookup // The current disk space being used by the server. This is cached to prevent slow lookup
// issues on frequent refreshes. // issues on frequent refreshes.
Disk uint64 `json:"disk_bytes"` Disk int64 `json:"disk_bytes"`
// Current network transmit in & out for a container. // Current network transmit in & out for a container.
Network struct { Network struct {
RxBytes uint64 `json:"rx_bytes"` RxBytes uint64 `json:"rx_bytes"`
@ -47,5 +47,5 @@ func (ru *ResourceUsage) CalculateAbsoluteCpu(previousCpu, previousSystem float6
percent = (cpuDelta / systemDelta) * cpus * 100.0 percent = (cpuDelta / systemDelta) * cpus * 100.0
} }
return math.Round(percent * 1000) / 1000 return math.Round(percent*1000) / 1000
} }