ctop/sort.go

38 lines
1.1 KiB
Go
Raw Normal View History

2016-12-30 03:22:25 +00:00
package main
import (
2017-02-07 03:33:09 +00:00
"github.com/bcicen/ctop/config"
2016-12-30 03:22:25 +00:00
)
2017-01-12 19:24:12 +00:00
type sortMethod func(c1, c2 *Container) bool
var Sorters = map[string]sortMethod{
"id": func(c1, c2 *Container) bool { return c1.id < c2.id },
"name": func(c1, c2 *Container) bool { return c1.name < c2.name },
"cpu": func(c1, c2 *Container) bool { return c1.metrics.CPUUtil < c2.metrics.CPUUtil },
"mem": func(c1, c2 *Container) bool { return c1.metrics.MemUsage < c2.metrics.MemUsage },
"mem %": func(c1, c2 *Container) bool { return c1.metrics.MemPercent < c2.metrics.MemPercent },
"net": func(c1, c2 *Container) bool { return sumNet(c1) < sumNet(c2) },
}
func SortFields() (fields []string) {
2017-01-12 19:24:12 +00:00
for k := range Sorters {
fields = append(fields, k)
2017-01-03 17:37:09 +00:00
}
return fields
2017-01-03 17:37:09 +00:00
}
2017-01-12 19:24:12 +00:00
type Containers []*Container
2016-12-30 22:17:46 +00:00
2017-01-12 19:24:12 +00:00
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 Containers) Less(i, j int) bool {
f := Sorters[config.GetVal("sortField")]
if config.GetSwitchVal("sortReversed") {
2017-01-12 19:48:29 +00:00
return f(a[j], a[i])
}
2017-01-12 19:24:12 +00:00
return f(a[i], a[j])
}
2017-01-10 21:56:49 +00:00
func sumNet(c *Container) int64 { return c.metrics.NetRx + c.metrics.NetTx }