Skip to content

Progressbar writer #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pmtiles/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pmtiles

import (
"fmt"
"github.com/schollz/progressbar/v3"
"io"
"log"
"os"
Expand Down Expand Up @@ -41,7 +40,7 @@ func Cluster(logger *log.Logger, InputPMTiles string, deduplicate bool) error {
return err
}

bar := progressbar.Default(int64(header.TileEntriesCount))
bar := defaultProgressbar(logger, int64(header.TileEntriesCount))

err = IterateEntries(header,
func(offset uint64, length uint64) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions pmtiles/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

"github.com/RoaringBitmap/roaring/roaring64"
"github.com/schollz/progressbar/v3"
"zombiezen.com/go/sqlite"
)

Expand Down Expand Up @@ -190,7 +189,7 @@ func convertMbtiles(logger *log.Logger, input string, output string, deduplicate
logger.Println("Pass 2: writing tiles")
resolve := newResolver(deduplicate, header.TileType == Mvt)
{
bar := progressbar.Default(int64(tileset.GetCardinality()))
bar := defaultProgressbar(logger, int64(tileset.GetCardinality()))
i := tileset.Iterator()
stmt := conn.Prep("SELECT tile_data FROM tiles WHERE zoom_level = ? AND tile_column = ? AND tile_row = ?")

Expand Down
6 changes: 3 additions & 3 deletions pmtiles/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/schollz/progressbar/v3"
"io"
"io/ioutil"
"log"
Expand All @@ -13,7 +12,7 @@ import (

// Edit parts of the header or metadata.
// works in-place if only the header is modified.
func Edit(_ *log.Logger, inputArchive string, newHeaderJSONFile string, newMetadataFile string) error {
func Edit(logger *log.Logger, inputArchive string, newHeaderJSONFile string, newMetadataFile string) error {
if newHeaderJSONFile == "" && newMetadataFile == "" {
return fmt.Errorf("must supply --header-json and/or --metadata to edit")
}
Expand Down Expand Up @@ -110,7 +109,8 @@ func Edit(_ *log.Logger, inputArchive string, newHeaderJSONFile string, newMetad
newHeader.LeafDirectoryOffset = newHeader.MetadataOffset + newHeader.MetadataLength
newHeader.TileDataOffset = newHeader.LeafDirectoryOffset + newHeader.LeafDirectoryLength

bar := progressbar.DefaultBytes(
bar := defaultBytesProgressbar(
logger,
int64(HeaderV3LenBytes+newHeader.RootLength+uint64(len(metadataBytes))+newHeader.LeafDirectoryLength+newHeader.TileDataLength),
"writing file",
)
Expand Down
6 changes: 3 additions & 3 deletions pmtiles/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/dustin/go-humanize"
"github.com/paulmach/orb"
"github.com/schollz/progressbar/v3"
"golang.org/x/sync/errgroup"
"io"
"io/ioutil"
Expand Down Expand Up @@ -249,7 +248,7 @@ func MergeRanges(ranges []srcDstRange, overfetch float32) (*list.List, uint64) {
// 9. get and write the metadata.
// 10. write the leaf directories (if any)
// 11. Get all tiles, and write directly to the output.
func Extract(_ *log.Logger, bucketURL string, key string, minzoom int8, maxzoom int8, regionFile string, bbox string, output string, downloadThreads int, overfetch float32, dryRun bool) error {
func Extract(logger *log.Logger, bucketURL string, key string, minzoom int8, maxzoom int8, regionFile string, bbox string, output string, downloadThreads int, overfetch float32, dryRun bool) error {
// 1. fetch the header
start := time.Now()
ctx := context.Background()
Expand Down Expand Up @@ -491,7 +490,8 @@ func Extract(_ *log.Logger, bucketURL string, key string, minzoom int8, maxzoom
return err
}

bar := progressbar.DefaultBytes(
bar := defaultBytesProgressbar(
logger,
int64(totalBytes),
"fetching chunks",
)
Expand Down
4 changes: 2 additions & 2 deletions pmtiles/makesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"github.com/cespare/xxhash/v2"
"github.com/schollz/progressbar/v3"
"golang.org/x/sync/errgroup"
"io"
"log"
Expand Down Expand Up @@ -106,7 +105,8 @@ func Makesync(logger *log.Logger, cliVersion string, fileName string, blockSizeK

defer output.Close()

bar := progressbar.Default(
bar := defaultProgressbar(
logger,
int64(header.TileEntriesCount),
"writing syncfile",
)
Expand Down
52 changes: 52 additions & 0 deletions pmtiles/progressbar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pmtiles

import (
"fmt"
"github.com/schollz/progressbar/v3"
"log"
"time"
)

func defaultProgressbar(log *log.Logger, max int64, description ...string) *progressbar.ProgressBar {

Check warning on line 10 in pmtiles/progressbar.go

View workflow job for this annotation

GitHub Actions / fmt_vet_lint

redefinition of the built-in function max
desc := ""
if len(description) > 0 {
desc = description[0]
}
return progressbar.NewOptions64(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to minimize the amount of configuration here, sticking to the defaults like before?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The progressbar interface isn't that flexible.

max,
progressbar.OptionSetDescription(desc),
progressbar.OptionSetWriter(log.Writer()),
progressbar.OptionSetWidth(10),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionOnCompletion(func() {
fmt.Fprint(log.Writer(), "\n")
}),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
progressbar.OptionSetRenderBlankState(true),
)
}

func defaultBytesProgressbar(log *log.Logger, maxBytes int64, description ...string) *progressbar.ProgressBar {
desc := ""
if len(description) > 0 {
desc = description[0]
}
return progressbar.NewOptions64(
maxBytes,
progressbar.OptionSetDescription(desc),
progressbar.OptionSetWriter(log.Writer()),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(10),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() {
fmt.Fprint(log.Writer(), "\n")
}),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
progressbar.OptionSetRenderBlankState(true),
)
}
16 changes: 10 additions & 6 deletions pmtiles/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"github.com/cespare/xxhash/v2"
"github.com/dustin/go-humanize"
"github.com/schollz/progressbar/v3"
"golang.org/x/sync/errgroup"
"io"
"log"
Expand Down Expand Up @@ -85,7 +84,8 @@ func Sync(logger *log.Logger, oldVersion string, newVersion string, dryRun bool)
if err != nil {
return err
}
bar := progressbar.DefaultBytes(
bar := defaultBytesProgressbar(
logger,
resp.ContentLength,
"downloading syncfile",
)
Expand Down Expand Up @@ -122,7 +122,8 @@ func Sync(logger *log.Logger, oldVersion string, newVersion string, dryRun bool)
return fmt.Errorf("archive must be clustered for sync")
}

bar = progressbar.Default(
bar = defaultProgressbar(
logger,
int64(len(blocks)),
"calculating diff",
)
Expand Down Expand Up @@ -282,15 +283,17 @@ func Sync(logger *log.Logger, oldVersion string, newVersion string, dryRun bool)
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", newHeader.LeafDirectoryOffset, newHeader.LeafDirectoryOffset+newHeader.LeafDirectoryLength-1))
resp, err = client.Do(req)

leafBar := progressbar.DefaultBytes(
leafBar := defaultBytesProgressbar(
logger,
int64(newHeader.LeafDirectoryLength),
"downloading leaf directories",
)
io.Copy(leafWriter, io.TeeReader(resp.Body, leafBar))
leafBar.Close()

fmt.Println(len(have), "local chunks")
bar := progressbar.DefaultBytes(
bar := defaultBytesProgressbar(
logger,
int64(totalRemoteBytes-toTransfer),
"copying local chunks",
)
Expand All @@ -308,7 +311,8 @@ func Sync(logger *log.Logger, oldVersion string, newVersion string, dryRun bool)
multiRanges := makeMultiRanges(ranges, int64(newHeader.TileDataOffset), 1048576-200)
fmt.Println("Batched into http requests", len(multiRanges))

bar = progressbar.DefaultBytes(
bar = defaultBytesProgressbar(
logger,
int64(toTransfer),
"fetching remote chunks",
)
Expand Down
5 changes: 2 additions & 3 deletions pmtiles/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pmtiles
import (
"context"
"fmt"
"github.com/schollz/progressbar/v3"
"gocloud.dev/blob"
"io"
"log"
Expand All @@ -19,7 +18,7 @@ func partSizeBytes(totalSize int64) int {
}

// Upload a pmtiles archive to a bucket.
func Upload(_ *log.Logger, InputPMTiles string, bucket string, RemotePMTiles string, maxConcurrency int) error {
func Upload(logger *log.Logger, InputPMTiles string, bucket string, RemotePMTiles string, maxConcurrency int) error {
ctx := context.Background()

b, err := blob.OpenBucket(ctx, bucket)
Expand Down Expand Up @@ -49,7 +48,7 @@ func Upload(_ *log.Logger, InputPMTiles string, bucket string, RemotePMTiles str
return fmt.Errorf("Failed to obtain writer: %w", err)
}

bar := progressbar.Default(filestat.Size())
bar := defaultProgressbar(logger, filestat.Size())
io.Copy(io.MultiWriter(w, bar), f)

if err := w.Close(); err != nil {
Expand Down
Loading