Skip to content
Snippets Groups Projects
Select Git revision
  • ae0c55a441ed950e90fb2c4c33b69e7032a66274
  • main default
2 results

go.mod

Blame
  • 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)