diff --git a/metrics/docker.go b/connector/collector/docker.go similarity index 90% rename from metrics/docker.go rename to connector/collector/docker.go index 8f11b51..10e4425 100644 --- a/metrics/docker.go +++ b/connector/collector/docker.go @@ -1,16 +1,17 @@ -package metrics +package collector import ( + "github.com/bcicen/ctop/metrics" api "github.com/fsouza/go-dockerclient" ) // Docker collector type Docker struct { - Metrics + metrics.Metrics id string client *api.Client running bool - stream chan Metrics + stream chan metrics.Metrics done chan bool lastCpu float64 lastSysCpu float64 @@ -18,7 +19,7 @@ type Docker struct { func NewDocker(client *api.Client, id string) *Docker { return &Docker{ - Metrics: Metrics{}, + Metrics: metrics.Metrics{}, id: id, client: client, } @@ -26,7 +27,7 @@ func NewDocker(client *api.Client, id string) *Docker { func (c *Docker) Start() { c.done = make(chan bool) - c.stream = make(chan Metrics) + c.stream = make(chan metrics.Metrics) stats := make(chan *api.Stats) go func() { @@ -60,7 +61,7 @@ func (c *Docker) Running() bool { return c.running } -func (c *Docker) Stream() chan Metrics { +func (c *Docker) Stream() chan metrics.Metrics { return c.stream } diff --git a/connector/collector/main.go b/connector/collector/main.go new file mode 100644 index 0000000..c7b98be --- /dev/null +++ b/connector/collector/main.go @@ -0,0 +1,21 @@ +package collector + +import ( + "math" + + "github.com/bcicen/ctop/logging" +) + +var log = logging.Init() + +func round(num float64) int { + return int(num + math.Copysign(0.5, num)) +} + +// return rounded percentage +func percent(val float64, total float64) int { + if total <= 0 { + return 0 + } + return round((val / total) * 100) +} diff --git a/metrics/mock.go b/connector/collector/mock.go similarity index 81% rename from metrics/mock.go rename to connector/collector/mock.go index 0cb47fd..9d8dadd 100644 --- a/metrics/mock.go +++ b/connector/collector/mock.go @@ -1,16 +1,18 @@ // +build !release -package metrics +package collector import ( "math/rand" "time" + + "github.com/bcicen/ctop/metrics" ) // Mock collector type Mock struct { - Metrics - stream chan Metrics + metrics.Metrics + stream chan metrics.Metrics done bool running bool aggression int64 @@ -18,7 +20,7 @@ type Mock struct { func NewMock(a int64) *Mock { c := &Mock{ - Metrics: Metrics{}, + Metrics: metrics.Metrics{}, aggression: a, } c.MemLimit = 2147483648 @@ -31,7 +33,7 @@ func (c *Mock) Running() bool { func (c *Mock) Start() { c.done = false - c.stream = make(chan Metrics) + c.stream = make(chan metrics.Metrics) go c.run() } @@ -39,7 +41,7 @@ func (c *Mock) Stop() { c.done = true } -func (c *Mock) Stream() chan Metrics { +func (c *Mock) Stream() chan metrics.Metrics { return c.stream } diff --git a/metrics/proc.go b/connector/collector/proc.go similarity index 98% rename from metrics/proc.go rename to connector/collector/proc.go index 457585f..ce6aee5 100644 --- a/metrics/proc.go +++ b/connector/collector/proc.go @@ -1,4 +1,4 @@ -package metrics +package collector import ( linuxproc "github.com/c9s/goprocinfo/linux" diff --git a/metrics/runc.go b/connector/collector/runc.go similarity index 91% rename from metrics/runc.go rename to connector/collector/runc.go index 44188bb..b12d325 100644 --- a/metrics/runc.go +++ b/connector/collector/runc.go @@ -1,18 +1,19 @@ -package metrics +package collector import ( "time" + "github.com/bcicen/ctop/metrics" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/cgroups" ) // Runc collector type Runc struct { - Metrics + metrics.Metrics id string libc libcontainer.Container - stream chan Metrics + stream chan metrics.Metrics done bool running bool interval int // collection interval, in seconds @@ -22,7 +23,7 @@ type Runc struct { func NewRunc(libc libcontainer.Container) *Runc { c := &Runc{ - Metrics: Metrics{}, + Metrics: metrics.Metrics{}, id: libc.ID(), libc: libc, interval: 1, @@ -36,7 +37,7 @@ func (c *Runc) Running() bool { func (c *Runc) Start() { c.done = false - c.stream = make(chan Metrics) + c.stream = make(chan metrics.Metrics) go c.run() } @@ -44,7 +45,7 @@ func (c *Runc) Stop() { c.done = true } -func (c *Runc) Stream() chan Metrics { +func (c *Runc) Stream() chan metrics.Metrics { return c.stream } diff --git a/connector/docker.go b/connector/docker.go index fcbf9cd..3e7e5a4 100644 --- a/connector/docker.go +++ b/connector/docker.go @@ -6,8 +6,8 @@ import ( "strings" "sync" + "github.com/bcicen/ctop/connector/collector" "github.com/bcicen/ctop/container" - "github.com/bcicen/ctop/metrics" api "github.com/fsouza/go-dockerclient" ) @@ -128,7 +128,7 @@ func (cm *Docker) MustGet(id string) *container.Container { // append container struct for new containers if !ok { // create collector - collector := metrics.NewDocker(cm.client, id) + collector := collector.NewDocker(cm.client, id) // create container c = container.New(id, collector) cm.lock.Lock() diff --git a/connector/mock.go b/connector/mock.go index fdf4073..daecc3e 100644 --- a/connector/mock.go +++ b/connector/mock.go @@ -7,8 +7,8 @@ import ( "strings" "time" + "github.com/bcicen/ctop/connector/collector" "github.com/bcicen/ctop/container" - "github.com/bcicen/ctop/metrics" "github.com/jgautheron/codename-generator" "github.com/nu7hatch/gouuid" ) @@ -39,7 +39,7 @@ func (cs *Mock) Init() { } func (cs *Mock) makeContainer(aggression int64) { - collector := metrics.NewMock(aggression) + collector := collector.NewMock(aggression) c := container.New(makeID(), collector) c.SetMeta("name", makeName()) c.SetState(makeState()) diff --git a/connector/runc.go b/connector/runc.go index d67e103..b983571 100644 --- a/connector/runc.go +++ b/connector/runc.go @@ -8,8 +8,8 @@ import ( "sync" "time" + "github.com/bcicen/ctop/connector/collector" "github.com/bcicen/ctop/container" - "github.com/bcicen/ctop/metrics" "github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer/cgroups/systemd" ) @@ -170,7 +170,7 @@ func (cm *Runc) MustGet(id string) *container.Container { libc := cm.GetLibc(id) // create collector - collector := metrics.NewRunc(libc) + collector := collector.NewRunc(libc) // create container c = container.New(id, collector) diff --git a/metrics/main.go b/metrics/main.go index 6ed90ba..8048122 100644 --- a/metrics/main.go +++ b/metrics/main.go @@ -1,13 +1,5 @@ package metrics -import ( - "math" - - "github.com/bcicen/ctop/logging" -) - -var log = logging.Init() - type Metrics struct { CPUUtil int NetTx int64 @@ -39,15 +31,3 @@ type Collector interface { Start() Stop() } - -func round(num float64) int { - return int(num + math.Copysign(0.5, num)) -} - -// return rounded percentage -func percent(val float64, total float64) int { - if total <= 0 { - return 0 - } - return round((val / total) * 100) -}