Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): show download speed and unpacking while pulling (#3563)

Hampusf98c6c13907b
diff --git a/fluxer_rexctl/cmd/lifecycle.go b/fluxer_rexctl/cmd/lifecycle.go
index 49dee9328..b22c91a26 100644
--- a/fluxer_rexctl/cmd/lifecycle.go
+++ b/fluxer_rexctl/cmd/lifecycle.go
@@ -286,12 +286,21 @@ type progress struct {
verb string
images map[string]bool
layers map[string][2]int64
+ states map[string]string
+ samples []byteSample
containers map[string]engine.Event
verbose bool
}
+type byteSample struct {
+ at time.Time
+ bytes int64
+}
+
+const rateWindow = 5 * time.Second
+
func (a *App) progress(s *ui.Step, verb string) func(engine.Event) {
- pr := &progress{app: a, step: s, verb: verb, images: map[string]bool{}, layers: map[string][2]int64{}, containers: map[string]engine.Event{}, verbose: a.UI.Verbose()}
+ pr := &progress{app: a, step: s, verb: verb, images: map[string]bool{}, layers: map[string][2]int64{}, states: map[string]string{}, containers: map[string]engine.Event{}, verbose: a.UI.Verbose()}
return pr.on
}
@@ -324,21 +333,75 @@ func (pr *progress) on(ev engine.Event) {
case ev.Kind == "container":
pr.containers[ev.ID] = ev
pr.step.Progress(0, 0)
+ pr.step.Rate(0)
pr.showContainers()
- case ev.Parent != "" && ev.Total > 0:
- pr.layers[ev.ID] = [2]int64{ev.Current, ev.Total}
- var cur, total int64
- for _, l := range pr.layers {
- cur, total = cur+l[0], total+l[1]
- }
- pr.step.Progress(cur, total)
+ case ev.Parent != "":
+ pr.states[ev.ID] = ev.Text
+ l, known := pr.layers[ev.ID]
+ switch {
+ case ev.Text == "Downloading" && ev.Total > 0:
+ pr.layers[ev.ID] = [2]int64{ev.Current, ev.Total}
+ case known && ev.Text != "Downloading":
+ pr.layers[ev.ID] = [2]int64{l[1], l[1]}
+ case !known && ev.Text != "Extracting" && ev.Total > 0:
+ pr.layers[ev.ID] = [2]int64{ev.Current, ev.Total}
+ }
+ pr.showPull()
case ev.Kind == "image" || ev.Parent == "" && ev.Total > 0:
pr.images[ev.ID] = pr.images[ev.ID] || done
- pr.step.Title(fmt.Sprintf("Downloading Fluxer, %d of %d images", count(pr.images), len(pr.images)))
if ev.Total > 0 {
+ pr.step.Title(pr.pullTitle())
pr.step.Progress(ev.Current, ev.Total)
+ return
}
+ pr.showPull()
+ }
+}
+
+func (pr *progress) pullTitle() string {
+ title := fmt.Sprintf("Downloading Fluxer, %d of %d images", count(pr.images), len(pr.images))
+ if pr.unpacking() {
+ title += ", unpacking"
+ }
+ return title
+}
+
+func (pr *progress) unpacking() bool {
+ extracting := false
+ for _, state := range pr.states {
+ switch state {
+ case "Pulling fs layer", "Waiting", "Downloading", "Verifying Checksum":
+ return false
+ case "Extracting":
+ extracting = true
+ }
+ }
+ return extracting
+}
+
+func (pr *progress) showPull() {
+ var cur, total int64
+ for _, l := range pr.layers {
+ cur, total = cur+l[0], total+l[1]
+ }
+ pr.step.Title(pr.pullTitle())
+ pr.step.Progress(cur, total)
+ if pr.unpacking() {
+ pr.samples = nil
+ pr.step.Rate(0)
+ return
+ }
+ now := time.Now()
+ pr.samples = append(pr.samples, byteSample{at: now, bytes: cur})
+ for len(pr.samples) > 1 && now.Sub(pr.samples[0].at) > rateWindow {
+ pr.samples = pr.samples[1:]
+ }
+ first := pr.samples[0]
+ if elapsed := now.Sub(first.at); elapsed >= time.Second && cur > first.bytes {
+ pr.step.Rate(int64(float64(cur-first.bytes) / elapsed.Seconds()))
+ return
}
+ pr.step.Rate(0)
}
func count(m map[string]bool) int {
diff --git a/fluxer_rexctl/internal/ui/step.go b/fluxer_rexctl/internal/ui/step.go
index 96022c56d..a66809498 100644
--- a/fluxer_rexctl/internal/ui/step.go
+++ b/fluxer_rexctl/internal/ui/step.go
@@ -26,6 +26,7 @@ type Step struct {
logs []string
cur int64
total int64
+ rate int64
waitFor string
since time.Time
announce bool
@@ -90,6 +91,12 @@ func (s *Step) Progress(current, total int64) {
s.mu.Unlock()
}
+func (s *Step) Rate(bytesPerSecond int64) {
+ s.mu.Lock()
+ s.rate = bytesPerSecond
+ s.mu.Unlock()
+}
+
func (s *Step) Logs() []string {
s.mu.Lock()
defer s.mu.Unlock()
@@ -103,10 +110,14 @@ func (s *Step) status() (string, string) {
if s.total <= 0 && s.cur <= 0 {
return title, ""
}
- if s.total <= 0 {
- return title, Bytes(s.cur)
+ progress := Bytes(s.cur)
+ if s.total > 0 {
+ progress += " of " + Bytes(s.total)
+ }
+ if s.rate > 0 {
+ progress += ", " + Bytes(s.rate) + "/s"
}
- return title, Bytes(s.cur) + " of " + Bytes(s.total)
+ return title, progress
}
func (s *Step) doneText() string {