Skip to content
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

stats for serialtiles #148

Merged
merged 2 commits into from
Jul 10, 2020
Merged
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
1 change: 1 addition & 0 deletions bin/mapbox-tile-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ if (isNumeric(argv.retry)) options.retry = parseInt(argv.retry, 10);
if (isNumeric(argv.timeout)) options.timeout = parseInt(argv.timeout, 10);
if (argv.bundle === 'true') options.bundle = true;
if (argv['bypass-validation'] === 'true') options.bypassValidation = true;
if (process.env.BRIDGE_LOG_MAX_VTILE_BYTES_COMPRESSED) options.tileSizeStats = true;

if (!dsturi) {
console.error('You must provide a valid s3:// or file:// url');
Expand Down
22 changes: 22 additions & 0 deletions lib/serialtilescopy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
var os = require('os');
var util = require('util');
var TileStatStream = require('tile-stat-stream');
var tilelive = require('@mapbox/tilelive');
Expand All @@ -14,6 +15,10 @@ function serialtiles(srcUri, s3urlTemplate, options, callback) {
options = {};
}

if (options.tileSizeStats) {
var tileSizeStats = { total: 0, count: 0, max: 0 };
}

var statStream = new TileStatStream();

var max_tilesize = options.limits && options.limits.max_tilesize ?
Expand Down Expand Up @@ -57,6 +62,13 @@ function serialtiles(srcUri, s3urlTemplate, options, callback) {
.on('tile', function(tile) {
if (!tile.buffer) return;
stats.done++;
if (options.tileSizeStats) {
tileSizeStats.count++;
tileSizeStats.total = tileSizeStats.total + (tile.buffer.length * 0.001);
if (tileSizeStats.max < tile.buffer.length) {
tileSizeStats.max = tile.buffer.length;
}
}
if (tile.buffer.length <= max_tilesize) return;
var err = new Error(util.format('Tile exceeds maximum size of %sk at z %s. Reduce the detail of data at this zoom level or omit it by adjusting your minzoom.', Math.round(max_tilesize / 1024), tile.z));
err.code = 'EINVALID';
Expand Down Expand Up @@ -88,6 +100,16 @@ function serialtiles(srcUri, s3urlTemplate, options, callback) {
once = true;

if (err) source.unpipe();

if (options.tileSizeStats) {
// dump file to tmp dir
tileSizeStats.avg = tileSizeStats.total / tileSizeStats.count;
if (tileSizeStats.count > 0) {
var file = os.tmpdir() + '/tilelive-bridge-stats.json';
fs.writeFileSync(file, JSON.stringify(tileSizeStats));
}
}

if (options.stats) {
callback(err, statStream.getStatistics());
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mapbox/mapbox-tile-copy",
"version": "7.3.2",
"version": "7.4.0",
"description": "From geodata files to tiles on S3",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ The following example will distribute tiles to the second part out of 4 total pa
$ mapbox-tile-copy ~/data/my-tiles.mbtiles s3://my-bucket/parallel/{z}/{x}/{y} --part 1 --parts 4
```

Collect tile size statistics and dump to your local tmp dir named `/tmp/<tmpdirpath>/tilelive-bridge-stats.json`
```
$ BRIDGE_LOG_MAX_VTILE_BYTES_COMPRESSED=1 mapbox-tile-copy ~/data/my-tiles.mbtiles s3://my-bucket/folder/mbtiles/{z}/{x}/{y}
```

## Supported file types

- .mbtiles
Expand Down