ctop/container/container.go

82 lines
1.7 KiB
Go
Raw Normal View History

package container
2016-12-22 16:15:22 +00:00
import (
"github.com/bcicen/ctop/cwidgets"
"github.com/bcicen/ctop/cwidgets/compact"
"github.com/bcicen/ctop/logging"
2017-02-23 02:03:55 +00:00
"github.com/bcicen/ctop/metrics"
2016-12-22 16:15:22 +00:00
)
var (
log = logging.Init()
)
2017-03-03 07:57:26 +00:00
// Metrics and metadata representing a container
type Container struct {
2017-03-03 07:57:26 +00:00
metrics.Metrics
Id string
Meta map[string]string
Widgets *compact.Compact
Display bool // display this container in compact view
updater cwidgets.WidgetUpdater
2017-03-03 07:57:26 +00:00
collector metrics.Collector
}
func New(id string, collector metrics.Collector) *Container {
widgets := compact.NewCompact(id)
2017-03-03 07:57:26 +00:00
return &Container{
Metrics: metrics.NewMetrics(),
Id: id,
Meta: make(map[string]string),
Widgets: widgets,
updater: widgets,
2017-03-03 07:57:26 +00:00
collector: collector,
}
}
func (c *Container) SetUpdater(u cwidgets.WidgetUpdater) {
c.updater = u
for k, v := range c.Meta {
c.updater.SetMeta(k, v)
2017-03-03 07:57:26 +00:00
}
}
2017-03-03 07:57:26 +00:00
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 ""
}
2017-02-03 23:33:13 +00:00
func (c *Container) SetState(s string) {
c.SetMeta("state", s)
2017-03-03 07:57:26 +00:00
// start collector, if needed
if s == "running" && !c.collector.Running() {
2017-03-03 07:57:26 +00:00
c.collector.Start()
c.Read(c.collector.Stream())
}
// stop collector, if needed
if s != "running" && c.collector.Running() {
2017-03-03 07:57:26 +00:00
c.collector.Stop()
}
2017-02-24 09:10:14 +00:00
}
// 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 {
2017-03-03 07:57:26 +00:00
c.Metrics = metrics
c.updater.SetMetrics(metrics)
}
2017-03-03 07:57:26 +00:00
log.Infof("reader stopped for container: %s", c.Id)
2017-03-06 02:05:34 +00:00
c.Metrics = metrics.NewMetrics()
2017-03-03 07:57:26 +00:00
c.Widgets.Reset()
}()
2017-03-03 07:57:26 +00:00
log.Infof("reader started for container: %s", c.Id)
}