mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor containers as array
This commit is contained in:
parent
7edde13a4d
commit
cf5eff40bb
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
type ContainerMap struct {
|
type ContainerMap struct {
|
||||||
client *docker.Client
|
client *docker.Client
|
||||||
containers map[string]*Container
|
containers Containers
|
||||||
collectors map[string]metrics.Collector
|
collectors map[string]metrics.Collector
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,10 +24,9 @@ func NewContainerMap() *ContainerMap {
|
|||||||
}
|
}
|
||||||
cm := &ContainerMap{
|
cm := &ContainerMap{
|
||||||
client: client,
|
client: client,
|
||||||
containers: make(map[string]*Container),
|
|
||||||
collectors: make(map[string]metrics.Collector),
|
collectors: make(map[string]metrics.Collector),
|
||||||
}
|
}
|
||||||
cm.Refresh()
|
//cm.Refresh()
|
||||||
return cm
|
return cm
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,24 +34,25 @@ func (cm *ContainerMap) Refresh() {
|
|||||||
var id, name string
|
var id, name string
|
||||||
|
|
||||||
opts := docker.ListContainersOptions{All: true}
|
opts := docker.ListContainersOptions{All: true}
|
||||||
containers, err := cm.client.ListContainers(opts)
|
allContainers, err := cm.client.ListContainers(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add new containers
|
// add new containers
|
||||||
states := make(map[string]string)
|
states := make(map[string]string)
|
||||||
for _, c := range containers {
|
for _, c := range allContainers {
|
||||||
id = c.ID[:12]
|
id = c.ID[:12]
|
||||||
states[id] = c.State
|
states[id] = c.State
|
||||||
|
|
||||||
if _, ok := cm.containers[id]; ok == false {
|
if _, ok := cm.Get(id); ok == false {
|
||||||
name = strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
name = strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
||||||
cm.containers[id] = &Container{
|
newc := &Container{
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
widgets: widgets.NewCompact(id, name),
|
widgets: widgets.NewCompact(id, name),
|
||||||
}
|
}
|
||||||
|
cm.containers = append(cm.containers, newc)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := cm.collectors[id]; ok == false {
|
if _, ok := cm.collectors[id]; ok == false {
|
||||||
@ -61,31 +61,26 @@ func (cm *ContainerMap) Refresh() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var removeIDs []string
|
var removeIdxs []int
|
||||||
var collectIDs []string
|
for n, c := range cm.containers {
|
||||||
for id, c := range cm.containers {
|
|
||||||
// mark stale internal containers
|
// mark stale internal containers
|
||||||
if _, ok := states[id]; ok == false {
|
if _, ok := states[c.id]; ok == false {
|
||||||
removeIDs = append(removeIDs, id)
|
removeIdxs = append(removeIdxs, n)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.SetState(states[id])
|
|
||||||
// start collector if needed
|
|
||||||
if c.state == "running" {
|
|
||||||
collectIDs = append(collectIDs, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, id := range collectIDs {
|
c.SetState(states[c.id])
|
||||||
if !cm.collectors[id].Running() {
|
// start collector if needed
|
||||||
cm.collectors[id].Start()
|
//collector := cm.collectors[id]
|
||||||
stream := cm.collectors[id].Stream()
|
if c.state == "running" && !cm.collectors[c.id].Running() {
|
||||||
cm.containers[id].Read(stream)
|
cm.collectors[c.id].Start()
|
||||||
|
c.Read(cm.collectors[c.id].Stream())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete removed containers
|
// delete removed containers
|
||||||
cm.Del(removeIDs...)
|
cm.Del(removeIdxs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kill a container by ID
|
// Kill a container by ID
|
||||||
@ -103,26 +98,24 @@ func (cm *ContainerMap) Len() uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get a single container, by ID
|
// Get a single container, by ID
|
||||||
func (cm *ContainerMap) Get(id string) *Container {
|
func (cm *ContainerMap) Get(id string) (*Container, bool) {
|
||||||
return cm.containers[id]
|
for _, c := range cm.containers {
|
||||||
|
if c.id == id {
|
||||||
|
return c, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove one or more containers
|
// Remove one or more containers by index
|
||||||
func (cm *ContainerMap) Del(ids ...string) {
|
func (cm *ContainerMap) Del(idx ...int) {
|
||||||
for _, id := range ids {
|
for _, i := range idx {
|
||||||
delete(cm.containers, id)
|
cm.containers = append(cm.containers[:i], cm.containers[i+1:]...)
|
||||||
delete(cm.collectors, 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
|
sort.Sort(cm.containers)
|
||||||
|
return cm.containers
|
||||||
for _, c := range cm.containers {
|
|
||||||
containers = append(containers, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Sort(containers)
|
|
||||||
return containers
|
|
||||||
}
|
}
|
||||||
|
5
grid.go
5
grid.go
@ -135,7 +135,7 @@ func (g *Grid) ExpandView() {
|
|||||||
ui.Clear()
|
ui.Clear()
|
||||||
ui.DefaultEvtStream.ResetHandlers()
|
ui.DefaultEvtStream.ResetHandlers()
|
||||||
defer ui.DefaultEvtStream.ResetHandlers()
|
defer ui.DefaultEvtStream.ResetHandlers()
|
||||||
container := g.cmap.Get(g.cursorID)
|
container, _ := g.cmap.Get(g.cursorID)
|
||||||
container.Expand()
|
container.Expand()
|
||||||
container.widgets.Render()
|
container.widgets.Render()
|
||||||
container.Collapse()
|
container.Collapse()
|
||||||
@ -158,6 +158,7 @@ func Display(g *Grid) bool {
|
|||||||
ui.DefaultEvtStream.Hook(logEvent)
|
ui.DefaultEvtStream.Hook(logEvent)
|
||||||
|
|
||||||
// initial draw
|
// initial draw
|
||||||
|
g.cmap.Refresh()
|
||||||
g.redrawRows()
|
g.redrawRows()
|
||||||
|
|
||||||
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
||||||
@ -170,6 +171,7 @@ func Display(g *Grid) bool {
|
|||||||
expand = true
|
expand = true
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
|
|
||||||
ui.Handle("/sys/kbd/a", func(ui.Event) {
|
ui.Handle("/sys/kbd/a", func(ui.Event) {
|
||||||
config.Toggle("allContainers")
|
config.Toggle("allContainers")
|
||||||
g.redrawRows()
|
g.redrawRows()
|
||||||
@ -196,6 +198,7 @@ func Display(g *Grid) bool {
|
|||||||
menu = SortMenu
|
menu = SortMenu
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
|
|
||||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||||
loopIter++
|
loopIter++
|
||||||
if loopIter%5 == 0 {
|
if loopIter%5 == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user