ctop/container.go

60 lines
1.2 KiB
Go
Raw Normal View History

2016-12-22 16:15:22 +00:00
package main
import (
"strings"
"github.com/bcicen/ctop/widgets"
"github.com/fsouza/go-dockerclient"
2016-12-22 16:15:22 +00:00
)
type Container struct {
id string
name string
done chan bool
2016-12-25 22:39:16 +00:00
stats chan *docker.Stats
widgets widgets.ContainerWidgets
2016-12-25 22:39:16 +00:00
reader *StatReader
}
func NewContainer(c docker.APIContainers) *Container {
id := c.ID[:12]
name := strings.Replace(c.Names[0], "/", "", 1) // use primary container name
return &Container{
id: id,
name: name,
done: make(chan bool),
stats: make(chan *docker.Stats),
widgets: widgets.NewCompact(id, name),
reader: &StatReader{},
}
}
2017-01-06 19:46:30 +00:00
func (c *Container) Expand() {
c.widgets = widgets.NewExpanded(c.id, c.name)
}
func (c *Container) Collapse() {
c.widgets = widgets.NewCompact(c.id, c.name)
}
2017-01-06 19:46:30 +00:00
func (c *Container) Collect(client *docker.Client) {
go func() {
opts := docker.StatsOptions{
ID: c.id,
Stats: c.stats,
Stream: true,
Done: c.done,
}
client.Stats(opts)
}()
go func() {
for s := range c.stats {
2016-12-25 22:39:16 +00:00
c.reader.Read(s)
c.widgets.SetCPU(c.reader.CPUUtil)
2017-01-06 12:02:56 +00:00
c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit, c.reader.MemPercent)
2016-12-26 17:57:55 +00:00
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
}
}()
}