From 08c3324dac9c991f16b761c3617a565e104920f7 Mon Sep 17 00:00:00 2001 From: Arsham Arya Date: Thu, 18 Apr 2024 02:22:03 +0330 Subject: [PATCH] refactor: move stats calculator and container entity test. --- entities/container_test.go | 28 ++++++++++++++++++++++++++++ entities/stats.go | 31 ++++++++++++++++++++++++++++++- telegram/msgs/format.go | 5 +++-- telegram/msgs/msgs.go | 2 +- tools/stats_calculator.go | 31 ------------------------------- 5 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 entities/container_test.go delete mode 100644 tools/stats_calculator.go diff --git a/entities/container_test.go b/entities/container_test.go new file mode 100644 index 0000000..5c45c32 --- /dev/null +++ b/entities/container_test.go @@ -0,0 +1,28 @@ +package entities_test + +import ( + "testing" + + "github.com/arshamalh/dockeroller/entities" + "github.com/stretchr/testify/assert" +) + +func TestContainer(t *testing.T) { + assert := assert.New(t) + newContainer := entities.Container{ + ID: "identified12", + Name: "Sweet Leonardo", + Image: "postgres:10.20-alpine3.15", + Status: "Exited with 0", + State: entities.ContainerStateCreated, + } + + assert.Equal( + "identified12 - Sweet Leonardo - Exited with 0 - image: postgres:10.20-alpine3.15", + newContainer.String(), + ) + + assert.Equal(false, newContainer.IsOn()) + newContainer.State = entities.ContainerStateRunning + assert.Equal(true, newContainer.IsOn()) +} diff --git a/entities/stats.go b/entities/stats.go index e8b858a..e2acb7c 100644 --- a/entities/stats.go +++ b/entities/stats.go @@ -20,6 +20,8 @@ type MemoryStats struct { } `json:"stats"` } +// TODO: Other fields should be implemented + type Stats struct { Read time.Time Preread time.Time @@ -29,4 +31,31 @@ type Stats struct { Memory MemoryStats `json:"memory_stats"` } -// TODO: Other fields should be implemented +// These are formulas to calculate stats +// cpu_delta = cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage +// system_cpu_delta = cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage +// number_of_cpu_cores = lenght(cpu_stats.cpu_usage.percpu_usage) or cpu_stats.online_cpus +// CPU usage % = (cpu_delta / system_cpu_delta) * number_of_cpu_cores * 100.0 +// +// CPU usage are based on the host, not VM, so in Windows devices, you may see different cpu usages in the Bot and Docker desktop and docker cli, +// but, that's calculable. both are correct anyway. +func (stat Stats) CPUUsage() float64 { + cpu_delta := stat.CPU.Usage.Total - stat.PerCPU.Usage.Total + system_cpu_delta := stat.CPU.SystemCPUUsage - stat.PerCPU.SystemCPUUsage + number_of_cpu_cores := stat.CPU.OnlineCPUs + if number_of_cpu_cores == 0 { + number_of_cpu_cores = len(stat.CPU.Usage.PerCPU) + } + cpu_usage := (float64(cpu_delta) / float64(system_cpu_delta)) * float64(number_of_cpu_cores) * 100 + return cpu_usage +} + +// Simplifying the memory usage calculations +// used_memory = memory_stats.usage - memory_stats.stats.cache +// available_memory = memory_stats.limit +// Memory usage % = (used_memory / available_memory) * 100.0 +func (stat Stats) MemoryUsage() float64 { + used_memory := stat.Memory.Usage - stat.Memory.Stats.Cache + memory_usage := (float64(used_memory) / float64(stat.Memory.Limit)) * 100 + return memory_usage +} diff --git a/telegram/msgs/format.go b/telegram/msgs/format.go index 21e5fd0..9f3054d 100644 --- a/telegram/msgs/format.go +++ b/telegram/msgs/format.go @@ -54,13 +54,14 @@ func FmtImage(image *entities.Image) string { } func FmtStats(stat entities.Stats) string { - cpu_usage, memory_usage_percent := tools.StatsCalculator(stat) + cpu_usage := stat.CPUUsage() + memory_usage_percent := stat.MemoryUsage() response := strings.NewReplacer( "{cpu_usage}", fmt.Sprintf("%.2f", cpu_usage), "{online_cpus}", fmt.Sprint(stat.CPU.OnlineCPUs), "{memory_usage}", tools.SizeToHumanReadable(stat.Memory.Usage), "{memory_usage%}", fmt.Sprintf("%.2f", memory_usage_percent), - "{avaiable_memory}", tools.SizeToHumanReadable(stat.Memory.Limit), + "{available_memory}", tools.SizeToHumanReadable(stat.Memory.Limit), ).Replace(Stat) response = FmtMono(response) return response diff --git a/telegram/msgs/msgs.go b/telegram/msgs/msgs.go index 11f8708..27e8905 100644 --- a/telegram/msgs/msgs.go +++ b/telegram/msgs/msgs.go @@ -42,6 +42,6 @@ Enter new image tag: ''CPU Usage: '' {cpu_usage} % ''Online CPUs: '' {online_cpus} ''Memory Usage: '' {memory_usage} ({memory_usage%} %) -''Avaiable Mem: '' {avaiable_memory} +''Available Mem: '' {available_memory} ` ) diff --git a/tools/stats_calculator.go b/tools/stats_calculator.go deleted file mode 100644 index bc1ff68..0000000 --- a/tools/stats_calculator.go +++ /dev/null @@ -1,31 +0,0 @@ -package tools - -import "github.com/arshamalh/dockeroller/entities" - -// These are formulas to calculate stats -// cpu_delta = cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage -// system_cpu_delta = cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage -// number_of_cpu_cores = lenght(cpu_stats.cpu_usage.percpu_usage) or cpu_stats.online_cpus -// CPU usage % = (cpu_delta / system_cpu_delta) * number_of_cpu_cores * 100.0 -// -// used_memory = memory_stats.usage - memory_stats.stats.cache -// available_memory = memory_stats.limit -// Memory usage % = (used_memory / available_memory) * 100.0 -// -// CPU usage are based on the host, not VM, so in Windows devices, you may see different cpu usages in the Bot and Docker desktop and docker cli, -// but, that's calculable. both are correct anyway. -func StatsCalculator(stat entities.Stats) (cpu_usage float64, memory_usage float64) { - // CPU usage calculation - cpu_delta := stat.CPU.Usage.Total - stat.PerCPU.Usage.Total - system_cpu_delta := stat.CPU.SystemCPUUsage - stat.PerCPU.SystemCPUUsage - number_of_cpu_cores := stat.CPU.OnlineCPUs - if number_of_cpu_cores == 0 { - number_of_cpu_cores = len(stat.CPU.Usage.PerCPU) - } - cpu_usage = (float64(cpu_delta) / float64(system_cpu_delta)) * float64(number_of_cpu_cores) * 100 - - // Memory usage calculation - used_memory := stat.Memory.Usage - stat.Memory.Stats.Cache - memory_usage = (float64(used_memory) / float64(stat.Memory.Limit)) * 100 - return -}