restructure container,connectors in subpackage

This commit is contained in:
Bradley Cicenas
2017-06-08 14:51:02 +00:00
parent 8fb7a8988f
commit b85ca680f0
9 changed files with 50 additions and 32 deletions

81
container/container.go Normal file
View File

@@ -0,0 +1,81 @@
package container
import (
"github.com/bcicen/ctop/cwidgets"
"github.com/bcicen/ctop/cwidgets/compact"
"github.com/bcicen/ctop/logging"
"github.com/bcicen/ctop/metrics"
)
var (
log = logging.Init()
)
// Metrics and metadata representing a container
type Container struct {
metrics.Metrics
Id string
Meta map[string]string
Widgets *compact.Compact
Display bool // display this container in compact view
updater cwidgets.WidgetUpdater
collector metrics.Collector
}
func New(id string, collector metrics.Collector) *Container {
widgets := compact.NewCompact(id)
return &Container{
Metrics: metrics.NewMetrics(),
Id: id,
Meta: make(map[string]string),
Widgets: widgets,
updater: widgets,
collector: collector,
}
}
func (c *Container) SetUpdater(u cwidgets.WidgetUpdater) {
c.updater = u
for k, v := range c.Meta {
c.updater.SetMeta(k, v)
}
}
func (c *Container) SetMeta(k, v string) {
c.Meta[k] = v
c.updater.SetMeta(k, v)
}
func (c *Container) GetMeta(k string) string {
if v, ok := c.Meta[k]; ok {
return v
}
return ""
}
func (c *Container) SetState(s string) {
c.SetMeta("state", s)
// start collector, if needed
if s == "running" && !c.collector.Running() {
c.collector.Start()
c.Read(c.collector.Stream())
}
// stop collector, if needed
if s != "running" && c.collector.Running() {
c.collector.Stop()
}
}
// Read metric stream, updating widgets
func (c *Container) Read(stream chan metrics.Metrics) {
go func() {
for metrics := range stream {
c.Metrics = metrics
c.updater.SetMetrics(metrics)
}
log.Infof("reader stopped for container: %s", c.Id)
c.Metrics = metrics.NewMetrics()
c.Widgets.Reset()
}()
log.Infof("reader started for container: %s", c.Id)
}

121
container/sort.go Normal file
View File

@@ -0,0 +1,121 @@
package container
import (
"fmt"
"regexp"
"github.com/bcicen/ctop/config"
)
type sortMethod func(c1, c2 *Container) bool
var stateMap = map[string]int{
"running": 3,
"paused": 2,
"exited": 1,
"created": 0,
"": 0,
}
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") }
var Sorters = map[string]sortMethod{
"id": idSorter,
"name": nameSorter,
"cpu": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
if c1.CPUUtil == c2.CPUUtil {
return nameSorter(c1, c2)
}
return c1.CPUUtil > c2.CPUUtil
},
"mem": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
if c1.MemUsage == c2.MemUsage {
return nameSorter(c1, c2)
}
return c1.MemUsage > c2.MemUsage
},
"mem %": func(c1, c2 *Container) bool {
// Use secondary sort method if equal values
if c1.MemPercent == c2.MemPercent {
return nameSorter(c1, c2)
}
return c1.MemPercent > c2.MemPercent
},
"net": func(c1, c2 *Container) bool {
sum1 := sumNet(c1)
sum2 := sumNet(c2)
// Use secondary sort method if equal values
if sum1 == sum2 {
return nameSorter(c1, c2)
}
return sum1 > sum2
},
"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
},
"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
},
"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]
},
}
func SortFields() (fields []string) {
for k := range Sorters {
fields = append(fields, k)
}
return fields
}
type Containers []*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 Containers) Less(i, j int) bool {
f := Sorters[config.GetVal("sortField")]
if config.GetSwitchVal("sortReversed") {
return f(a[j], a[i])
}
return f(a[i], a[j])
}
func (a Containers) Filter() {
filter := config.GetVal("filterStr")
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
for _, c := range a {
c.Display = true
// Apply name filter
if re.FindAllString(c.GetMeta("name"), 1) == nil {
c.Display = false
}
// Apply state filter
if !config.GetSwitchVal("allContainers") && c.GetMeta("state") != "running" {
c.Display = false
}
}
}
func sumNet(c *Container) int64 { return c.NetRx + c.NetTx }
func sumIO(c *Container) int64 { return c.IOBytesRead + c.IOBytesWrite }