add interval refresh of running/killed containers in map

This commit is contained in:
Bradley Cicenas 2017-01-26 00:53:03 +00:00
parent 330ba8e188
commit 1705acf506
4 changed files with 30 additions and 2 deletions

View File

@ -33,6 +33,8 @@ func NewDocker(client *api.Client, id string) *Docker {
}() }()
go func() { go func() {
defer close(stats)
defer close(c.stream)
for s := range stats { for s := range stats {
c.ReadCPU(s) c.ReadCPU(s)
c.ReadMem(s) c.ReadMem(s)

View File

@ -8,7 +8,7 @@ import (
type Container struct { type Container struct {
id string id string
name string name string
done chan bool dead bool
metrics collector.Metrics metrics collector.Metrics
collect collector.Collector collect collector.Collector
widgets widgets.ContainerWidgets widgets widgets.ContainerWidgets
@ -30,5 +30,6 @@ func (c *Container) Collect() {
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent) c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
c.widgets.SetNet(metrics.NetRx, metrics.NetTx) c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
} }
c.dead = true
}() }()
} }

View File

@ -36,6 +36,7 @@ type ContainerMap struct {
func (cm *ContainerMap) Refresh() { func (cm *ContainerMap) Refresh() {
var id, name string var id, name string
opts := docker.ListContainersOptions{ opts := docker.ListContainersOptions{
Filters: filters, Filters: filters,
} }
@ -43,6 +44,8 @@ func (cm *ContainerMap) Refresh() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
// add new containers
for _, c := range containers { for _, c := range containers {
id = c.ID[:12] id = c.ID[:12]
if _, ok := cm.containers[id]; ok == false { if _, ok := cm.containers[id]; ok == false {
@ -50,13 +53,22 @@ func (cm *ContainerMap) Refresh() {
cm.containers[id] = &Container{ cm.containers[id] = &Container{
id: id, id: id,
name: name, name: name,
done: make(chan bool),
collect: collector.NewDocker(cm.client, id), collect: collector.NewDocker(cm.client, id),
widgets: widgets.NewCompact(id, name), widgets: widgets.NewCompact(id, name),
} }
cm.containers[id].Collect() cm.containers[id].Collect()
} }
} }
// remove dead containers
var removeIDs []string
for id, c := range cm.containers {
if c.dead {
removeIDs = append(removeIDs, id)
}
}
cm.Del(removeIDs...)
} }
// Kill a container by ID // Kill a container by ID
@ -78,9 +90,17 @@ func (cm *ContainerMap) Get(id string) *Container {
return cm.containers[id] return cm.containers[id]
} }
// Remove one or more containers
func (cm *ContainerMap) Del(ids ...string) {
for _, id := range ids {
delete(cm.containers, id)
}
}
// Return array of all containers, sorted by field // Return array of all containers, sorted by field
func (cm *ContainerMap) All() []*Container { func (cm *ContainerMap) All() []*Container {
var containers Containers var containers Containers
filter := GlobalConfig["filterStr"] filter := GlobalConfig["filterStr"]
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))

View File

@ -129,6 +129,7 @@ func (g *Grid) ExpandView() {
func Display(g *Grid) bool { func Display(g *Grid) bool {
var menu func() var menu func()
var expand bool var expand bool
var loopIter int
// calculate layout // calculate layout
ui.Body.Align() ui.Body.Align()
@ -164,6 +165,10 @@ func Display(g *Grid) bool {
ui.StopLoop() ui.StopLoop()
}) })
ui.Handle("/timer/1s", func(e ui.Event) { ui.Handle("/timer/1s", func(e ui.Event) {
loopIter++
if loopIter%5 == 0 {
g.cmap.Refresh()
}
g.containers = g.cmap.All() // refresh containers for current sort order g.containers = g.cmap.All() // refresh containers for current sort order
g.redrawRows() g.redrawRows()
}) })