ctop/container/sort.go

133 lines
3.1 KiB
Go
Raw Normal View History

package container
2016-12-30 04:22:25 +01:00
import (
2017-02-20 00:23:17 +01:00
"fmt"
"regexp"
2017-06-12 15:40:52 +02:00
"sort"
2017-02-20 00:23:17 +01:00
2017-02-07 04:33:09 +01:00
"github.com/bcicen/ctop/config"
2016-12-30 04:22:25 +01:00
)
2017-01-12 20:24:12 +01:00
type sortMethod func(c1, c2 *Container) bool
var stateMap = map[string]int{
"running": 3,
"paused": 2,
"exited": 1,
"created": 0,
"": 0,
}
2017-03-03 08:57:26 +01:00
var idSorter = func(c1, c2 *Container) bool { return c1.Id < c2.Id }
var nameSorter = func(c1, c2 *Container) bool { return c1.GetMeta("name") < c2.GetMeta("name") }
2017-02-23 02:32:35 +01:00
2017-01-12 20:24:12 +01:00
var Sorters = map[string]sortMethod{
2017-02-23 02:32:35 +01:00
"id": idSorter,
"name": nameSorter,
"cpu": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
2017-03-03 08:57:26 +01:00
if c1.CPUUtil == c2.CPUUtil {
2017-02-23 02:32:35 +01:00
return nameSorter(c1, c2)
}
2017-03-03 08:57:26 +01:00
return c1.CPUUtil > c2.CPUUtil
2017-02-23 02:32:35 +01:00
},
"mem": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
2017-03-03 08:57:26 +01:00
if c1.MemUsage == c2.MemUsage {
2017-02-23 02:32:35 +01:00
return nameSorter(c1, c2)
}
2017-03-03 08:57:26 +01:00
return c1.MemUsage > c2.MemUsage
2017-02-23 02:32:35 +01:00
},
"mem %": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
2017-03-03 08:57:26 +01:00
if c1.MemPercent == c2.MemPercent {
2017-02-23 02:32:35 +01:00
return nameSorter(c1, c2)
}
2017-03-03 08:57:26 +01:00
return c1.MemPercent > c2.MemPercent
2017-02-23 02:32:35 +01:00
},
"net": func(c1, c2 *Container) bool {
sum1 := sumNet(c1)
sum2 := sumNet(c2)
// Use secondary sort method if equal values
2017-02-23 02:32:35 +01:00
if sum1 == sum2 {
return nameSorter(c1, c2)
}
return sum1 > sum2
2017-02-23 02:32:35 +01:00
},
2017-03-12 11:11:19 +01:00
"pids": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
if c1.Pids == c2.Pids {
return nameSorter(c1, c2)
}
return c1.Pids > c2.Pids
},
2017-03-12 02:35:40 +01:00
"io": func(c1, c2 *Container) bool {
sum1 := sumIO(c1)
sum2 := sumIO(c2)
// Use secondary sort method if equal values
if sum1 == sum2 {
return nameSorter(c1, c2)
}
return sum1 > sum2
},
2017-02-22 06:20:37 +01:00
"state": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
c1state := c1.GetMeta("state")
c2state := c2.GetMeta("state")
if c1state == c2state {
return nameSorter(c1, c2)
}
return stateMap[c1state] > stateMap[c2state]
2017-02-22 06:20:37 +01:00
},
2020-10-26 15:32:51 +01:00
"uptime": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
c1Uptime := c1.GetMeta("uptime")
c2Uptime := c2.GetMeta("uptime")
if c1Uptime == c2Uptime {
return nameSorter(c1, c2)
}
return c1Uptime > c2Uptime
},
2017-01-12 20:24:12 +01:00
}
func SortFields() (fields []string) {
2017-01-12 20:24:12 +01:00
for k := range Sorters {
fields = append(fields, k)
2017-01-03 18:37:09 +01:00
}
return fields
2017-01-03 18:37:09 +01:00
}
2017-01-12 20:24:12 +01:00
type Containers []*Container
2016-12-30 23:17:46 +01:00
2017-06-12 15:40:52 +02:00
func (a Containers) Sort() { sort.Sort(a) }
2017-01-12 20:24:12 +01: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 20:48:29 +01:00
return f(a[j], a[i])
}
2017-01-12 20:24:12 +01:00
return f(a[i], a[j])
}
2017-01-10 22:56:49 +01:00
2017-03-08 01:26:22 +01:00
func (a Containers) Filter() {
2017-02-20 00:23:17 +01:00
filter := config.GetVal("filterStr")
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
for _, c := range a {
c.Display = true
2017-02-20 00:23:17 +01:00
// Apply name filter
if re.FindAllString(c.GetMeta("name"), 1) == nil {
c.Display = false
2017-02-20 00:23:17 +01:00
}
// Apply state filter
if !config.GetSwitchVal("allContainers") && c.GetMeta("state") != "running" {
c.Display = false
2017-02-20 00:23:17 +01:00
}
}
}
2017-03-03 08:57:26 +01:00
func sumNet(c *Container) int64 { return c.NetRx + c.NetTx }
2017-03-12 02:35:40 +01:00
func sumIO(c *Container) int64 { return c.IOBytesRead + c.IOBytesWrite }