ES6 MapNumber, CLI progressbar

Date: 2019-02-11
const fetch = require('node-fetch');

let tvs = {};

const limit = (v, min, max) => Math.max(min, Math.min(max, Number(v) || 0));
const mapNumber = (v, inMin, inMax, outMin, outMax) => (limit(v, inMin, inMax) - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;

let getProgressBar;
getProgressBar = (name, perc, width) => {
    perc = mapNumber(perc, 85, 100, 0, 100); // correctie om meer verschil te zien
    let full = Math.floor((perc / 100) * width);
    let empty = width - full;    
    return name + ' [' + '='.repeat(full) + ' '.repeat(empty) + ']';
};

const updateProgress = () => {
    var pbs = [];
    Object.keys(tvs)
    .forEach(k => {
        pbs.push(getProgressBar(k, 100 - tvs[k], 100));
    });
    const out = pbs.join(', ') + '\r';
    //console.log(out);
    process.stdout.write(out);
};

const showInfo = (tv, data) => {
    if (data && data.SystemUsage) {
        let memory = data.SystemUsage.memory;
        let total = memory.total;
        let free = (memory.total - memory.used);
        let perc = (free / total) * 100;    
        tvs[tv] = perc;
        updateProgress();
    }
};

const getInfo = (url, tv) => {
   return fetch(url)
    .then(r => r.json())
    .then(d => showInfo(tv, d));
}

setInterval(() => {    
    getInfo('http://10.199.108.35:9500/api/info', '35');
    getInfo('http://10.199.108.34:9500/api/info', '34');
}, 2000);
18640cookie-checkES6 MapNumber, CLI progressbar