Select Git revision
This project manages its dependencies using Go Modules.
Learn more
index.js 1.78 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.toFixed(3)} [%${(value.percentage * 100).toFixed(2)}]`
}
const humanizePrice = value =>
`${value.avg.toFixed(2)} [${value.min.toFixed(2)}, ${value.max.toFixed(2)}]`
const toLine = stat => [
stat.total.toFixed(3),
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.perSteelCompany || []).map(([org, val]) => [
org,
...toLine(val)
])
]
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)