Break out the reporting from the parsing so I can parse the files on all my servers in a cron job, but collect them and report on a central server.
parent
267ffb4858
commit
593b120be5
@ -0,0 +1,49 @@ |
|||||||
|
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); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue