1 | import { _hb, CommonLogger, NumberStack } from '@naturalcycles/js-lib'
|
2 | import { yellow } from '../colors'
|
3 | import { gzipBuffer } from '../util/zip.util'
|
4 |
|
5 | export class SizeStack extends NumberStack {
|
6 | constructor(public name: string, size: number) {
|
7 | super(size)
|
8 | }
|
9 |
|
10 | total = 0
|
11 |
|
12 | override push(item: any): this {
|
13 | this.total += item
|
14 | return super.push(item)
|
15 | }
|
16 |
|
17 | getStats(): string {
|
18 |
|
19 |
|
20 | return [
|
21 | ' ' + this.name,
|
22 | 'avg',
|
23 | yellow(_hb(this.avg())),
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | 'total',
|
29 | yellow(_hb(this.total)),
|
30 | ].join(' ')
|
31 | }
|
32 |
|
33 | static async countItem(
|
34 | item: any,
|
35 | logger: CommonLogger,
|
36 | sizes?: SizeStack,
|
37 | sizesZipped?: SizeStack,
|
38 | ): Promise<void> {
|
39 | if (!sizes) return
|
40 |
|
41 |
|
42 | try {
|
43 | const buf = Buffer.from(JSON.stringify(item))
|
44 | sizes.push(buf.byteLength)
|
45 |
|
46 | if (sizesZipped) {
|
47 | const { byteLength } = await gzipBuffer(buf)
|
48 | sizesZipped.push(byteLength)
|
49 | }
|
50 | } catch (err) {
|
51 | logger.warn(
|
52 | `transformLogProgress failed to JSON.stringify the chunk: ${(err as Error).message}`,
|
53 | )
|
54 | }
|
55 | }
|
56 | }
|