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

Add JSON output to stats script and use layer source #882

Merged
merged 15 commits into from
Jun 23, 2023
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ When adding or changing style layer code, it can be helpful to assess the change

There is a "stats" script that will generate various statistics about layer composition and complexity:

- `npm run stats -- -a -s` - overall size and breakdown of layers
- `npm run stats -- -c` - total layer count
- `npm run stats -- -h` - list all options
- `npm run -s stats -- -a -s` - overall size and breakdown of layers
- `npm run -s stats -- -c` - total layer count
- `npm run -s stats -- -h` - list all options

There is an "extract_layers" script that will extract layer style data:

- `node scripts/extract_layers -pl <layer>` - JSON contents of a specified layer
- `node scripts/extract_layers -pg <source>` - list of layers from a specified source
- `node scripts/extract_layers -h` - list all options
- `npm run -s extract_layers -pl <layer>` - JSON contents of a specified layer
- `npm run -s extract_layers -pg <source>` - list of layers from a specified source
- `npm run -s extract_layers -h` - list all options
ZeLonewolf marked this conversation as resolved.
Show resolved Hide resolved

## Layers

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"code_format": "run-s code_format:prettier code_format:svgo",
"code_format:prettier": "prettier --write --list-different .",
"code_format:svgo": "svgo -q -f icons/",
"extract_layer": "node scripts/extract_layer",
"presprites": "shx rm -rf dist/sprites",
"serve": "ts-node scripts/serve",
"shields": "node scripts/generate_shield_defs.js -o dist/shields.json",
Expand Down
10 changes: 5 additions & 5 deletions scripts/extract_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ program
.option("-loc, --locales <locale1 locale2...>", "language codes", ["mul"]);
program.parse(process.argv);

let opts = program.opts();
const opts = program.opts();

if (Object.keys(opts).length === 1) program.help();

let locales = opts.locales[0].split(",");
const locales = opts.locales[0].split(",");

let style = Style.build(
const style = Style.build(
config.OPENMAPTILES_URL,
"https://zelonewolf.github.io/openstreetmap-americana/sprites/sprite",
"https://osm-americana.github.io/fontstack66/{fontstack}/{range}.pbf",
Expand All @@ -33,9 +33,9 @@ const layerMap = new Map();
const layerGroupMap = new Map();

for (let i = 0; i < layers.length; i++) {
let layer = layers[i];
const layer = layers[i];
layerMap.set(layer.id, layers[i]);
let layerGroup = layer["source-layer"] || layer.source || layer.type;
const layerGroup = layer["source-layer"] || layer.source || layer.type;
if (!layerGroupMap.has(layerGroup)) {
layerGroupMap.set(layerGroup, [layer.id]);
} else {
Expand Down
43 changes: 25 additions & 18 deletions scripts/stats.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import * as Style from "../src/js/style.js";
import config from "../src/config.js";
import { Command } from "commander";
import { Command, Option } from "commander";

const program = new Command();
program
.option("-a, --all-layers", "summary layer stats")
.option("-c, --layer-count", "count number of layers")
.option("-s, --layer-size", "size of all layers")
.addOption(
new Option("-a, --all-layers", "summary layer stats")
.conflicts("layerCount")
.conflicts("layerSize")
.conflicts("allJson")
)
.addOption(
new Option("-c, --layer-count", "count number of layers")
.conflicts("layerSize")
.conflicts("allJson")
)
.addOption(
new Option("-s, --layer-size", "size of all layers").conflicts("allJson")
)
.option("-loc, --locales <locale1 locale2...>", "language codes", ["mul"])
.option("-j, --all-json", "output all stats in JSON")
.option("-pp, --pretty", "pretty-print JSON output");

program.parse(process.argv);

let opts = program.opts();
const opts = program.opts();

if (Object.keys(opts).length === 1) program.help();

let locales = opts.locales[0].split(",");
const locales = opts.locales[0].split(",");

let style = Style.build(
const style = Style.build(
config.OPENMAPTILES_URL,
"https://zelonewolf.github.io/openstreetmap-americana/sprites/sprite",
"https://osm-americana.github.io/fontstack66/{fontstack}/{range}.pbf",
Expand Down Expand Up @@ -50,34 +61,30 @@ const stats = {
};

for (let i = 0; i < layerCount; i++) {
let layer = layers[i];
const layer = layers[i];
layerMap.set(layer.id, layers[i]);
let layerSize = JSON.stringify(layer).length;
let layerGroup = layer["source-layer"] || layer.source || layer.type;
const layerSize = JSON.stringify(layer).length;
const layerGroup = layer["source-layer"] || layer.source || layer.type;
if (stats.layerGroup[layerGroup]) {
stats.layerGroup[layerGroup].size += layerSize;
stats.layerGroup[layerGroup].count++;
stats.layerGroup[layerGroup].layerCount++;
} else {
stats.layerGroup[layerGroup] = {
size: layerSize,
count: 1,
layerCount: 1,
};
}
}

if (opts.allJson) {
if (opts.pretty) {
process.stdout.write(JSON.stringify(stats, null, 2));
} else {
process.stdout.write(JSON.stringify(stats));
}
process.stdout.write(JSON.stringify(stats, null, opts.pretty ? 2 : null));
process.exit();
}

if (opts.allLayers) {
for (const layerGroup in stats.layerGroup) {
let layerStats = stats.layerGroup[layerGroup];
let layerString = `${layerGroup}(${layerStats.count})`.padEnd(30, ".");
let layerString = `${layerGroup}(${layerStats.layerCount})`.padEnd(30, ".");
console.log(
`${layerString}${layerStats.size
.toLocaleString("en-US")
Expand Down