These are the projects for the JavaScript Level 2 module in Learn JS the Hard Way.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.5 KiB

import fs from "fs";
import assert from "assert";
import readline from "readline";
import { program } from "commander";
import glob from "fast-glob";
import format from "date-fns/format/index.js";
program
.option("--min <Number>", "The lowest count to print. Stop at this.", 1)
.option("--errors", "Show the errors so you can fix them.", false)
.option("--outfile <String>", "Save to file rather than stdout.")
.requiredOption("--input <String>", "Input file glob.")
.description("Loads the output of a chain parser and produces a report.")
.version(0.1);
program.parse();
const OPTS = program.opts();
OPTS.min = parseInt(OPTS.min);
assert(!isNaN(OPTS.min), `min must be a number, you have ${OPTS.min}`);
const sort_request_chains = (chains, min) => {
const converted = [];
for(let [url, stats] of Object.entries(chains)) {
if(stats.count < min) continue; // skip below min
if(stats.comes_from) {
converted.push([stats.count, `[${stats.comes_from}] ${stats.full_chain.join(' ')}`]);
} else {
converted.push([stats.count, `${stats.full_chain.join(' ')}`]);
}
}
return converted.sort((a, b) => b[0] - a[0]);
}
const { stats, domain, result, generated_on} = JSON.parse(fs.readFileSync(OPTS.input));
const dates = Object.keys(result).sort();
for(let key of dates) {
console.log(`--- ${key} ---`);
const stats = sort_request_chains(result[key], OPTS.min);
for(let [count, chain] of stats) {
console.log(count, "\t", chain);
}
}