Skip to content

Commit

Permalink
refactor: move stats calculator and container entity test.
Browse files Browse the repository at this point in the history
  • Loading branch information
arshamalh committed Apr 17, 2024
1 parent 134ceaa commit 08c3324
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
28 changes: 28 additions & 0 deletions entities/container_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
31 changes: 30 additions & 1 deletion entities/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type MemoryStats struct {
} `json:"stats"`
}

// TODO: Other fields should be implemented

type Stats struct {
Read time.Time
Preread time.Time
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions telegram/msgs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion telegram/msgs/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`
)
31 changes: 0 additions & 31 deletions tools/stats_calculator.go

This file was deleted.

0 comments on commit 08c3324

Please sign in to comment.