@naturalcycles/nodejs-lib 12.47.4 → 12.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/colors/colors.d.ts +6 -1
- package/dist/colors/colors.js +7 -7
- package/dist/stream/transform/transformLogProgress.d.ts +8 -0
- package/dist/stream/transform/transformLogProgress.js +9 -8
- package/package.json +1 -2
- package/src/colors/colors.ts +7 -5
- package/src/stream/transform/transformLogProgress.ts +20 -7
package/dist/colors/colors.d.ts
CHANGED
package/dist/colors/colors.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hasColors = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.hasColors = hasColors;
|
|
4
|
+
const tty = require("tty");
|
|
5
|
+
/**
|
|
6
|
+
* Based on: https://github.com/sindresorhus/yoctocolors/pull/5
|
|
7
|
+
*
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
exports.hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors();
|
|
@@ -75,6 +75,14 @@ export interface TransformLogProgressOptions<IN = any> extends TransformOptions
|
|
|
75
75
|
* chunk is undefined for "final" stats, otherwise is defined.
|
|
76
76
|
*/
|
|
77
77
|
extra?: (chunk: IN | undefined, index: number) => AnyObject;
|
|
78
|
+
/**
|
|
79
|
+
* If specified - will multiply the counter by this number.
|
|
80
|
+
* Useful e.g when using `transformBuffer({ batchSize: 500 })`, so
|
|
81
|
+
* it'll accurately represent the number of processed entries (not batches).
|
|
82
|
+
*
|
|
83
|
+
* Defaults to 1.
|
|
84
|
+
*/
|
|
85
|
+
batchSize?: number;
|
|
78
86
|
}
|
|
79
87
|
/**
|
|
80
88
|
* Pass-through transform that optionally logs progress.
|
|
@@ -8,14 +8,14 @@ const time_lib_1 = require("@naturalcycles/time-lib");
|
|
|
8
8
|
const colors_1 = require("../../colors");
|
|
9
9
|
const colors_2 = require("../../colors/colors");
|
|
10
10
|
const inspectOpt = {
|
|
11
|
-
colors:
|
|
11
|
+
colors: colors_2.hasColors,
|
|
12
12
|
breakLength: 300,
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
15
|
* Pass-through transform that optionally logs progress.
|
|
16
16
|
*/
|
|
17
17
|
function transformLogProgress(opt = {}) {
|
|
18
|
-
const { metric = 'progress', heapTotal: logHeapTotal = false, heapUsed: logHeapUsed = false, rss: logRss = true, peakRSS: logPeakRSS = true, logRPS = true, logEvery = 1000, extra, } = opt;
|
|
18
|
+
const { metric = 'progress', heapTotal: logHeapTotal = false, heapUsed: logHeapUsed = false, rss: logRss = true, peakRSS: logPeakRSS = true, logRPS = true, logEvery = 1000, batchSize = 1, extra, } = opt;
|
|
19
19
|
const logProgress = opt.logProgress !== false && logEvery !== 0; // true by default
|
|
20
20
|
const logEvery10 = logEvery * 10;
|
|
21
21
|
const started = Date.now();
|
|
@@ -46,15 +46,16 @@ function transformLogProgress(opt = {}) {
|
|
|
46
46
|
return;
|
|
47
47
|
const mem = process.memoryUsage();
|
|
48
48
|
const now = Date.now();
|
|
49
|
-
const
|
|
50
|
-
const
|
|
49
|
+
const batchedProgress = progress * batchSize;
|
|
50
|
+
const lastRPS = (processedLastSecond * batchedProgress) / ((now - lastSecondStarted) / 1000) || 0;
|
|
51
|
+
const rpsTotal = Math.round(batchedProgress / ((now - started) / 1000)) || 0;
|
|
51
52
|
lastSecondStarted = now;
|
|
52
53
|
processedLastSecond = 0;
|
|
53
54
|
const rps10 = Math.round(sma.push(lastRPS));
|
|
54
55
|
if (mem.rss > peakRSS)
|
|
55
56
|
peakRSS = mem.rss;
|
|
56
57
|
console.log((0, util_1.inspect)({
|
|
57
|
-
[final ? `${metric}_final` : metric]:
|
|
58
|
+
[final ? `${metric}_final` : metric]: batchedProgress,
|
|
58
59
|
...(extra ? extra(chunk, progress) : {}),
|
|
59
60
|
...(logHeapUsed ? { heapUsed: (0, js_lib_1._mb)(mem.heapUsed) } : {}),
|
|
60
61
|
...(logHeapTotal ? { heapTotal: (0, js_lib_1._mb)(mem.heapTotal) } : {}),
|
|
@@ -71,14 +72,14 @@ function transformLogProgress(opt = {}) {
|
|
|
71
72
|
: {}),
|
|
72
73
|
}, inspectOpt));
|
|
73
74
|
if (tenx) {
|
|
74
|
-
let perHour = Math.round((
|
|
75
|
+
let perHour = Math.round((batchedProgress * 1000 * 60 * 60) / (now - started)) || 0;
|
|
75
76
|
if (perHour > 900) {
|
|
76
77
|
perHour = Math.round(perHour / 1000) + 'K';
|
|
77
78
|
}
|
|
78
|
-
console.log(`${(0, colors_1.dimGrey)((0, time_lib_1.dayjs)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(
|
|
79
|
+
console.log(`${(0, colors_1.dimGrey)((0, time_lib_1.dayjs)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(batchedProgress)} rows, ~${(0, colors_1.yellow)(perHour)}/hour`);
|
|
79
80
|
}
|
|
80
81
|
else if (final) {
|
|
81
|
-
console.log(`${(0, colors_1.boldWhite)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} to process ${(0, colors_1.yellow)(
|
|
82
|
+
console.log(`${(0, colors_1.boldWhite)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} to process ${(0, colors_1.yellow)(batchedProgress)} rows with total RPS of ${(0, colors_1.yellow)(rpsTotal)}`);
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/nodejs-lib",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.49.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"docs-serve": "vuepress dev docs",
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
"move-file": "^2.0.0",
|
|
38
38
|
"nanoid": "^3.0.0",
|
|
39
39
|
"sanitize-html": "^2.5.2",
|
|
40
|
-
"supports-color": "^8.0.0",
|
|
41
40
|
"through2-concurrent": "^2.0.0",
|
|
42
41
|
"yargs": "^17.0.0"
|
|
43
42
|
},
|
package/src/colors/colors.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import * as tty from 'tty'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Based on: https://github.com/sindresorhus/yoctocolors/pull/5
|
|
5
|
+
*
|
|
6
|
+
* @experimental
|
|
7
|
+
*/
|
|
8
|
+
export const hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors()
|
|
@@ -92,10 +92,19 @@ export interface TransformLogProgressOptions<IN = any> extends TransformOptions
|
|
|
92
92
|
* chunk is undefined for "final" stats, otherwise is defined.
|
|
93
93
|
*/
|
|
94
94
|
extra?: (chunk: IN | undefined, index: number) => AnyObject
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* If specified - will multiply the counter by this number.
|
|
98
|
+
* Useful e.g when using `transformBuffer({ batchSize: 500 })`, so
|
|
99
|
+
* it'll accurately represent the number of processed entries (not batches).
|
|
100
|
+
*
|
|
101
|
+
* Defaults to 1.
|
|
102
|
+
*/
|
|
103
|
+
batchSize?: number
|
|
95
104
|
}
|
|
96
105
|
|
|
97
106
|
const inspectOpt: InspectOptions = {
|
|
98
|
-
colors: hasColors
|
|
107
|
+
colors: hasColors,
|
|
99
108
|
breakLength: 300,
|
|
100
109
|
}
|
|
101
110
|
|
|
@@ -113,6 +122,7 @@ export function transformLogProgress<IN = any>(
|
|
|
113
122
|
peakRSS: logPeakRSS = true,
|
|
114
123
|
logRPS = true,
|
|
115
124
|
logEvery = 1000,
|
|
125
|
+
batchSize = 1,
|
|
116
126
|
extra,
|
|
117
127
|
} = opt
|
|
118
128
|
const logProgress = opt.logProgress !== false && logEvery !== 0 // true by default
|
|
@@ -153,8 +163,10 @@ export function transformLogProgress<IN = any>(
|
|
|
153
163
|
const mem = process.memoryUsage()
|
|
154
164
|
|
|
155
165
|
const now = Date.now()
|
|
156
|
-
const
|
|
157
|
-
const
|
|
166
|
+
const batchedProgress = progress * batchSize
|
|
167
|
+
const lastRPS =
|
|
168
|
+
(processedLastSecond * batchedProgress) / ((now - lastSecondStarted) / 1000) || 0
|
|
169
|
+
const rpsTotal = Math.round(batchedProgress / ((now - started) / 1000)) || 0
|
|
158
170
|
lastSecondStarted = now
|
|
159
171
|
processedLastSecond = 0
|
|
160
172
|
|
|
@@ -164,7 +176,7 @@ export function transformLogProgress<IN = any>(
|
|
|
164
176
|
console.log(
|
|
165
177
|
inspect(
|
|
166
178
|
{
|
|
167
|
-
[final ? `${metric}_final` : metric]:
|
|
179
|
+
[final ? `${metric}_final` : metric]: batchedProgress,
|
|
168
180
|
...(extra ? extra(chunk, progress) : {}),
|
|
169
181
|
...(logHeapUsed ? { heapUsed: _mb(mem.heapUsed) } : {}),
|
|
170
182
|
...(logHeapTotal ? { heapTotal: _mb(mem.heapTotal) } : {}),
|
|
@@ -185,7 +197,8 @@ export function transformLogProgress<IN = any>(
|
|
|
185
197
|
)
|
|
186
198
|
|
|
187
199
|
if (tenx) {
|
|
188
|
-
let perHour: number | string =
|
|
200
|
+
let perHour: number | string =
|
|
201
|
+
Math.round((batchedProgress * 1000 * 60 * 60) / (now - started)) || 0
|
|
189
202
|
if (perHour > 900) {
|
|
190
203
|
perHour = Math.round(perHour / 1000) + 'K'
|
|
191
204
|
}
|
|
@@ -193,12 +206,12 @@ export function transformLogProgress<IN = any>(
|
|
|
193
206
|
console.log(
|
|
194
207
|
`${dimGrey(dayjs().toPretty())} ${white(metric)} took ${yellow(
|
|
195
208
|
_since(started),
|
|
196
|
-
)} so far to process ${yellow(
|
|
209
|
+
)} so far to process ${yellow(batchedProgress)} rows, ~${yellow(perHour)}/hour`,
|
|
197
210
|
)
|
|
198
211
|
} else if (final) {
|
|
199
212
|
console.log(
|
|
200
213
|
`${boldWhite(metric)} took ${yellow(_since(started))} to process ${yellow(
|
|
201
|
-
|
|
214
|
+
batchedProgress,
|
|
202
215
|
)} rows with total RPS of ${yellow(rpsTotal)}`,
|
|
203
216
|
)
|
|
204
217
|
}
|