const { interval, fromPromise, fromIter, forEach, map, pipe, flatten } = require("callbag-basics"); const fetch = require('node-fetch'); 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 = (name, perc, width) => { perc = mapNumber(perc, 95, 100, 0, 100); // correctie om meer verschil te zien let full = Math.floor((perc / 100) * width); return `${name} [${'='.repeat(full)}${' '.repeat(width - full)}]`; }; let tvs = {}; const updateProgress = () => { var pbs = []; pbs.push(new Date().toISOString().slice(11, 19)); Object.keys(tvs).forEach(k => { pbs.push(getProgressBar(k, 100 - tvs[k], 50)); }); const out = pbs.join(', ') + '\r'; //\n process.stdout.write(out); }; const numberRegex = /(\d+)/gm; const fields = ['total', 'used', 'free', 'shared', 'buffers', 'cached', 'buffers_used', 'buffers_free', 'swap_total', 'swap_used', 'swap_free']; const parseInfo = (tv, data) => { const res = {}; const matches = []; while (m = numberRegex.exec(data)) { matches.push(m[1]) } matches.map(a => Number(a)).slice(0, fields.length).forEach((a, i) => res[fields[i]] = a); return { tv, info: res }; }; const showInfo = (tv, memInfo) => { if (memInfo && memInfo.total) { let total = memInfo.total; let free = (memInfo.total - memInfo.used); let perc = (free / total) * 100; tvs[tv] = perc; updateProgress(); } }; const getInfo = (tv, url) => { return fetch(url) .then(r => r.text()) .then(text => ({ tv: tv, info: text })); } pipe( interval(500), map(x => fromIter([34, 35])), flatten, map(x => fromPromise(getInfo(`${x}`, `http://10.199.108.${x}:9500/api/memoryinfo`))), flatten, map(x => parseInfo(x.tv, x.info)), forEach(x => showInfo(x.tv, x.info)) );
191500cookie-checkCallbag example