@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
51 lines (50 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NDJsonStats = void 0;
const js_lib_1 = require("@naturalcycles/js-lib");
const colors_1 = require("../../colors");
class NDJsonStats {
constructor() {
this.tookMillis = 0;
this.rows = 0;
this.sizeBytes = 0;
}
static create(o = {}) {
return Object.assign(new NDJsonStats(), o);
}
static empty() {
return new NDJsonStats();
}
static createCombined(stats) {
// eslint-disable-next-line unicorn/no-array-reduce
return stats.reduce((statsTotal, stats) => statsTotal.add(stats), new NDJsonStats());
}
get rpsTotal() {
return Math.round(this.rows / ((this.tookMillis || 1) / 1000));
}
get bpsTotal() {
return this.sizeBytes === 0 ? 0 : Math.round(this.sizeBytes / ((this.tookMillis || 1) / 1000));
}
get avgBytesPerRow() {
return Math.round(this.sizeBytes / this.rows);
}
/**
* Non-mutating addition, returns new object
*/
add(s) {
return NDJsonStats.create({
tookMillis: this.tookMillis + s.tookMillis,
rows: this.rows + s.rows,
sizeBytes: this.sizeBytes + s.sizeBytes,
});
}
toPretty(name) {
return [
`Processed ${name ? (0, colors_1.boldWhite)(name) + ': ' : ''}${(0, colors_1.dimWhite)(this.rows)} rows, ${(0, colors_1.dimWhite)((0, js_lib_1._hb)(this.sizeBytes))} in ${(0, colors_1.dimWhite)((0, js_lib_1._ms)(this.tookMillis))}`,
`${(0, colors_1.dimWhite)(this.rpsTotal + ' rows/sec')}`,
`${(0, colors_1.dimWhite)((0, js_lib_1._hb)(this.avgBytesPerRow) + '/row')}`,
`${(0, colors_1.dimWhite)((0, js_lib_1._hb)(this.bpsTotal) + '/sec')}`,
].join(', ');
}
}
exports.NDJsonStats = NDJsonStats;