Browse code

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>

Michael Crosby authored on 2015/01/22 06:42:31
Showing 1 changed files
... ...
@@ -2641,34 +2641,54 @@ func (s *containerStats) Collect(stream io.ReadCloser) {
2641 2641
 		previousSystem uint64
2642 2642
 		start          = true
2643 2643
 		dec            = json.NewDecoder(stream)
2644
+		u              = make(chan error, 1)
2644 2645
 	)
2646
+	go func() {
2647
+		for {
2648
+			var v *stats.Stats
2649
+			if err := dec.Decode(&v); err != nil {
2650
+				u <- err
2651
+				return
2652
+			}
2653
+			var (
2654
+				memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0
2655
+				cpuPercent = 0.0
2656
+			)
2657
+			if !start {
2658
+				cpuPercent = calcuateCpuPercent(previousCpu, previousSystem, v)
2659
+			}
2660
+			start = false
2661
+			s.mu.Lock()
2662
+			s.CpuPercentage = cpuPercent
2663
+			s.Memory = float64(v.MemoryStats.Usage)
2664
+			s.MemoryLimit = float64(v.MemoryStats.Limit)
2665
+			s.MemoryPercentage = memPercent
2666
+			s.NetworkRx = float64(v.Network.RxBytes)
2667
+			s.NetworkTx = float64(v.Network.TxBytes)
2668
+			s.mu.Unlock()
2669
+			previousCpu = v.CpuStats.CpuUsage.TotalUsage
2670
+			previousSystem = v.CpuStats.SystemUsage
2671
+			u <- nil
2672
+		}
2673
+	}()
2645 2674
 	for {
2646
-		var v *stats.Stats
2647
-		if err := dec.Decode(&v); err != nil {
2675
+		select {
2676
+		case <-time.After(2 * time.Second):
2677
+			// zero out the values if we have not received an update within
2678
+			// the specified duration.
2648 2679
 			s.mu.Lock()
2649
-			s.err = err
2680
+			s.CpuPercentage = 0
2681
+			s.Memory = 0
2682
+			s.MemoryPercentage = 0
2650 2683
 			s.mu.Unlock()
2651
-			return
2684
+		case err := <-u:
2685
+			if err != nil {
2686
+				s.mu.Lock()
2687
+				s.err = err
2688
+				s.mu.Unlock()
2689
+				return
2690
+			}
2652 2691
 		}
2653
-		var (
2654
-			memPercent = float64(v.MemoryStats.Usage) / float64(v.MemoryStats.Limit) * 100.0
2655
-			cpuPercent = 0.0
2656
-		)
2657
-		if !start {
2658
-			cpuPercent = calcuateCpuPercent(previousCpu, previousSystem, v)
2659
-		}
2660
-		start = false
2661
-		s.mu.Lock()
2662
-		s.CpuPercentage = cpuPercent
2663
-		s.Memory = float64(v.MemoryStats.Usage)
2664
-		s.MemoryLimit = float64(v.MemoryStats.Limit)
2665
-		s.MemoryPercentage = memPercent
2666
-		s.NetworkRx = float64(v.Network.RxBytes)
2667
-		s.NetworkTx = float64(v.Network.TxBytes)
2668
-		s.mu.Unlock()
2669
-
2670
-		previousCpu = v.CpuStats.CpuUsage.TotalUsage
2671
-		previousSystem = v.CpuStats.SystemUsage
2672 2692
 	}
2673 2693
 }
2674 2694