From ca9ea17a81ce204bbaa0688500f6797381669bc2 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 25 Dec 2016 19:06:57 +0000 Subject: [PATCH] move helper methods to util --- container.go | 62 +--------------------------------------------------- grid.go | 11 ---------- util.go | 37 +++++++++++++++++++++++++++++++ widgets.go | 51 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 72 deletions(-) create mode 100644 util.go create mode 100644 widgets.go diff --git a/container.go b/container.go index 74ef6ac..1e37615 100644 --- a/container.go +++ b/container.go @@ -9,41 +9,11 @@ import ( ui "github.com/gizak/termui" ) -type Widgets struct { - cid *ui.Par - cpu *ui.Gauge - memory *ui.Gauge -} - -func NewWidgets(id string) *Widgets { - cid := ui.NewPar(id) - cid.Border = false - cid.Height = 1 - cid.Width = 10 - cid.TextFgColor = ui.ColorWhite - return &Widgets{cid, mkGauge(), mkGauge()} -} - -type CpuCalc struct { - lastCpu uint64 - lastSysCpu uint64 -} - -func (c *CpuCalc) Utilization(cpu uint64, syscpu uint64, ncpus int) int { - cpudiff := float64(cpu) - float64(c.lastCpu) - syscpudiff := float64(syscpu) - float64(c.lastSysCpu) - util := round((cpudiff / syscpudiff * 100) * float64(ncpus)) - c.lastCpu = cpu - c.lastSysCpu = syscpu - return util -} - type Container struct { id string widgets *Widgets stats chan *docker.Stats done chan bool - cpucalc *CpuCalc } func NewContainer(cid string) *Container { @@ -80,7 +50,7 @@ func (c *Container) Collect(client *docker.Client) { } func (c *Container) UpdateCPU(total uint64, system uint64, ncpus int) { - util := c.cpucalc.Utilization(total, system, ncpus) + util := c.widgets.cpucalc.Utilization(total, system, ncpus) c.widgets.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(util)) c.widgets.cpu.BarColor = colorScale(util) if util < 5 && util > 0 { @@ -97,33 +67,3 @@ func (c *Container) UpdateMem(cur uint64, limit uint64) { c.widgets.memory.Percent = percent c.widgets.memory.Label = fmt.Sprintf("%s / %s", byteFormat(cur), byteFormat(limit)) } - -func byteFormat(n uint64) string { - if n < 1024 { - return fmt.Sprintf("%sB", strconv.FormatUint(n, 10)) - } - if n < 1048576 { - n = n / 1024 - return fmt.Sprintf("%sK", strconv.FormatUint(n, 10)) - } - if n < 1073741824 { - n = n / 1048576 - return fmt.Sprintf("%sM", strconv.FormatUint(n, 10)) - } - n = n / 1024000000 - return fmt.Sprintf("%sG", strconv.FormatUint(n, 10)) -} - -func round(num float64) int { - return int(num + math.Copysign(0.5, num)) -} - -func colorScale(n int) ui.Attribute { - if n > 70 { - return ui.ColorRed - } - if n > 30 { - return ui.ColorYellow - } - return ui.ColorGreen -} diff --git a/grid.go b/grid.go index 26573b6..1d5f733 100644 --- a/grid.go +++ b/grid.go @@ -36,17 +36,6 @@ func (g *Grid) Rows() (rows []*ui.Row) { return rows } -func mkGauge() *ui.Gauge { - g := ui.NewGauge() - g.Height = 1 - g.Border = false - g.Percent = 0 - g.PaddingBottom = 0 - g.BarColor = ui.ColorGreen - g.Label = "-" - return g -} - func header() *ui.Row { //cid c1 := ui.NewPar(" CID") diff --git a/util.go b/util.go new file mode 100644 index 0000000..7965d7f --- /dev/null +++ b/util.go @@ -0,0 +1,37 @@ +package main + +import ( + "math" + + ui "github.com/gizak/termui" +) + +func byteFormat(n uint64) string { + if n < 1024 { + return fmt.Sprintf("%sB", strconv.FormatUint(n, 10)) + } + if n < 1048576 { + n = n / 1024 + return fmt.Sprintf("%sK", strconv.FormatUint(n, 10)) + } + if n < 1073741824 { + n = n / 1048576 + return fmt.Sprintf("%sM", strconv.FormatUint(n, 10)) + } + n = n / 1024000000 + return fmt.Sprintf("%sG", strconv.FormatUint(n, 10)) +} + +func round(num float64) int { + return int(num + math.Copysign(0.5, num)) +} + +func colorScale(n int) ui.Attribute { + if n > 70 { + return ui.ColorRed + } + if n > 30 { + return ui.ColorYellow + } + return ui.ColorGreen +} diff --git a/widgets.go b/widgets.go new file mode 100644 index 0000000..ced84c8 --- /dev/null +++ b/widgets.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "math" + "strconv" + + "github.com/fsouza/go-dockerclient" + ui "github.com/gizak/termui" +) + +type CpuCalc struct { + lastCpu uint64 + lastSysCpu uint64 +} + +func (c *CpuCalc) Utilization(cpu uint64, syscpu uint64, ncpus int) int { + cpudiff := float64(cpu) - float64(c.lastCpu) + syscpudiff := float64(syscpu) - float64(c.lastSysCpu) + util := round((cpudiff / syscpudiff * 100) * float64(ncpus)) + c.lastCpu = cpu + c.lastSysCpu = syscpu + return util +} + +type Widgets struct { + cid *ui.Par + cpu *ui.Gauge + memory *ui.Gauge + cpucalc *CpuCalc +} + +func NewWidgets(id string) *Widgets { + cid := ui.NewPar(id) + cid.Border = false + cid.Height = 1 + cid.Width = 10 + cid.TextFgColor = ui.ColorWhite + return &Widgets{cid, mkGauge(), mkGauge()} +} + +func mkGauge() *ui.Gauge { + g := ui.NewGauge() + g.Height = 1 + g.Border = false + g.Percent = 0 + g.PaddingBottom = 0 + g.BarColor = ui.ColorGreen + g.Label = "-" + return g +}