From a30e735d5edfe9023ba36e9e8a7146c70a76085f Mon Sep 17 00:00:00 2001 From: acud Date: Fri, 27 Sep 2019 21:58:48 +0530 Subject: [PATCH] cmd/swarm: add debug mode for progress bars --- cmd/swarm/upload.go | 62 ++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/cmd/swarm/upload.go b/cmd/swarm/upload.go index 224f1768d5..2455a909f0 100644 --- a/cmd/swarm/upload.go +++ b/cmd/swarm/upload.go @@ -73,6 +73,7 @@ func upload(ctx *cli.Context) { defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name) fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name) mimeType = ctx.GlobalString(SwarmUploadMimeType.Name) + debug = ctx.GlobalBool("debug") client = swarm.NewClient(bzzapi) toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name) toPin = ctx.Bool(SwarmPinFlag.Name) @@ -80,6 +81,9 @@ func upload(ctx *cli.Context) { autoDefaultPath = false file string ) + if !debug { + chunkStates = chunkStates[3:] // just poll Synced state + } if autoDefaultPathString := os.Getenv(SwarmAutoDefaultPath); autoDefaultPathString != "" { b, err := strconv.ParseBool(autoDefaultPathString) if err != nil { @@ -194,15 +198,15 @@ func upload(ctx *cli.Context) { seen, total, err := tag.Status(chunk.StateSeen) if total-seen > 0 { fmt.Println("Upload status:") - bars := createTagBars(tag) - pollTag(client, tag, bars) + bars := createTagBars(tag, debug) + pollTag(client, tag, bars, debug) } fmt.Println("Done! took", time.Since(tag.StartedAt)) fmt.Println("Your Swarm hash should now be retrievable from other nodes!") } -func pollTag(client *client.Client, tag *chunk.Tag, bars map[string]*mpb.Bar) { +func pollTag(client *client.Client, tag *chunk.Tag, bars map[string]*mpb.Bar, debug bool) { oldTag := *tag lastTime := time.Now() @@ -223,23 +227,23 @@ func pollTag(client *client.Client, tag *chunk.Tag, bars map[string]*mpb.Bar) { if err != nil { utils.Fatalf("error while getting tag status: %v", err) } - // increment individual bar d := int(newCount - count) - bars[state.name].IncrBy(d, time.Since(lastTime)) - // check if done if newCount != total { done = false } + bars[state.name].SetTotal(total, done) + bars[state.name].IncrBy(d, time.Since(lastTime)) } if done { return } + oldTag = *newTag lastTime = time.Now() } } -func createTagBars(tag *chunk.Tag) map[string]*mpb.Bar { +func createTagBars(tag *chunk.Tag, debug bool) map[string]*mpb.Bar { p := mpb.New(mpb.WithWidth(64)) bars := make(map[string]*mpb.Bar) for _, state := range chunkStates { @@ -247,18 +251,40 @@ func createTagBars(tag *chunk.Tag) map[string]*mpb.Bar { if err != nil { utils.Fatalf("could not get tag status: %v", err) } - barElement := p.AddBar(total, - mpb.PrependDecorators( - // align the elements with a constant size (10 chars) - decor.Name(state.name, decor.WC{W: 10, C: decor.DidentRight}), - // replace ETA decorator with "done" message, OnComplete event - decor.OnComplete( - // ETA decorator with ewma age of 60, and width reservation of 4 - decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 6}), "done", + title := state.name + var barElement *mpb.Bar + width := 10 + if debug { + barElement = p.AddBar(total, + mpb.PrependDecorators( + // align the elements with a constant size (10 chars) + decor.Name(title, decor.WC{W: width, C: decor.DidentRight}), + // add unit counts + decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60, and width reservation of 4 + decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 6}), "done", + ), ), - ), - mpb.AppendDecorators(decor.Percentage()), - ) + mpb.AppendDecorators(decor.Percentage()), + ) + } else { + title = fmt.Sprintf("Syncing %d chunks", total) + width = len(title) + 3 + barElement = p.AddBar(total, + mpb.PrependDecorators( + // align the elements with a constant size (10 chars) + decor.Name(title, decor.WC{W: width, C: decor.DidentRight}), + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + // ETA decorator with ewma age of 60, and width reservation of 4 + decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 6}), "done", + ), + ), + mpb.AppendDecorators(decor.Percentage()), + ) + } // increment the bar with the initial value from the tag barElement.IncrBy(int(count)) bars[state.name] = barElement