2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-24 01:18:59 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-02-23 02:03:55 +00:00
|
|
|
"github.com/bcicen/ctop/metrics"
|
2017-01-06 11:51:11 +00:00
|
|
|
"github.com/bcicen/ctop/widgets"
|
2016-12-22 16:15:22 +00:00
|
|
|
)
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
type Container struct {
|
|
|
|
id string
|
2017-01-01 22:42:13 +00:00
|
|
|
name string
|
2017-02-03 23:33:13 +00:00
|
|
|
state string
|
2017-02-23 02:03:55 +00:00
|
|
|
metrics metrics.Metrics
|
2017-01-06 12:59:45 +00:00
|
|
|
widgets widgets.ContainerWidgets
|
2017-01-06 11:51:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 01:18:59 +00:00
|
|
|
func NewContainer(id, name string) *Container {
|
|
|
|
c := &Container{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
}
|
2017-02-24 09:10:14 +00:00
|
|
|
c.widgets = widgets.NewCompact(c.ShortID(), c.ShortName(), c.state)
|
2017-02-24 01:18:59 +00:00
|
|
|
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() {
|
2017-02-24 09:10:14 +00:00
|
|
|
var curWidgets widgets.ContainerWidgets
|
2017-01-06 19:46:30 +00:00
|
|
|
|
2017-02-24 09:10:14 +00:00
|
|
|
curWidgets = c.widgets
|
2017-02-26 21:12:28 +00:00
|
|
|
c.widgets = widgets.NewExpanded(c.ShortID(), c.name)
|
2017-02-26 06:22:50 +00:00
|
|
|
c.widgets.Render(0, 0)
|
2017-02-24 09:10:14 +00:00
|
|
|
c.widgets = curWidgets
|
2017-01-06 19:46:30 +00:00
|
|
|
}
|
2016-12-22 22:04:21 +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()
|
|
|
|
}
|
|
|
|
|
2017-02-23 02:01:56 +00:00
|
|
|
// Read metric stream, updating widgets
|
2017-02-23 02:03:55 +00:00
|
|
|
func (c *Container) Read(stream chan metrics.Metrics) {
|
2016-12-22 22:04:21 +00:00
|
|
|
go func() {
|
2017-02-23 02:01:56 +00:00
|
|
|
for metrics := range stream {
|
2017-01-20 12:41:26 +00:00
|
|
|
c.metrics = metrics
|
2017-01-09 14:09:26 +00:00
|
|
|
c.widgets.SetCPU(metrics.CPUUtil)
|
|
|
|
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
|
|
|
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
2017-02-23 02:24:26 +00:00
|
|
|
log.Infof("reader stopped for container: %s", c.id)
|
2017-02-24 09:10:14 +00:00
|
|
|
c.reset()
|
2016-12-22 22:04:21 +00:00
|
|
|
}()
|
2017-02-23 02:24:26 +00:00
|
|
|
log.Infof("reader started for container: %s", c.id)
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|