UNPKG

@naturalcycles/nodejs-lib

Version:
76 lines 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_lib_1 = require("@naturalcycles/js-lib"); const time_lib_1 = require("@naturalcycles/time-lib"); const stream_1 = require("stream"); const util_1 = require("util"); const __1 = require("../.."); const inspectOpt = { colors: true, breakLength: 100, }; /** * Pass-through transform that optionally logs progress. */ function transformLogProgress(opt = {}) { const { metric = 'progress', heapTotal: logHeapTotal = false, logEvery = 100, extra } = opt; const logProgress = opt.logProgress !== false; // true by default const logHeapUsed = opt.heapUsed !== false; // true by default const logRss = opt.rss !== false; // true by default const logRPS = opt.logRPS !== false; // true by default const logEvery10 = logEvery * 10; const started = Date.now(); let lastSecondStarted = Date.now(); const sma = new js_lib_1.SimpleMovingAverage(10); // over last 10 seconds let processedLastSecond = 0; let progress = 0; logStats(); // initial return new stream_1.Transform({ objectMode: true, ...opt, transform(chunk, _encoding, cb) { progress++; processedLastSecond++; if (logProgress && progress % logEvery === 0) { logStats(chunk, false, progress % logEvery10 === 0); } cb(null, chunk); // pass-through }, final(cb) { logStats(undefined, true); cb(); }, }); function logStats(chunk, final = false, tenx = false) { if (!logProgress) return; const { rss, heapUsed, heapTotal } = process.memoryUsage(); const now = Date.now(); const lastRPS = processedLastSecond / ((now - lastSecondStarted) / 1000) || 0; const rpsTotal = Math.round(progress / ((now - started) / 1000)) || 0; lastSecondStarted = now; processedLastSecond = 0; const rps10 = Math.round(sma.push(lastRPS)); console.log(util_1.inspect({ [metric]: progress, ...(extra && !final ? extra(chunk, progress) : {}), ...(logHeapUsed ? { heapUsed: __1.mb(heapUsed) } : {}), ...(logHeapTotal ? { heapTotal: __1.mb(heapTotal) } : {}), ...(logRss ? { rss: __1.mb(rss) } : {}), ...(logRPS ? { rps10, rpsTotal, } : {}), }, inspectOpt)); if (tenx) { console.log(`${__1.dimGrey(time_lib_1.dayjs().toPretty())} ${__1.white(metric)} took ${__1.yellow(time_lib_1.since(started))} so far to process ${__1.yellow(progress)} rows`); } else if (final) { console.log(`${__1.boldWhite(metric)} took ${__1.yellow(time_lib_1.since(started))} to process ${__1.yellow(progress)} rows with total RPS of ${__1.yellow(rpsTotal)}`); } } } exports.transformLogProgress = transformLogProgress; //# sourceMappingURL=transformLogProgress.js.map