ctop/sort.go

66 lines
2.0 KiB
Go
Raw Normal View History

2016-12-30 03:22:25 +00:00
package main
import (
"sort"
)
2017-01-10 21:56:49 +00:00
var SortFields = []string{"id", "name", "cpu", "mem", "mem %", "net"}
2017-01-03 17:37:09 +00:00
// Sort array of containers by field
func SortContainers(field string, containers []*Container) {
switch field {
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)))
2017-01-06 12:02:56 +00:00
case "mem %":
sort.Sort(sort.Reverse(ByMemPercent(containers)))
2017-01-10 21:56:49 +00:00
case "net":
sort.Sort(sort.Reverse(ByNet(containers)))
2017-01-03 17:37:09 +00:00
default:
sort.Sort(ByID(containers))
}
}
2016-12-30 22:17:46 +00:00
type ByID []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
func (a ByID) Len() int { return len(a) }
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 }
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
type ByName []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].name < a[j].name }
2016-12-30 03:22:25 +00:00
2016-12-30 22:17:46 +00:00
type ByCPU []*Container
2016-12-30 21:14:07 +00:00
2016-12-30 22:17:46 +00:00
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 }
2016-12-30 22:17:46 +00:00
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 }
2017-01-06 12:02:56 +00:00
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 }
2017-01-10 21:56:49 +00:00
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 }