Select Git revision
index.js 1.75 KiB
// COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2021.
const { createCLIApp, loginAndCallAfterwards } = require("@hypercog/utils")
const { table } = require("table")
const TABLE_CONFIG = {
border: {
topBody: `─`,
topJoin: `┬`,
topLeft: `┌`,
topRight: `┐`,
bottomBody: `─`,
bottomJoin: `┴`,
bottomLeft: `└`,
bottomRight: `┘`,
bodyLeft: `│`,
bodyRight: `│`,
bodyJoin: `│`,
joinBody: `─`,
joinLeft: `├`,
joinRight: `┤`,
joinJoin: `┼`
},
columns: [
{ alignment: "left" },
{ alignment: "right" },
{ alignment: "right" },
{ alignment: "right" },
{ alignment: "right" },
{ alignment: "right" },
{ alignment: "right" }
]
}
const humanizePercentage = value => {
if (value.percentage === undefined) {
return value.total
}
return `${value.total} [%${value.percentage.toFixed(2)}]`
}
const humanizePrice = value => `${value.avg} [${value.min}, ${value.max}]`
const toLine = stat => [
stat.total,
humanizePercentage(stat.reused),
humanizePercentage(stat.discarded),
humanizePrice(stat.price)
]
const getStats = async apiClient => {
const stats = await apiClient.getStats()
const tableValues = [
[
"",
"Total slag\nt",
"Reused\nt [%]",
"Discarded\nt [%]",
"Price (€)\n Avg [Min, Max]"
],
...Object.entries(stats.perCompany || []).map(([org, val]) => [
org,
...toLine(val)
]),
["Total", ...toLine(stats.global)]
]
console.log(table(tableValues, TABLE_CONFIG))
}
const program = createCLIApp()
program
.command("stats")
.description("Get stats for public administration")
// get max, get min
.action(loginAndCallAfterwards(getStats))
program.parse(process.argv)