mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
add statreader
This commit is contained in:
38
container.go
38
container.go
@@ -2,27 +2,25 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
id string
|
||||
widgets *Widgets
|
||||
stats chan *docker.Stats
|
||||
done chan bool
|
||||
stats chan *docker.Stats
|
||||
widgets *Widgets
|
||||
reader *StatReader
|
||||
}
|
||||
|
||||
func NewContainer(cid string) *Container {
|
||||
return &Container{
|
||||
id: cid,
|
||||
widgets: NewWidgets(cid),
|
||||
stats: make(chan *docker.Stats),
|
||||
done: make(chan bool),
|
||||
cpucalc: &CpuCalc{},
|
||||
stats: make(chan *docker.Stats),
|
||||
widgets: NewWidgets(cid),
|
||||
reader: &StatReader{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,28 +40,10 @@ func (c *Container) Collect(client *docker.Client) {
|
||||
|
||||
go func() {
|
||||
for s := range c.stats {
|
||||
c.UpdateMem(s.MemoryStats.Usage, s.MemoryStats.Limit)
|
||||
c.UpdateCPU(s.CPUStats.CPUUsage.TotalUsage, s.CPUStats.SystemCPUUsage, len(s.CPUStats.CPUUsage.PercpuUsage))
|
||||
c.reader.Read(s)
|
||||
c.widgets.SetCPU(c.reader.CPUUtil)
|
||||
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
func (c *Container) UpdateCPU(total uint64, system uint64, ncpus int) {
|
||||
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 {
|
||||
util = 5
|
||||
}
|
||||
c.widgets.cpu.Percent = util
|
||||
}
|
||||
|
||||
func (c *Container) UpdateMem(cur uint64, limit uint64) {
|
||||
percent := round((float64(cur) / float64(limit)) * 100)
|
||||
if percent < 5 {
|
||||
percent = 5
|
||||
}
|
||||
c.widgets.memory.Percent = percent
|
||||
c.widgets.memory.Label = fmt.Sprintf("%s / %s", byteFormat(cur), byteFormat(limit))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user