ctop/container.go

74 lines
1.5 KiB
Go
Raw Normal View History

2016-12-22 16:15:22 +00:00
package main
import (
"github.com/bcicen/ctop/cwidgets/compact"
2017-02-23 02:03:55 +00:00
"github.com/bcicen/ctop/metrics"
2016-12-22 16:15:22 +00:00
)
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
Name string
State string
Meta map[string]string
Updates chan [2]string
Widgets *compact.Compact
collector metrics.Collector
}
func NewContainer(id string, collector metrics.Collector) *Container {
2017-03-03 07:57:26 +00:00
return &Container{
Metrics: metrics.NewMetrics(),
Id: id,
Meta: make(map[string]string),
Updates: make(chan [2]string),
Widgets: compact.NewCompact(id),
2017-03-03 07:57:26 +00:00
collector: collector,
}
}
2017-03-03 07:57:26 +00:00
func (c *Container) GetMeta(k string) string {
if v, ok := c.Meta[k]; ok {
return v
}
return ""
}
2017-03-03 07:57:26 +00:00
func (c *Container) SetMeta(k, v string) {
c.Meta[k] = v
c.Updates <- [2]string{k, v}
}
func (c *Container) SetName(n string) {
c.Name = n
c.Widgets.Name.Set(n)
}
2017-02-03 23:33:13 +00:00
func (c *Container) SetState(s string) {
2017-03-03 07:57:26 +00:00
c.State = s
c.Widgets.Status.Set(s)
// start collector, if needed
if c.State == "running" && !c.collector.Running() {
c.collector.Start()
c.Read(c.collector.Stream())
}
// stop collector, if needed
if c.State != "running" && c.collector.Running() {
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.Widgets.SetMetrics(metrics)
}
2017-03-03 07:57:26 +00:00
log.Infof("reader stopped for container: %s", c.Id)
c.Widgets.Reset()
}()
2017-03-03 07:57:26 +00:00
log.Infof("reader started for container: %s", c.Id)
}