Zero out stats values in the cli

Based on some feedback, when you have a container via the cli that you
are monitoring for stats, if you stop the container it will stay in the
display but report the last datapoint that was received.

This PR changes the display to zero out the values for containers where
an update has not been received within a specified duration, i.e. 2
seconds.  This signals the user that the container has stopped as it
reports cpu and memory usage of 0.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-01-21 13:42:31 -08:00
parent 649ad2d56e
commit bbc38497b6
1 changed files with 43 additions and 23 deletions

View File

@ -2641,13 +2641,13 @@ func (s *containerStats) Collect(stream io.ReadCloser) {
previousSystem uint64
start = true
dec = json.NewDecoder(stream)
u = make(chan error, 1)
)
go func() {
for {
var v *stats.Stats
if err := dec.Decode(&v); err != nil {
s.mu.Lock()
s.err = err
s.mu.Unlock()
u <- err
return
}
var (
@ -2666,9 +2666,29 @@ func (s *containerStats) Collect(stream io.ReadCloser) {
s.NetworkRx = float64(v.Network.RxBytes)
s.NetworkTx = float64(v.Network.TxBytes)
s.mu.Unlock()
previousCpu = v.CpuStats.CpuUsage.TotalUsage
previousSystem = v.CpuStats.SystemUsage
u <- nil
}
}()
for {
select {
case <-time.After(2 * time.Second):
// zero out the values if we have not received an update within
// the specified duration.
s.mu.Lock()
s.CpuPercentage = 0
s.Memory = 0
s.MemoryPercentage = 0
s.mu.Unlock()
case err := <-u:
if err != nil {
s.mu.Lock()
s.err = err
s.mu.Unlock()
return
}
}
}
}