add mem% as sortable field

This commit is contained in:
Bradley Cicenas 2017-01-06 12:02:56 +00:00
parent d5e4782839
commit 6d63d09c83
5 changed files with 31 additions and 24 deletions

View File

@ -45,7 +45,7 @@ func (c *Container) Collect(client *docker.Client) {
for s := range c.stats {
c.reader.Read(s)
c.widgets.SetCPU(c.reader.CPUUtil)
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit)
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit, c.reader.MemPercent)
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
}
}()

View File

@ -1,16 +1,18 @@
package main
import (
"math"
"github.com/fsouza/go-dockerclient"
)
type StatReader struct {
CPUUtil float64
NetTx int64
NetRx int64
MemUsage int64
MemLimit int64
//MemPercent int64
CPUUtil int
NetTx int64
NetRx int64
MemLimit int64
MemPercent int
MemUsage int64
lastCpu float64
lastSysCpu float64
}
@ -28,7 +30,7 @@ func (s *StatReader) ReadCPU(stats *docker.Stats) {
cpudiff := total - s.lastCpu
syscpudiff := system - s.lastSysCpu
s.CPUUtil = (cpudiff / syscpudiff * 100) * ncpus
s.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
s.lastCpu = total
s.lastSysCpu = system
}
@ -36,7 +38,7 @@ func (s *StatReader) ReadCPU(stats *docker.Stats) {
func (s *StatReader) ReadMem(stats *docker.Stats) {
s.MemUsage = int64(stats.MemoryStats.Usage)
s.MemLimit = int64(stats.MemoryStats.Limit)
//s.MemPercent = round((float64(cur) / float64(limit)) * 100)
s.MemPercent = round((float64(s.MemUsage) / float64(s.MemLimit)) * 100)
}
func (s *StatReader) ReadNet(stats *docker.Stats) {
@ -46,3 +48,7 @@ func (s *StatReader) ReadNet(stats *docker.Stats) {
s.NetRx += int64(network.RxBytes)
}
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}

10
sort.go
View File

@ -4,7 +4,7 @@ import (
"sort"
)
var SortFields = []string{"id", "name", "cpu", "mem"}
var SortFields = []string{"id", "name", "cpu", "mem", "mem %"}
// Sort array of containers by field
func SortContainers(field string, containers []*Container) {
@ -17,6 +17,8 @@ func SortContainers(field string, containers []*Container) {
sort.Sort(sort.Reverse(ByCPU(containers)))
case "mem":
sort.Sort(sort.Reverse(ByMem(containers)))
case "mem %":
sort.Sort(sort.Reverse(ByMemPercent(containers)))
default:
sort.Sort(ByID(containers))
}
@ -45,3 +47,9 @@ type ByMem []*Container
func (a ByMem) Len() int { return len(a) }
func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMem) Less(i, j int) bool { return a[i].reader.MemUsage < a[j].reader.MemUsage }
type ByMemPercent []*Container
func (a ByMemPercent) Len() int { return len(a) }
func (a ByMemPercent) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMemPercent) Less(i, j int) bool { return a[i].reader.MemPercent < a[j].reader.MemPercent }

View File

@ -35,23 +35,21 @@ func (w *Compact) Row() *ui.Row {
)
}
func (w *Compact) SetCPU(val float64) {
intVal := round(val)
w.Cpu.BarColor = colorScale(intVal)
w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(intVal))
if intVal < 5 {
intVal = 5
func (w *Compact) SetCPU(val int) {
w.Cpu.BarColor = colorScale(val)
w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
if val < 5 {
val = 5
w.Cpu.BarColor = ui.ColorBlack
}
w.Cpu.Percent = intVal
w.Cpu.Percent = val
}
func (w *Compact) SetNet(rx int64, tx int64) {
w.Net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
}
func (w *Compact) SetMem(val int64, limit int64) {
percent := round((float64(val) / float64(limit)) * 100)
func (w *Compact) SetMem(val int64, limit int64, percent int) {
w.Memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
if percent < 5 {
percent = 5

View File

@ -2,7 +2,6 @@ package widgets
import (
"fmt"
"math"
"strconv"
ui "github.com/gizak/termui"
@ -30,10 +29,6 @@ func byteFormat(n int64) string {
return fmt.Sprintf("%sG", strconv.FormatInt(n, 10))
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func compactPar(s string) *ui.Par {
p := ui.NewPar(s)
p.Border = false