ctop/container.go

67 lines
1.4 KiB
Go
Raw Normal View History

2016-12-22 16:15:22 +00:00
package main
import (
"strings"
"github.com/bcicen/ctop/cwidgets"
"github.com/bcicen/ctop/cwidgets/compact"
"github.com/bcicen/ctop/cwidgets/expanded"
2017-02-23 02:03:55 +00:00
"github.com/bcicen/ctop/metrics"
2016-12-22 16:15:22 +00:00
)
type Container struct {
id string
name string
2017-02-03 23:33:13 +00:00
state string
2017-02-23 02:03:55 +00:00
metrics metrics.Metrics
widgets cwidgets.ContainerWidgets
}
func NewContainer(id, name string) *Container {
c := &Container{
id: id,
name: name,
metrics: metrics.NewMetrics(),
}
c.widgets = compact.NewCompact(c.ShortID(), c.ShortName(), c.state)
return c
}
func (c *Container) ShortID() string {
return c.id[:12]
}
func (c *Container) ShortName() string {
return strings.Replace(c.name, "/", "", 1) // use primary container name
}
2017-01-06 19:46:30 +00:00
func (c *Container) Expand() {
c.widgets = expanded.NewExpanded(c.ShortID(), c.name)
2017-01-06 19:46:30 +00:00
}
2017-02-03 23:33:13 +00:00
func (c *Container) SetState(s string) {
c.state = s
c.widgets.SetStatus(s)
}
2017-02-24 09:10:14 +00:00
// Set metrics to zero state, clear widget gauges
func (c *Container) reset() {
c.metrics = metrics.Metrics{}
c.widgets.Reset()
}
// Read metric stream, updating widgets
2017-02-23 02:03:55 +00:00
func (c *Container) Read(stream chan metrics.Metrics) {
go func() {
for metrics := range stream {
c.metrics = metrics
c.widgets.SetCPU(metrics.CPUUtil)
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
}
log.Infof("reader stopped for container: %s", c.id)
2017-02-24 09:10:14 +00:00
c.reset()
}()
log.Infof("reader started for container: %s", c.id)
}