mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
move containermap to own file
This commit is contained in:
parent
72b643d5d2
commit
adaa07fd4d
10
container.go
10
container.go
@ -14,16 +14,6 @@ type Container struct {
|
|||||||
reader *StatReader
|
reader *StatReader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(cid, names string) *Container {
|
|
||||||
return &Container{
|
|
||||||
id: cid,
|
|
||||||
done: make(chan bool),
|
|
||||||
stats: make(chan *docker.Stats),
|
|
||||||
widgets: NewWidgets(cid, names),
|
|
||||||
reader: &StatReader{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Container) Collect(client *docker.Client) {
|
func (c *Container) Collect(client *docker.Client) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
50
containermap.go
Normal file
50
containermap.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewContainerMap() *ContainerMap {
|
||||||
|
return &ContainerMap{
|
||||||
|
containers: make(map[string]*Container),
|
||||||
|
sortField: "cpu",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ContainerMap struct {
|
||||||
|
containers map[string]*Container
|
||||||
|
sortField string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return number of containers/rows
|
||||||
|
func (cm *ContainerMap) Len() uint {
|
||||||
|
return uint(len(cm.containers))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cm *ContainerMap) Add(c docker.APIContainers) {
|
||||||
|
id := c.ID[:12]
|
||||||
|
name := strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
||||||
|
cm.containers[id] = &Container{
|
||||||
|
id: id,
|
||||||
|
done: make(chan bool),
|
||||||
|
stats: make(chan *docker.Stats),
|
||||||
|
widgets: NewWidgets(cid, name),
|
||||||
|
reader: &StatReader{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a single container, by ID
|
||||||
|
func (cm *ContainerMap) Get(id string) *Container {
|
||||||
|
return cm.containers[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return array of all containers
|
||||||
|
func (cm *ContainerMap) All() []*Container {
|
||||||
|
var containers []*Container
|
||||||
|
for _, c := range cm.containers {
|
||||||
|
containers = append(containers, c)
|
||||||
|
}
|
||||||
|
return containers
|
||||||
|
}
|
80
sort.go
80
sort.go
@ -2,68 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
|
||||||
return &ContainerMap{
|
|
||||||
containers: make(map[string]*Container),
|
|
||||||
sortField: "cpu",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type ContainerMap struct {
|
|
||||||
containers map[string]*Container
|
|
||||||
sortField string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return number of containers/rows
|
|
||||||
func (cm *ContainerMap) Len() uint {
|
|
||||||
return uint(len(cm.containers))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cm *ContainerMap) Add(c docker.APIContainers) {
|
|
||||||
id := c.ID[:12]
|
|
||||||
name := strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
|
||||||
cm.containers[id] = NewContainer(id, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a single container, by ID
|
|
||||||
func (cm *ContainerMap) Get(id string) *Container {
|
|
||||||
return cm.containers[id]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return array of all containers
|
|
||||||
func (cm *ContainerMap) All() []*Container {
|
|
||||||
var containers []*Container
|
|
||||||
for _, c := range cm.containers {
|
|
||||||
containers = append(containers, c)
|
|
||||||
}
|
|
||||||
return containers
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return array of containers, sorted by field
|
|
||||||
func (cm *ContainerMap) Sorted() []*Container {
|
|
||||||
containers := cm.All()
|
|
||||||
|
|
||||||
switch cm.sortField {
|
|
||||||
case "id":
|
|
||||||
sort.Sort(ByID(containers))
|
|
||||||
case "name":
|
|
||||||
sort.Sort(ByName(containers))
|
|
||||||
case "cpu":
|
|
||||||
sort.Sort(sort.Reverse(ByCPU(containers)))
|
|
||||||
case "mem":
|
|
||||||
sort.Sort(sort.Reverse(ByMem(containers)))
|
|
||||||
default:
|
|
||||||
sort.Sort(ByID(containers))
|
|
||||||
}
|
|
||||||
|
|
||||||
return containers
|
|
||||||
}
|
|
||||||
|
|
||||||
type ByID []*Container
|
type ByID []*Container
|
||||||
|
|
||||||
func (a ByID) Len() int { return len(a) }
|
func (a ByID) Len() int { return len(a) }
|
||||||
@ -87,3 +27,23 @@ type ByMem []*Container
|
|||||||
func (a ByMem) Len() int { return len(a) }
|
func (a ByMem) Len() int { return len(a) }
|
||||||
func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
func (a ByMem) Less(i, j int) bool { return a[i].reader.MemUsage < a[j].reader.MemUsage }
|
func (a ByMem) Less(i, j int) bool { return a[i].reader.MemUsage < a[j].reader.MemUsage }
|
||||||
|
|
||||||
|
// Return array of containers, sorted by field
|
||||||
|
func (cm *ContainerMap) Sorted() []*Container {
|
||||||
|
containers := cm.All()
|
||||||
|
|
||||||
|
switch cm.sortField {
|
||||||
|
case "id":
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
case "name":
|
||||||
|
sort.Sort(ByName(containers))
|
||||||
|
case "cpu":
|
||||||
|
sort.Sort(sort.Reverse(ByCPU(containers)))
|
||||||
|
case "mem":
|
||||||
|
sort.Sort(sort.Reverse(ByMem(containers)))
|
||||||
|
default:
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
}
|
||||||
|
|
||||||
|
return containers
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user