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() {
defer close(stats)
defer close(c.stream)
for s := range stats {
c.ReadCPU(s)
c.ReadMem(s)

View File

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

View File

@ -36,6 +36,7 @@ type ContainerMap struct {
func (cm *ContainerMap) Refresh() {
var id, name string
opts := docker.ListContainersOptions{
Filters: filters,
}
@ -43,6 +44,8 @@ func (cm *ContainerMap) Refresh() {
if err != nil {
panic(err)
}
// add new containers
for _, c := range containers {
id = c.ID[:12]
if _, ok := cm.containers[id]; ok == false {
@ -50,13 +53,22 @@ func (cm *ContainerMap) Refresh() {
cm.containers[id] = &Container{
id: id,
name: name,
done: make(chan bool),
collect: collector.NewDocker(cm.client, id),
widgets: widgets.NewCompact(id, name),
}
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
@ -78,9 +90,17 @@ func (cm *ContainerMap) Get(id string) *Container {
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
func (cm *ContainerMap) All() []*Container {
var containers Containers
filter := GlobalConfig["filterStr"]
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))

View File

@ -129,6 +129,7 @@ func (g *Grid) ExpandView() {
func Display(g *Grid) bool {
var menu func()
var expand bool
var loopIter int
// calculate layout
ui.Body.Align()
@ -164,6 +165,10 @@ func Display(g *Grid) bool {
ui.StopLoop()
})
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.redrawRows()
})