diff --git a/container.go b/container.go index 74391d1..0e0369e 100644 --- a/container.go +++ b/container.go @@ -14,16 +14,6 @@ type Container struct { 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) { go func() { diff --git a/containermap.go b/containermap.go new file mode 100644 index 0000000..ffff2f4 --- /dev/null +++ b/containermap.go @@ -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 +} diff --git a/sort.go b/sort.go index d2aaa7a..7237d75 100644 --- a/sort.go +++ b/sort.go @@ -2,68 +2,8 @@ package main import ( "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 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) 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 } + +// 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 +}