move helper methods to util

This commit is contained in:
Bradley Cicenas 2016-12-25 19:06:57 +00:00
parent 2a09ccc4a6
commit ca9ea17a81
4 changed files with 89 additions and 72 deletions

View File

@ -9,41 +9,11 @@ import (
ui "github.com/gizak/termui" 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 { type Container struct {
id string id string
widgets *Widgets widgets *Widgets
stats chan *docker.Stats stats chan *docker.Stats
done chan bool done chan bool
cpucalc *CpuCalc
} }
func NewContainer(cid string) *Container { 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) { 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.Label = fmt.Sprintf("%s%%", strconv.Itoa(util))
c.widgets.cpu.BarColor = colorScale(util) c.widgets.cpu.BarColor = colorScale(util)
if util < 5 && util > 0 { 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.Percent = percent
c.widgets.memory.Label = fmt.Sprintf("%s / %s", byteFormat(cur), byteFormat(limit)) 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
}

11
grid.go
View File

@ -36,17 +36,6 @@ func (g *Grid) Rows() (rows []*ui.Row) {
return rows 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 { func header() *ui.Row {
//cid //cid
c1 := ui.NewPar(" CID") c1 := ui.NewPar(" CID")

37
util.go Normal file
View File

@ -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
}

51
widgets.go Normal file
View File

@ -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
}