@choiceform/os-client-core 2.1.65 → 2.1.69
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/README.md +27 -27
- package/dist/core.js +1 -1
- package/dist/infer.js +414 -414
- package/dist/modern/core.js +1 -1
- package/dist/modern/web-core.js +2 -2
- package/dist/support/atcp.js +1 -1
- package/dist/web-core.js +2 -2
- package/enum2union.js +27 -27
- package/index.d.ts +10092 -10059
- package/package.json +1 -1
package/dist/infer.js
CHANGED
|
@@ -1,414 +1,414 @@
|
|
|
1
|
-
const inferCommander = {
|
|
2
|
-
/**
|
|
3
|
-
* 接受子任务发来的数据结果池
|
|
4
|
-
* 可能一下子接到很多工人发来的结果,来不及上传
|
|
5
|
-
* 先积累到这里
|
|
6
|
-
*/
|
|
7
|
-
accumulated: [],
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 是否开始了消耗数据结果池
|
|
11
|
-
*/
|
|
12
|
-
consumingStarted: false,
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 是否已经完成
|
|
16
|
-
*/
|
|
17
|
-
ended: false,
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 当前已发起并等待结果的请求数量
|
|
21
|
-
*/
|
|
22
|
-
postingCount: 0,
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 查询参数
|
|
26
|
-
* @type {{
|
|
27
|
-
* max_try_count:string;
|
|
28
|
-
* max_error_count:string;
|
|
29
|
-
* thread: string;
|
|
30
|
-
* dry: string;
|
|
31
|
-
* max_skip_count: string;
|
|
32
|
-
* sid: string;
|
|
33
|
-
* fake_port: string;
|
|
34
|
-
* real_port: string;
|
|
35
|
-
* dev: string;
|
|
36
|
-
* time_range: string;
|
|
37
|
-
* status: string;
|
|
38
|
-
* collector_id: string;
|
|
39
|
-
* }}
|
|
40
|
-
*/
|
|
41
|
-
queryObj: null,
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 派遣的工人个数
|
|
45
|
-
* @type {number}
|
|
46
|
-
*/
|
|
47
|
-
iframeCount: 0,
|
|
48
|
-
/**
|
|
49
|
-
* 开始时间
|
|
50
|
-
* @type {number}
|
|
51
|
-
*/
|
|
52
|
-
startTime: 0,
|
|
53
|
-
/**
|
|
54
|
-
* 消息展示div
|
|
55
|
-
* @type {HTMLElement}
|
|
56
|
-
*/
|
|
57
|
-
messageDiv: null,
|
|
58
|
-
/**
|
|
59
|
-
* 错误展示div
|
|
60
|
-
* @type {HTMLElement}
|
|
61
|
-
*/
|
|
62
|
-
errorDiv: null,
|
|
63
|
-
/**
|
|
64
|
-
* 工人状态展示div
|
|
65
|
-
* @type {HTMLElement}
|
|
66
|
-
*/
|
|
67
|
-
workersDiv: null,
|
|
68
|
-
/**
|
|
69
|
-
* 消息颜色表
|
|
70
|
-
*/
|
|
71
|
-
typeColorMap: {
|
|
72
|
-
error: 'red',
|
|
73
|
-
skip: 'orange',
|
|
74
|
-
},
|
|
75
|
-
/**
|
|
76
|
-
* 已成功次数
|
|
77
|
-
*/
|
|
78
|
-
successCount: 0,
|
|
79
|
-
/**
|
|
80
|
-
* 已跳过次数
|
|
81
|
-
*/
|
|
82
|
-
skipCount: 0,
|
|
83
|
-
/**
|
|
84
|
-
* 已错误次数
|
|
85
|
-
*/
|
|
86
|
-
errorCount: 0,
|
|
87
|
-
/**
|
|
88
|
-
* 已尝试次数
|
|
89
|
-
*/
|
|
90
|
-
tryCount: 0,
|
|
91
|
-
/**
|
|
92
|
-
* 已被拒绝次数
|
|
93
|
-
*/
|
|
94
|
-
rejectCount: 0,
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 要求的数量
|
|
98
|
-
*/
|
|
99
|
-
requireCount: 0,
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 时间日志
|
|
103
|
-
* @type {HTMLElement}
|
|
104
|
-
*/
|
|
105
|
-
timeLog: null,
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 开始运行
|
|
109
|
-
*/
|
|
110
|
-
start() {
|
|
111
|
-
this.queryObj = this.queryToObj();
|
|
112
|
-
this.iframeCount = this.queryObj.thread || 10;
|
|
113
|
-
this.startTime = Date.now();
|
|
114
|
-
this.progressDiv = document.getElementById('message');
|
|
115
|
-
this.errorDiv = document.getElementById('error');
|
|
116
|
-
this.workersDiv = document.getElementById('workers');
|
|
117
|
-
this.timeLog = document.querySelector('#time-log tbody');
|
|
118
|
-
window.addEventListener('message', (e) => {
|
|
119
|
-
const { name, id, progress, round } = e.data;
|
|
120
|
-
if (name === 'infer_worker') {
|
|
121
|
-
this.accumulate(e.data);
|
|
122
|
-
} else if (name === 'infer_progress') {
|
|
123
|
-
this.updateWorkerStatus(progress, id, round);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
this.requireCount = Number(this.queryObj.count);
|
|
128
|
-
this.iframeCount = Math.min(this.requireCount, this.iframeCount);
|
|
129
|
-
this.workersDiv.innerHTML = `<div>使用${this.iframeCount}个工人执行任务</div>`;
|
|
130
|
-
this.iframes = [];
|
|
131
|
-
for (let i = 0; i < this.iframeCount; i++) {
|
|
132
|
-
const iframe = document.createElement('iframe');
|
|
133
|
-
let src = `../infer-worker.html?sid=${this.queryObj.sid}&infer_id=${i}`;
|
|
134
|
-
const {
|
|
135
|
-
fake_port,
|
|
136
|
-
real_port,
|
|
137
|
-
dev,
|
|
138
|
-
time_range,
|
|
139
|
-
status,
|
|
140
|
-
collector_id,
|
|
141
|
-
} = this.queryObj;
|
|
142
|
-
if (fake_port) {
|
|
143
|
-
src += `&fake_port=${fake_port}`;
|
|
144
|
-
}
|
|
145
|
-
if (real_port) {
|
|
146
|
-
src += `&real_port=${real_port}`;
|
|
147
|
-
}
|
|
148
|
-
if (dev) {
|
|
149
|
-
src += `&dev=${dev}`;
|
|
150
|
-
}
|
|
151
|
-
if (time_range) {
|
|
152
|
-
src += `&infer_time_range=${time_range}`;
|
|
153
|
-
}
|
|
154
|
-
if (status) {
|
|
155
|
-
src += `&infer_status=${status}`;
|
|
156
|
-
}
|
|
157
|
-
if (collector_id) {
|
|
158
|
-
src += `&infer_collector_id=${collector_id}`;
|
|
159
|
-
}
|
|
160
|
-
iframe.src = src;
|
|
161
|
-
iframe.style.display = 'none';
|
|
162
|
-
this.iframes.push(iframe);
|
|
163
|
-
document.body.appendChild(iframe);
|
|
164
|
-
|
|
165
|
-
const html = `<span>工人${i + 1}:</span>
|
|
166
|
-
<span class='worker_status'>准备中...</span>
|
|
167
|
-
<span class='worker_progress'></span>`;
|
|
168
|
-
const div = document.createElement('div');
|
|
169
|
-
div.setAttribute('id', 'infer_id_' + i);
|
|
170
|
-
div.innerHTML = html;
|
|
171
|
-
this.workersDiv.appendChild(div);
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 是否允许立刻发起后续的任务
|
|
177
|
-
* 如果可以发起则会立即发送下一个任务,不会等待当前任务的完整与否,
|
|
178
|
-
* 这样可以更多并发请求,速度更快
|
|
179
|
-
* 单页要注意不能导致成功总数超标
|
|
180
|
-
*/
|
|
181
|
-
enableImmediateNextTask() {
|
|
182
|
-
// 如果当前已经发起并在等待中的任务数小于剩余要求的任务数时才发起
|
|
183
|
-
// 否则先不要发起,如果已发出的人有失败的场合则会继续发起.
|
|
184
|
-
// 如果不这样则可能发送过得的请求导致最终的成功总数大于期望数量
|
|
185
|
-
const result = this.postingCount < this.requireCount - this.successCount;
|
|
186
|
-
// console.log(postingCount, requireCount - successCount, result);
|
|
187
|
-
return result;
|
|
188
|
-
},
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 接受子任务的数据并防止到数据池中,同时尝试开启/唤醒消耗数据的任务
|
|
192
|
-
* @param {*} data
|
|
193
|
-
*/
|
|
194
|
-
accumulate(data) {
|
|
195
|
-
this.accumulated.push(data);
|
|
196
|
-
// console.log('receive and accumulate', data.type, accumulated.length)
|
|
197
|
-
if (!this.consumingStarted) {
|
|
198
|
-
// console.log('start/resume consume')
|
|
199
|
-
this.consumingStarted = true;
|
|
200
|
-
this.consume();
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* 消耗任务
|
|
206
|
-
*/
|
|
207
|
-
consume() {
|
|
208
|
-
if (this.ended) {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (!this.enableImmediateNextTask()) {
|
|
212
|
-
this.consumingStarted = false;
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
const data = this.accumulated.shift();
|
|
216
|
-
if (data) {
|
|
217
|
-
this.tryCount++;
|
|
218
|
-
const { type, message } = data;
|
|
219
|
-
|
|
220
|
-
if (type === 'END') {
|
|
221
|
-
if (this.queryObj.dry) {
|
|
222
|
-
this.successCount++;
|
|
223
|
-
this.handleProgress('success');
|
|
224
|
-
const count = data.data.result.answers.length;
|
|
225
|
-
const time = data.data.result.
|
|
226
|
-
const avg = Math.round(time / count);
|
|
227
|
-
console.log(time, count, avg); // eslint-disable-line
|
|
228
|
-
const row = document.createElement('tr');
|
|
229
|
-
row.innerHTML = `<td>${this.successCount}</td>
|
|
230
|
-
<td>${time}</td><td>${count}</td><td>${avg}</td>`;
|
|
231
|
-
this.timeLog.appendChild(row);
|
|
232
|
-
// console.log('dry run');
|
|
233
|
-
} else {
|
|
234
|
-
// console.log('start to post data');
|
|
235
|
-
this.postData(data.data)
|
|
236
|
-
.then(() => {
|
|
237
|
-
// console.log('post data success');
|
|
238
|
-
this.successCount++;
|
|
239
|
-
this.handleProgress('success');
|
|
240
|
-
})
|
|
241
|
-
.catch((err) => {
|
|
242
|
-
// console.error('post data error');
|
|
243
|
-
if (err === 'rejected') {
|
|
244
|
-
this.rejectCount++;
|
|
245
|
-
this.handleProgress('reject');
|
|
246
|
-
} else {
|
|
247
|
-
this.errorCount++;
|
|
248
|
-
this.handleProgress('error', message);
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
// console.log('immediately start next concurrent consume')
|
|
252
|
-
this.consume();
|
|
253
|
-
}
|
|
254
|
-
} else {
|
|
255
|
-
if (type === 'ERROR' || type === 'NEXT_FAIL' || type === 'TIMEOUT' || type === 'ABANDON') {
|
|
256
|
-
this.errorCount++;
|
|
257
|
-
this.handleProgress('error', message);
|
|
258
|
-
} else if (type === 'CHECK_END') {
|
|
259
|
-
this.skipCount++;
|
|
260
|
-
this.handleProgress('skip', message);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
} else {
|
|
264
|
-
// console.log('consume out all, pause');
|
|
265
|
-
this.consumingStarted = false;
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* 发起请求
|
|
271
|
-
*/
|
|
272
|
-
postData(data) {
|
|
273
|
-
this.postingCount++;
|
|
274
|
-
return new Promise((resolve, reject) => {
|
|
275
|
-
const req = new XMLHttpRequest();
|
|
276
|
-
req.onload = () => {
|
|
277
|
-
this.postingCount--;
|
|
278
|
-
if (req.status >= 200 && req.status < 300) {
|
|
279
|
-
resolve();
|
|
280
|
-
} else {
|
|
281
|
-
reject();
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
req.onerror = () => {
|
|
285
|
-
this.postingCount--;
|
|
286
|
-
reject();
|
|
287
|
-
};
|
|
288
|
-
req.open('POST', data.url);
|
|
289
|
-
req.setRequestHeader('content-type', 'application/json');
|
|
290
|
-
req.setRequestHeader('Authorization', 'bearer ' + data.jwt);
|
|
291
|
-
req.send(JSON.stringify(data.result));
|
|
292
|
-
return req;
|
|
293
|
-
});
|
|
294
|
-
},
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* URL参数转换
|
|
298
|
-
*/
|
|
299
|
-
queryToObj() {
|
|
300
|
-
const query = location.href.split('?')[1];
|
|
301
|
-
const parts = query.split('&');
|
|
302
|
-
const obj = {};
|
|
303
|
-
parts.forEach((part) => {
|
|
304
|
-
const pair = part.split('=');
|
|
305
|
-
obj[pair[0]] = pair[1];
|
|
306
|
-
});
|
|
307
|
-
return obj;
|
|
308
|
-
},
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* 是否应该终止任务
|
|
312
|
-
*/
|
|
313
|
-
shouldEnd() {
|
|
314
|
-
if (this.successCount >= this.requireCount || this.rejectCount) {
|
|
315
|
-
// console.log('should abandon', successCount, tryCount)
|
|
316
|
-
return true;
|
|
317
|
-
}
|
|
318
|
-
const { max_error_count, max_try_count, max_skip_count } = this.queryObj;
|
|
319
|
-
if (max_try_count && this.tryCount >= max_try_count) {
|
|
320
|
-
return true;
|
|
321
|
-
}
|
|
322
|
-
if (max_error_count && this.errorCount >= max_error_count) {
|
|
323
|
-
return true;
|
|
324
|
-
}
|
|
325
|
-
if (max_skip_count && this.skipCount >= max_skip_count) {
|
|
326
|
-
return true;
|
|
327
|
-
}
|
|
328
|
-
return false;
|
|
329
|
-
},
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* 更新工人状态
|
|
333
|
-
* @param {*} progress
|
|
334
|
-
* @param {*} id
|
|
335
|
-
* @param {*} round
|
|
336
|
-
*/
|
|
337
|
-
updateWorkerStatus(progress, id, round) {
|
|
338
|
-
if (this.ended) {
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
if (window.parent === window) {
|
|
342
|
-
const { percent, fullLength, index } = progress;
|
|
343
|
-
const elem = this.workersDiv.querySelector('#infer_id_' + id);
|
|
344
|
-
elem.querySelector('.worker_status').textContent = `轮次: ${round} `;
|
|
345
|
-
elem.querySelector(
|
|
346
|
-
'.worker_progress'
|
|
347
|
-
).textContent = `进度: ${index}/${fullLength} ${percent}%`;
|
|
348
|
-
}
|
|
349
|
-
},
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* 处理进度
|
|
353
|
-
* @param {*} type
|
|
354
|
-
* @param {*} message
|
|
355
|
-
*/
|
|
356
|
-
handleProgress(type, message) {
|
|
357
|
-
// console.log('consumed ', tryCount, successCount);
|
|
358
|
-
if (this.shouldEnd()) {
|
|
359
|
-
// console.log('complete and abandon', accumulated.length)
|
|
360
|
-
this.ended = true;
|
|
361
|
-
let pop = this.iframes.pop();
|
|
362
|
-
while (pop) {
|
|
363
|
-
document.body.removeChild(pop);
|
|
364
|
-
pop = this.iframes.pop();
|
|
365
|
-
}
|
|
366
|
-
const statusList = this.workersDiv.querySelectorAll('.worker_status');
|
|
367
|
-
const progressList = this.workersDiv.querySelectorAll('.worker_progress');
|
|
368
|
-
statusList.forEach((item) => (item.innerHTML = '收工'));
|
|
369
|
-
progressList.forEach((item) => (item.innerHTML = ''));
|
|
370
|
-
}
|
|
371
|
-
if (window.parent !== window) {
|
|
372
|
-
window.parent.postMessage(
|
|
373
|
-
{
|
|
374
|
-
name: 'infer_result',
|
|
375
|
-
requireCount: this.requireCount,
|
|
376
|
-
successCount: this.successCount,
|
|
377
|
-
skipCount: this.skipCount,
|
|
378
|
-
errorCount: this.errorCount,
|
|
379
|
-
tryCount: this.tryCount,
|
|
380
|
-
rejectCount: this.rejectCount,
|
|
381
|
-
ended: this.ended,
|
|
382
|
-
cost: this.ended ? Date.now() - this.startTime : undefined,
|
|
383
|
-
type,
|
|
384
|
-
message,
|
|
385
|
-
},
|
|
386
|
-
'*'
|
|
387
|
-
);
|
|
388
|
-
} else {
|
|
389
|
-
const text = [
|
|
390
|
-
this.ended
|
|
391
|
-
? `<div>已结束,耗时: ${(Date.now() - this.startTime) / 1000}s</div>`
|
|
392
|
-
: `<div>运行中...</div>`,
|
|
393
|
-
`<div>要求总数量:${this.requireCount}</div>`,
|
|
394
|
-
`<div>已尝试次数:${this.tryCount}</div>`,
|
|
395
|
-
`<div style='color: green'>已成功次数:${this.successCount}</div>`,
|
|
396
|
-
`<div style='color: red'>出错误次数:${this.errorCount}</div>`,
|
|
397
|
-
`<div style='color: orange'>被甄别次数:${this.skipCount}</div>`,
|
|
398
|
-
`<div style='color: tomato'>被拒绝次数:${this.rejectCount}</div>`,
|
|
399
|
-
].join('\n');
|
|
400
|
-
this.progressDiv.innerHTML = text;
|
|
401
|
-
if (message) {
|
|
402
|
-
const error = `<div style='color: ${this.typeColorMap[type]}'>${message}</div>`;
|
|
403
|
-
this.errorDiv.innerHTML += error;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
if (!this.ended) {
|
|
408
|
-
// console.log('next consume start');
|
|
409
|
-
this.consume();
|
|
410
|
-
}
|
|
411
|
-
},
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
inferCommander.start();
|
|
1
|
+
const inferCommander = {
|
|
2
|
+
/**
|
|
3
|
+
* 接受子任务发来的数据结果池
|
|
4
|
+
* 可能一下子接到很多工人发来的结果,来不及上传
|
|
5
|
+
* 先积累到这里
|
|
6
|
+
*/
|
|
7
|
+
accumulated: [],
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 是否开始了消耗数据结果池
|
|
11
|
+
*/
|
|
12
|
+
consumingStarted: false,
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 是否已经完成
|
|
16
|
+
*/
|
|
17
|
+
ended: false,
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 当前已发起并等待结果的请求数量
|
|
21
|
+
*/
|
|
22
|
+
postingCount: 0,
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 查询参数
|
|
26
|
+
* @type {{
|
|
27
|
+
* max_try_count:string;
|
|
28
|
+
* max_error_count:string;
|
|
29
|
+
* thread: string;
|
|
30
|
+
* dry: string;
|
|
31
|
+
* max_skip_count: string;
|
|
32
|
+
* sid: string;
|
|
33
|
+
* fake_port: string;
|
|
34
|
+
* real_port: string;
|
|
35
|
+
* dev: string;
|
|
36
|
+
* time_range: string;
|
|
37
|
+
* status: string;
|
|
38
|
+
* collector_id: string;
|
|
39
|
+
* }}
|
|
40
|
+
*/
|
|
41
|
+
queryObj: null,
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 派遣的工人个数
|
|
45
|
+
* @type {number}
|
|
46
|
+
*/
|
|
47
|
+
iframeCount: 0,
|
|
48
|
+
/**
|
|
49
|
+
* 开始时间
|
|
50
|
+
* @type {number}
|
|
51
|
+
*/
|
|
52
|
+
startTime: 0,
|
|
53
|
+
/**
|
|
54
|
+
* 消息展示div
|
|
55
|
+
* @type {HTMLElement}
|
|
56
|
+
*/
|
|
57
|
+
messageDiv: null,
|
|
58
|
+
/**
|
|
59
|
+
* 错误展示div
|
|
60
|
+
* @type {HTMLElement}
|
|
61
|
+
*/
|
|
62
|
+
errorDiv: null,
|
|
63
|
+
/**
|
|
64
|
+
* 工人状态展示div
|
|
65
|
+
* @type {HTMLElement}
|
|
66
|
+
*/
|
|
67
|
+
workersDiv: null,
|
|
68
|
+
/**
|
|
69
|
+
* 消息颜色表
|
|
70
|
+
*/
|
|
71
|
+
typeColorMap: {
|
|
72
|
+
error: 'red',
|
|
73
|
+
skip: 'orange',
|
|
74
|
+
},
|
|
75
|
+
/**
|
|
76
|
+
* 已成功次数
|
|
77
|
+
*/
|
|
78
|
+
successCount: 0,
|
|
79
|
+
/**
|
|
80
|
+
* 已跳过次数
|
|
81
|
+
*/
|
|
82
|
+
skipCount: 0,
|
|
83
|
+
/**
|
|
84
|
+
* 已错误次数
|
|
85
|
+
*/
|
|
86
|
+
errorCount: 0,
|
|
87
|
+
/**
|
|
88
|
+
* 已尝试次数
|
|
89
|
+
*/
|
|
90
|
+
tryCount: 0,
|
|
91
|
+
/**
|
|
92
|
+
* 已被拒绝次数
|
|
93
|
+
*/
|
|
94
|
+
rejectCount: 0,
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 要求的数量
|
|
98
|
+
*/
|
|
99
|
+
requireCount: 0,
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 时间日志
|
|
103
|
+
* @type {HTMLElement}
|
|
104
|
+
*/
|
|
105
|
+
timeLog: null,
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 开始运行
|
|
109
|
+
*/
|
|
110
|
+
start() {
|
|
111
|
+
this.queryObj = this.queryToObj();
|
|
112
|
+
this.iframeCount = this.queryObj.thread || 10;
|
|
113
|
+
this.startTime = Date.now();
|
|
114
|
+
this.progressDiv = document.getElementById('message');
|
|
115
|
+
this.errorDiv = document.getElementById('error');
|
|
116
|
+
this.workersDiv = document.getElementById('workers');
|
|
117
|
+
this.timeLog = document.querySelector('#time-log tbody');
|
|
118
|
+
window.addEventListener('message', (e) => {
|
|
119
|
+
const { name, id, progress, round } = e.data;
|
|
120
|
+
if (name === 'infer_worker') {
|
|
121
|
+
this.accumulate(e.data);
|
|
122
|
+
} else if (name === 'infer_progress') {
|
|
123
|
+
this.updateWorkerStatus(progress, id, round);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
this.requireCount = Number(this.queryObj.count);
|
|
128
|
+
this.iframeCount = Math.min(this.requireCount, this.iframeCount);
|
|
129
|
+
this.workersDiv.innerHTML = `<div>使用${this.iframeCount}个工人执行任务</div>`;
|
|
130
|
+
this.iframes = [];
|
|
131
|
+
for (let i = 0; i < this.iframeCount; i++) {
|
|
132
|
+
const iframe = document.createElement('iframe');
|
|
133
|
+
let src = `../infer-worker.html?sid=${this.queryObj.sid}&infer_id=${i}`;
|
|
134
|
+
const {
|
|
135
|
+
fake_port,
|
|
136
|
+
real_port,
|
|
137
|
+
dev,
|
|
138
|
+
time_range,
|
|
139
|
+
status,
|
|
140
|
+
collector_id,
|
|
141
|
+
} = this.queryObj;
|
|
142
|
+
if (fake_port) {
|
|
143
|
+
src += `&fake_port=${fake_port}`;
|
|
144
|
+
}
|
|
145
|
+
if (real_port) {
|
|
146
|
+
src += `&real_port=${real_port}`;
|
|
147
|
+
}
|
|
148
|
+
if (dev) {
|
|
149
|
+
src += `&dev=${dev}`;
|
|
150
|
+
}
|
|
151
|
+
if (time_range) {
|
|
152
|
+
src += `&infer_time_range=${time_range}`;
|
|
153
|
+
}
|
|
154
|
+
if (status) {
|
|
155
|
+
src += `&infer_status=${status}`;
|
|
156
|
+
}
|
|
157
|
+
if (collector_id) {
|
|
158
|
+
src += `&infer_collector_id=${collector_id}`;
|
|
159
|
+
}
|
|
160
|
+
iframe.src = src;
|
|
161
|
+
iframe.style.display = 'none';
|
|
162
|
+
this.iframes.push(iframe);
|
|
163
|
+
document.body.appendChild(iframe);
|
|
164
|
+
|
|
165
|
+
const html = `<span>工人${i + 1}:</span>
|
|
166
|
+
<span class='worker_status'>准备中...</span>
|
|
167
|
+
<span class='worker_progress'></span>`;
|
|
168
|
+
const div = document.createElement('div');
|
|
169
|
+
div.setAttribute('id', 'infer_id_' + i);
|
|
170
|
+
div.innerHTML = html;
|
|
171
|
+
this.workersDiv.appendChild(div);
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 是否允许立刻发起后续的任务
|
|
177
|
+
* 如果可以发起则会立即发送下一个任务,不会等待当前任务的完整与否,
|
|
178
|
+
* 这样可以更多并发请求,速度更快
|
|
179
|
+
* 单页要注意不能导致成功总数超标
|
|
180
|
+
*/
|
|
181
|
+
enableImmediateNextTask() {
|
|
182
|
+
// 如果当前已经发起并在等待中的任务数小于剩余要求的任务数时才发起
|
|
183
|
+
// 否则先不要发起,如果已发出的人有失败的场合则会继续发起.
|
|
184
|
+
// 如果不这样则可能发送过得的请求导致最终的成功总数大于期望数量
|
|
185
|
+
const result = this.postingCount < this.requireCount - this.successCount;
|
|
186
|
+
// console.log(postingCount, requireCount - successCount, result);
|
|
187
|
+
return result;
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 接受子任务的数据并防止到数据池中,同时尝试开启/唤醒消耗数据的任务
|
|
192
|
+
* @param {*} data
|
|
193
|
+
*/
|
|
194
|
+
accumulate(data) {
|
|
195
|
+
this.accumulated.push(data);
|
|
196
|
+
// console.log('receive and accumulate', data.type, accumulated.length)
|
|
197
|
+
if (!this.consumingStarted) {
|
|
198
|
+
// console.log('start/resume consume')
|
|
199
|
+
this.consumingStarted = true;
|
|
200
|
+
this.consume();
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 消耗任务
|
|
206
|
+
*/
|
|
207
|
+
consume() {
|
|
208
|
+
if (this.ended) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (!this.enableImmediateNextTask()) {
|
|
212
|
+
this.consumingStarted = false;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const data = this.accumulated.shift();
|
|
216
|
+
if (data) {
|
|
217
|
+
this.tryCount++;
|
|
218
|
+
const { type, message } = data;
|
|
219
|
+
|
|
220
|
+
if (type === 'END') {
|
|
221
|
+
if (this.queryObj.dry) {
|
|
222
|
+
this.successCount++;
|
|
223
|
+
this.handleProgress('success');
|
|
224
|
+
const count = data.data.result.answers.length;
|
|
225
|
+
const time = data.data.result.time_consuming;
|
|
226
|
+
const avg = Math.round(time / count);
|
|
227
|
+
console.log(time, count, avg); // eslint-disable-line
|
|
228
|
+
const row = document.createElement('tr');
|
|
229
|
+
row.innerHTML = `<td>${this.successCount}</td>
|
|
230
|
+
<td>${time}</td><td>${count}</td><td>${avg}</td>`;
|
|
231
|
+
this.timeLog.appendChild(row);
|
|
232
|
+
// console.log('dry run');
|
|
233
|
+
} else {
|
|
234
|
+
// console.log('start to post data');
|
|
235
|
+
this.postData(data.data)
|
|
236
|
+
.then(() => {
|
|
237
|
+
// console.log('post data success');
|
|
238
|
+
this.successCount++;
|
|
239
|
+
this.handleProgress('success');
|
|
240
|
+
})
|
|
241
|
+
.catch((err) => {
|
|
242
|
+
// console.error('post data error');
|
|
243
|
+
if (err === 'rejected') {
|
|
244
|
+
this.rejectCount++;
|
|
245
|
+
this.handleProgress('reject');
|
|
246
|
+
} else {
|
|
247
|
+
this.errorCount++;
|
|
248
|
+
this.handleProgress('error', message);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
// console.log('immediately start next concurrent consume')
|
|
252
|
+
this.consume();
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
if (type === 'ERROR' || type === 'NEXT_FAIL' || type === 'TIMEOUT' || type === 'ABANDON') {
|
|
256
|
+
this.errorCount++;
|
|
257
|
+
this.handleProgress('error', message);
|
|
258
|
+
} else if (type === 'CHECK_END') {
|
|
259
|
+
this.skipCount++;
|
|
260
|
+
this.handleProgress('skip', message);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
// console.log('consume out all, pause');
|
|
265
|
+
this.consumingStarted = false;
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 发起请求
|
|
271
|
+
*/
|
|
272
|
+
postData(data) {
|
|
273
|
+
this.postingCount++;
|
|
274
|
+
return new Promise((resolve, reject) => {
|
|
275
|
+
const req = new XMLHttpRequest();
|
|
276
|
+
req.onload = () => {
|
|
277
|
+
this.postingCount--;
|
|
278
|
+
if (req.status >= 200 && req.status < 300) {
|
|
279
|
+
resolve();
|
|
280
|
+
} else {
|
|
281
|
+
reject();
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
req.onerror = () => {
|
|
285
|
+
this.postingCount--;
|
|
286
|
+
reject();
|
|
287
|
+
};
|
|
288
|
+
req.open('POST', data.url);
|
|
289
|
+
req.setRequestHeader('content-type', 'application/json');
|
|
290
|
+
req.setRequestHeader('Authorization', 'bearer ' + data.jwt);
|
|
291
|
+
req.send(JSON.stringify(data.result));
|
|
292
|
+
return req;
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* URL参数转换
|
|
298
|
+
*/
|
|
299
|
+
queryToObj() {
|
|
300
|
+
const query = location.href.split('?')[1];
|
|
301
|
+
const parts = query.split('&');
|
|
302
|
+
const obj = {};
|
|
303
|
+
parts.forEach((part) => {
|
|
304
|
+
const pair = part.split('=');
|
|
305
|
+
obj[pair[0]] = pair[1];
|
|
306
|
+
});
|
|
307
|
+
return obj;
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 是否应该终止任务
|
|
312
|
+
*/
|
|
313
|
+
shouldEnd() {
|
|
314
|
+
if (this.successCount >= this.requireCount || this.rejectCount) {
|
|
315
|
+
// console.log('should abandon', successCount, tryCount)
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
const { max_error_count, max_try_count, max_skip_count } = this.queryObj;
|
|
319
|
+
if (max_try_count && this.tryCount >= max_try_count) {
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
if (max_error_count && this.errorCount >= max_error_count) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
if (max_skip_count && this.skipCount >= max_skip_count) {
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
return false;
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 更新工人状态
|
|
333
|
+
* @param {*} progress
|
|
334
|
+
* @param {*} id
|
|
335
|
+
* @param {*} round
|
|
336
|
+
*/
|
|
337
|
+
updateWorkerStatus(progress, id, round) {
|
|
338
|
+
if (this.ended) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (window.parent === window) {
|
|
342
|
+
const { percent, fullLength, index } = progress;
|
|
343
|
+
const elem = this.workersDiv.querySelector('#infer_id_' + id);
|
|
344
|
+
elem.querySelector('.worker_status').textContent = `轮次: ${round} `;
|
|
345
|
+
elem.querySelector(
|
|
346
|
+
'.worker_progress'
|
|
347
|
+
).textContent = `进度: ${index}/${fullLength} ${percent}%`;
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 处理进度
|
|
353
|
+
* @param {*} type
|
|
354
|
+
* @param {*} message
|
|
355
|
+
*/
|
|
356
|
+
handleProgress(type, message) {
|
|
357
|
+
// console.log('consumed ', tryCount, successCount);
|
|
358
|
+
if (this.shouldEnd()) {
|
|
359
|
+
// console.log('complete and abandon', accumulated.length)
|
|
360
|
+
this.ended = true;
|
|
361
|
+
let pop = this.iframes.pop();
|
|
362
|
+
while (pop) {
|
|
363
|
+
document.body.removeChild(pop);
|
|
364
|
+
pop = this.iframes.pop();
|
|
365
|
+
}
|
|
366
|
+
const statusList = this.workersDiv.querySelectorAll('.worker_status');
|
|
367
|
+
const progressList = this.workersDiv.querySelectorAll('.worker_progress');
|
|
368
|
+
statusList.forEach((item) => (item.innerHTML = '收工'));
|
|
369
|
+
progressList.forEach((item) => (item.innerHTML = ''));
|
|
370
|
+
}
|
|
371
|
+
if (window.parent !== window) {
|
|
372
|
+
window.parent.postMessage(
|
|
373
|
+
{
|
|
374
|
+
name: 'infer_result',
|
|
375
|
+
requireCount: this.requireCount,
|
|
376
|
+
successCount: this.successCount,
|
|
377
|
+
skipCount: this.skipCount,
|
|
378
|
+
errorCount: this.errorCount,
|
|
379
|
+
tryCount: this.tryCount,
|
|
380
|
+
rejectCount: this.rejectCount,
|
|
381
|
+
ended: this.ended,
|
|
382
|
+
cost: this.ended ? Date.now() - this.startTime : undefined,
|
|
383
|
+
type,
|
|
384
|
+
message,
|
|
385
|
+
},
|
|
386
|
+
'*'
|
|
387
|
+
);
|
|
388
|
+
} else {
|
|
389
|
+
const text = [
|
|
390
|
+
this.ended
|
|
391
|
+
? `<div>已结束,耗时: ${(Date.now() - this.startTime) / 1000}s</div>`
|
|
392
|
+
: `<div>运行中...</div>`,
|
|
393
|
+
`<div>要求总数量:${this.requireCount}</div>`,
|
|
394
|
+
`<div>已尝试次数:${this.tryCount}</div>`,
|
|
395
|
+
`<div style='color: green'>已成功次数:${this.successCount}</div>`,
|
|
396
|
+
`<div style='color: red'>出错误次数:${this.errorCount}</div>`,
|
|
397
|
+
`<div style='color: orange'>被甄别次数:${this.skipCount}</div>`,
|
|
398
|
+
`<div style='color: tomato'>被拒绝次数:${this.rejectCount}</div>`,
|
|
399
|
+
].join('\n');
|
|
400
|
+
this.progressDiv.innerHTML = text;
|
|
401
|
+
if (message) {
|
|
402
|
+
const error = `<div style='color: ${this.typeColorMap[type]}'>${message}</div>`;
|
|
403
|
+
this.errorDiv.innerHTML += error;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (!this.ended) {
|
|
408
|
+
// console.log('next consume start');
|
|
409
|
+
this.consume();
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
inferCommander.start();
|