Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
cmd/swarm: add debug mode for progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
acud committed Sep 27, 2019
1 parent 8fcc78c commit a30e735
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions cmd/swarm/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ 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)
notrack = ctx.Bool(SwarmNoTrackUploadFlag.Name)
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 {
Expand Down Expand Up @@ -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()

Expand All @@ -223,42 +227,64 @@ 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 {
count, total, err := tag.Status(state.state)
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
Expand Down

0 comments on commit a30e735

Please sign in to comment.