-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathReport.ts
268 lines (208 loc) Β· 6.75 KB
/
Report.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import throttle from 'lodash/throttle';
import {PassThrough} from 'stream';
import {StringDecoder} from 'string_decoder';
import {MessageName} from './MessageName';
import {Locator, LocatorHash} from './types';
const TITLE_PROGRESS_FPS = 15;
export class ReportError extends Error {
public reportCode: MessageName;
public originalError?: Error;
constructor(code: MessageName, message: string, public reportExtra?: (report: Report) => void) {
super(message);
this.reportCode = code;
}
}
export function isReportError(error: Error): error is ReportError {
return typeof (error as ReportError).reportCode !== `undefined`;
}
export type ProgressDefinition = {
progress?: number;
title?: string;
};
export type ProgressIterable = AsyncIterable<ProgressDefinition> & {
hasProgress: boolean;
hasTitle: boolean;
};
export type SectionOptions = {
reportHeader?: () => void;
reportFooter?: (elapsedTime: number) => void;
skipIfEmpty?: boolean;
};
export type TimerOptions = Pick<SectionOptions, `skipIfEmpty`>;
export abstract class Report {
cacheHits = new Set<LocatorHash>();
cacheMisses = new Set<LocatorHash>();
private reportedInfos: Set<any> = new Set();
private reportedWarnings: Set<any> = new Set();
private reportedErrors: Set<any> = new Set();
getRecommendedLength() {
return 180;
}
reportCacheHit(locator: Locator) {
this.cacheHits.add(locator.locatorHash);
}
reportCacheMiss(locator: Locator, message?: string) {
this.cacheMisses.add(locator.locatorHash);
}
abstract startSectionPromise<T>(opts: SectionOptions, cb: () => Promise<T>): Promise<T>;
abstract startSectionSync<T>(opts: SectionOptions, cb: () => T): T;
abstract startTimerPromise<T>(what: string, opts: TimerOptions, cb: () => Promise<T>): Promise<T>;
abstract startTimerPromise<T>(what: string, cb: () => Promise<T>): Promise<T>;
abstract startTimerSync<T>(what: string, opts: TimerOptions, cb: () => T): T;
abstract startTimerSync<T>(what: string, cb: () => T): T;
abstract reportSeparator(): void;
abstract reportInfo(name: MessageName | null, text: string): void;
abstract reportWarning(name: MessageName, text: string): void;
abstract reportError(name: MessageName, text: string): void;
abstract reportProgress(progress: AsyncIterable<ProgressDefinition>): Promise<void> & {stop: () => void};
abstract reportJson(data: any): void;
abstract reportFold(title: string, text: string): void;
abstract finalize(): void;
static progressViaCounter(max: number) {
let current = 0;
let unlock: () => void;
let lock = new Promise<void>(resolve => {
unlock = resolve;
});
const set = (n: number) => {
const thisUnlock = unlock;
lock = new Promise<void>(resolve => {
unlock = resolve;
});
current = n;
thisUnlock();
};
const tick = (n: number = 0) => {
set(current + 1);
};
const gen = (async function * () {
while (current < max) {
await lock;
yield {
progress: current / max,
};
}
})();
return {
[Symbol.asyncIterator]() {
return gen;
},
hasProgress: true,
hasTitle: false,
set,
tick,
};
}
static progressViaTitle() {
let currentTitle: string | undefined;
let unlock: () => void;
let lock = new Promise<void>(resolve => {
unlock = resolve;
});
const setTitle: (title: string) => void = throttle((title: string) => {
const thisUnlock = unlock;
lock = new Promise<void>(resolve => {
unlock = resolve;
});
currentTitle = title;
thisUnlock();
}, 1000 / TITLE_PROGRESS_FPS);
const gen = (async function * () {
while (true) {
await lock;
yield {
title: currentTitle,
};
}
})();
return {
[Symbol.asyncIterator]() {
return gen;
},
hasProgress: false,
hasTitle: true,
setTitle,
};
}
async startProgressPromise<T, P extends ProgressIterable>(progressIt: P, cb: (progressIt: P) => Promise<T>): Promise<T> {
const reportedProgress = this.reportProgress(progressIt);
try {
return await cb(progressIt);
} finally {
reportedProgress.stop();
}
}
startProgressSync<T, P extends ProgressIterable>(progressIt: P, cb: (progressIt: P) => T): T {
const reportedProgress = this.reportProgress(progressIt);
try {
return cb(progressIt);
} finally {
reportedProgress.stop();
}
}
reportInfoOnce(name: MessageName, text: string, opts?: {key?: any, reportExtra?: (report: Report) => void}) {
const key = opts && opts.key ? opts.key : text;
if (!this.reportedInfos.has(key)) {
this.reportedInfos.add(key);
this.reportInfo(name, text);
opts?.reportExtra?.(this);
}
}
reportWarningOnce(name: MessageName, text: string, opts?: {key?: any, reportExtra?: (report: Report) => void}) {
const key = opts && opts.key ? opts.key : text;
if (!this.reportedWarnings.has(key)) {
this.reportedWarnings.add(key);
this.reportWarning(name, text);
opts?.reportExtra?.(this);
}
}
reportErrorOnce(name: MessageName, text: string, opts?: {key?: any, reportExtra?: (report: Report) => void}) {
const key = opts && opts.key ? opts.key : text;
if (!this.reportedErrors.has(key)) {
this.reportedErrors.add(key);
this.reportError(name, text);
opts?.reportExtra?.(this);
}
}
reportExceptionOnce(error: Error | ReportError) {
if (isReportError(error)) {
this.reportErrorOnce(error.reportCode, error.message, {key: error, reportExtra: error.reportExtra});
} else {
this.reportErrorOnce(MessageName.EXCEPTION, error.stack || error.message, {key: error});
}
}
createStreamReporter(prefix: string | null = null) {
const stream = new PassThrough();
const decoder = new StringDecoder();
let buffer = ``;
stream.on(`data`, chunk => {
let chunkStr = decoder.write(chunk);
let lineIndex;
do {
lineIndex = chunkStr.indexOf(`\n`);
if (lineIndex !== -1) {
const line = buffer + chunkStr.substring(0, lineIndex);
chunkStr = chunkStr.substring(lineIndex + 1);
buffer = ``;
if (prefix !== null) {
this.reportInfo(null, `${prefix} ${line}`);
} else {
this.reportInfo(null, line);
}
}
} while (lineIndex !== -1);
buffer += chunkStr;
});
stream.on(`end`, () => {
const last = decoder.end();
if (last !== ``) {
if (prefix !== null) {
this.reportInfo(null, `${prefix} ${last}`);
} else {
this.reportInfo(null, last);
}
}
});
return stream;
}
}