@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
49 lines (48 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SizeStack = void 0;
const js_lib_1 = require("@naturalcycles/js-lib");
const colors_1 = require("../colors");
const zip_util_1 = require("../util/zip.util");
class SizeStack extends js_lib_1.NumberStack {
constructor(name, size) {
super(size);
this.name = name;
this.total = 0;
}
push(item) {
this.total += item;
return super.push(item);
}
getStats() {
// const pcs = this.percentiles([50, 90])
return [
' ' + this.name,
'avg',
(0, colors_1.yellow)((0, js_lib_1._hb)(this.avg())),
// 'p50',
// yellow(_hb(pcs[50])),
// 'p90',
// yellow(_hb(pcs[90])),
'total',
(0, colors_1.yellow)((0, js_lib_1._hb)(this.total)),
].join(' ');
}
static async countItem(item, logger, sizes, sizesZipped) {
if (!sizes)
return;
// try-catch, because we don't want to fail the pipeline on logProgress
try {
const buf = Buffer.from(JSON.stringify(item));
sizes.push(buf.byteLength);
if (sizesZipped) {
const { byteLength } = await (0, zip_util_1.gzipBuffer)(buf);
sizesZipped.push(byteLength);
}
}
catch (err) {
logger.warn(`transformLogProgress failed to JSON.stringify the chunk: ${err.message}`);
}
}
}
exports.SizeStack = SizeStack;