mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor container sorting
This commit is contained in:
parent
fd0cab6b7c
commit
6856a7028b
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -66,10 +68,10 @@ func (cm *ContainerMap) Get(id string) *Container {
|
|||||||
|
|
||||||
// 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 []*Container
|
var containers Containers
|
||||||
for _, c := range cm.containers {
|
for _, c := range cm.containers {
|
||||||
containers = append(containers, c)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
SortContainers(GlobalConfig["sortField"], containers)
|
sort.Sort(containers)
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
2
menus.go
2
menus.go
@ -24,7 +24,7 @@ func HelpMenu() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SortMenu() {
|
func SortMenu() {
|
||||||
m := widgets.NewMenu(SortFields)
|
m := widgets.NewMenu(SortFields())
|
||||||
m.Selectable = true
|
m.Selectable = true
|
||||||
m.TextFgColor = ui.ColorWhite
|
m.TextFgColor = ui.ColorWhite
|
||||||
m.BorderLabel = "Sort Field"
|
m.BorderLabel = "Sort Field"
|
||||||
|
75
sort.go
75
sort.go
@ -4,62 +4,33 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
var SortFields = []string{"id", "name", "cpu", "mem", "mem %", "net"}
|
type sortMethod func(c1, c2 *Container) bool
|
||||||
|
|
||||||
// Sort array of containers by field
|
var Sorters = map[string]sortMethod{
|
||||||
func SortContainers(field string, containers []*Container) {
|
"id": func(c1, c2 *Container) bool { return c1.id < c2.id },
|
||||||
switch field {
|
"name": func(c1, c2 *Container) bool { return c1.name < c2.name },
|
||||||
case "id":
|
"cpu": func(c1, c2 *Container) bool { return c1.metrics.CPUUtil < c2.metrics.CPUUtil },
|
||||||
sort.Sort(ByID(containers))
|
"mem": func(c1, c2 *Container) bool { return c1.metrics.MemUsage < c2.metrics.MemUsage },
|
||||||
case "name":
|
"mem %": func(c1, c2 *Container) bool { return c1.metrics.MemPercent < c2.metrics.MemPercent },
|
||||||
sort.Sort(ByName(containers))
|
"net": func(c1, c2 *Container) bool { return sumNet(c1) < sumNet(c2) },
|
||||||
case "cpu":
|
|
||||||
sort.Sort(sort.Reverse(ByCPU(containers)))
|
|
||||||
case "mem":
|
|
||||||
sort.Sort(sort.Reverse(ByMem(containers)))
|
|
||||||
case "mem %":
|
|
||||||
sort.Sort(sort.Reverse(ByMemPercent(containers)))
|
|
||||||
case "net":
|
|
||||||
sort.Sort(sort.Reverse(ByNet(containers)))
|
|
||||||
default:
|
|
||||||
sort.Sort(ByID(containers))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ByID []*Container
|
func SortFields() []string {
|
||||||
|
a := sort.StringSlice{}
|
||||||
|
for k := range Sorters {
|
||||||
|
a = append(a, k)
|
||||||
|
}
|
||||||
|
sort.Sort(a)
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
func (a ByID) Len() int { return len(a) }
|
type Containers []*Container
|
||||||
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByID) Less(i, j int) bool { return a[i].id < a[j].id }
|
|
||||||
|
|
||||||
type ByName []*Container
|
func (a Containers) Len() int { return len(a) }
|
||||||
|
func (a Containers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
func (a ByName) Len() int { return len(a) }
|
func (a Containers) Less(i, j int) bool {
|
||||||
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
f := Sorters[GlobalConfig["sortField"]]
|
||||||
func (a ByName) Less(i, j int) bool { return a[i].name < a[j].name }
|
return f(a[i], a[j])
|
||||||
|
}
|
||||||
type ByCPU []*Container
|
|
||||||
|
|
||||||
func (a ByCPU) Len() int { return len(a) }
|
|
||||||
func (a ByCPU) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByCPU) Less(i, j int) bool { return a[i].metrics.CPUUtil < a[j].metrics.CPUUtil }
|
|
||||||
|
|
||||||
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].metrics.MemUsage < a[j].metrics.MemUsage }
|
|
||||||
|
|
||||||
type ByMemPercent []*Container
|
|
||||||
|
|
||||||
func (a ByMemPercent) Len() int { return len(a) }
|
|
||||||
func (a ByMemPercent) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByMemPercent) Less(i, j int) bool { return a[i].metrics.MemPercent < a[j].metrics.MemPercent }
|
|
||||||
|
|
||||||
type ByNet []*Container
|
|
||||||
|
|
||||||
func (a ByNet) Len() int { return len(a) }
|
|
||||||
func (a ByNet) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
func (a ByNet) Less(i, j int) bool { return sumNet(a[i]) < sumNet(a[j]) }
|
|
||||||
|
|
||||||
func sumNet(c *Container) int64 { return c.metrics.NetRx + c.metrics.NetTx }
|
func sumNet(c *Container) int64 { return c.metrics.NetRx + c.metrics.NetTx }
|
||||||
|
Loading…
Reference in New Issue
Block a user