Repository navigation
Expand file tree
/
Copy pathlogParser.js
More file actions
540 lines (488 loc) · 20.4 KB
/
Copy pathlogParser.js
File metadata and controls
540 lines (488 loc) · 20.4 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
const ANSI_RE = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g')
export function stripAnsi(str) {
return typeof str === 'string' ? str.replace(ANSI_RE, '') : str
}
const LINE_RE = /^\[([^\]]*)\] \[([^\]]*)\] \[(INFO|WARN|ERROR|DEBUG)\] (MAIN|MOBILE|DESKTOP) \[([^\]]*)\] ([\s\S]*)$/
const SEVERITY = { debug: 0, info: 1, warn: 2, error: 3 }
export function severityRank(level) {
return SEVERITY[level] ?? 1
}
export function parseLogLine(rawInput, source = 'stdout') {
const raw = stripAnsi(String(rawInput))
const match = raw.match(LINE_RE)
if (match) {
const [, ts, user, levelTag, platform, title, message] = match
return {
ts,
level: levelTag.toLowerCase(),
user: user || null,
platform,
title,
message,
source,
parsed: true,
raw
}
}
let level = source === 'stderr' ? 'error' : 'info'
if (/\b(ERROR|Error:|ERR!|FATAL|Traceback|Unhandled)\b/.test(raw)) level = 'error'
else if (/\b(WARN|WARNING|Deprecat)/i.test(raw)) level = 'warn'
return {
ts: null,
level,
user: null,
platform: null,
title: null,
message: raw,
source,
parsed: false,
raw
}
}
export function createRunState() {
return {
version: null,
clusters: null,
accountsTotal: null,
currentEmail: null,
userToEmail: {}, // log "user" (email localpart) -> full email, for attributing live lines
lastPointUpdateAt: null,
totals: null, // { collected, oldTotal, newTotal, runtimeMinutes, accountsProcessed }
order: [], // emails in the order they started
accounts: {}, // email -> account summary
errors: [], // recent error/warn messages { ts, level, title, message }
finished: false,
pendingDelay: null // { seconds, nextEmail, sinceTs } while waiting between accounts/workers
}
}
function ensureAccount(state, email) {
if (!email) return null
if (!state.accounts[email]) {
state.accounts[email] = {
email,
geoLocale: null,
locale: null,
cachedRegion: null,
initialPoints: null,
collectedPoints: null,
finalPoints: null,
earnable: null, // { mobile, browser, app }; browser is the desktop search pool
searchSummary: null, // { mobile, desktop, bonus, total }
streakProtection: null, // { enabled, remainingDays, streakCounter, updatedAt }
edgeBrowsing: null, // background Edge activity progress and ETA
durationSeconds: null,
success: null,
error: null,
live: {
balance: null, // latest known available-points balance
gained: 0, // points earned so far this run (per this account)
bySource: {}, // keyed by normalized activity source
lastUpdateTs: null
}
}
state.order.push(email)
}
return state.accounts[email]
}
// All patterns adapted for the Chinese-localized fork of Microsoft-Rewards-Script.
// The field names (offers=, pointsGained=, currentBalance=, etc.) remain in English
// inside the log messages, but the surrounding text is Chinese.
const RE = {
runStart: /^启动微软奖励脚本 \| v(\S+) \| 账户数: (\d+) \| 集群数: (\d+)/,
accountStart:
/^开始处理账户: (\S+) \| geoLocale: ([^|]+?)(?: \| locale: (\S+))?(?: \| 缓存区域: (\S+))?\s*$/,
earnable: /^今日可赚取 \| 移动端: (\d+) \| 浏览器: (\d+) \| App: (\d+) \| (\S+) \| locale: (\S+)\s*$/,
searchSummary: /^搜索汇总 \| 移动端=(-?\d+) \| 桌面端=(-?\d+) \| 额外=(-?\d+) \| 总计=(-?\d+)/,
streakProtection:
/^快照完成 \| offers=(\d+) \| 可上报=(\d+) \| streaks=(\d+) \| 连续保护已启用=(true|false|null) \| 连续保护剩余天数=(\d+|null) \| 连续计数=(\d+|null) \| 等级=([^|]+) \| 账户=(\S+@\S+)$/,
accountEnd:
/^账户完成: (\S+) \| 获得积分=(-?\d+) \| 原余额=(\d+) \| 现余额=(\d+) \| 持续秒数=([\d.]+)/,
runEnd: /^全部账户完成 \| 处理账户数=(\d+) \| 获得积分=(-?\d+) \| 原余额=(\d+) \| 现余额=(\d+) \| 运行分钟数=([\d.]+)/,
accountError: /^(\S+@\S+) \| 错误=([\s\S]+)$/,
flowFailed: /(\S+@\S+) 的.*流程失败:/i,
accountDelay: /^等待 ([\d.]+) 秒后开始下一个账户(?: \((\S+@\S+)\))?$/,
searchStart: /^开始必应搜索 \| 当前余额=(\d+)/,
flowCollected: /^积分已收集 \| 获得积分=(-?\d+) \| 现余额=(\d+) \| 账户=(\S+@\S+)/
}
function numericField(message, name) {
const match = message.match(new RegExp(`(?:^| \\| )${name}=(-?\\d+(?:\\.\\d+)?)(?= \\| |$)`))
return match ? Number(match[1]) : null
}
function fractionField(message, name) {
const match = message.match(new RegExp(`(?:^| \\| )${name}=(\\d+)\\/(\\d+)(?= \\| |$)`))
return match ? { current: Number(match[1]), total: Number(match[2]) } : null
}
function eventTime(entry) {
return entry.receivedAt ?? entry.ts ?? null
}
function accountEmailForEntry(state, entry) {
return (entry.user && state.userToEmail[entry.user]) || state.currentEmail || null
}
function pointEventSource(title, message) {
switch (title) {
case 'SEARCH-BING':
return message.startsWith('必应搜索完成') ? 'search' : null
case 'SEARCH-BONUS':
return message.startsWith('必应搜索完成') ? 'bonus' : null
case 'READ-TO-EARN':
return (message.startsWith('文章后的积分变化') ||
message.startsWith('未获得积分,停止读文赚积分') ||
message.startsWith('已阅读第') ||
message.startsWith('读文赚积分完成')) ? 'read' : null
case 'DAILY-CHECK-IN':
return message.startsWith('每日签到完成') ? 'checkIn' : null
case 'CLAIM-BONUS-POINTS':
return (message.startsWith('领取奖励积分完成') || message.startsWith('没有可领取的积分'))
? 'claimBonus'
: null
case 'CLAIM-REWARD':
return message.startsWith('奖励已领取') ? 'claimReward' : null
case 'URL-REWARD':
return (message.startsWith('UrlReward 完成') || message.startsWith('UrlReward 未获得积分'))
? 'urlReward'
: null
case 'VISUAL-SEARCH':
return (message.startsWith('每日视觉搜索完成') || message.startsWith('每日视觉搜索已记录'))
? 'visualSearch'
: null
case 'APP-REWARD':
return (message.startsWith('AppReward 完成') || message.startsWith('AppReward 完成但未获得积分'))
? 'appReward'
: null
case 'PUNCHCARD':
return (message.includes('COMPLETE') || message.includes('in progress')) ? 'punchcard' : null
case 'SEARCH-ON-BING-SEARCH':
return message.startsWith('SearchOnBing 活动完成') ? 'searchOnBing' : null
default:
return null
}
}
function applyLivePoints(state, entry) {
const msg = entry.message ?? ''
const target = email => ensureAccount(state, email || accountEmailForEntry(state, entry))
const num = s => {
const n = Number(s)
return Number.isFinite(n) ? n : null
}
const touch = acc => {
const at = eventTime(entry)
acc.live.lastUpdateTs = at
state.lastPointUpdateAt = at
}
const setBalance = (acc, balance) => {
if (!acc || balance == null) return false
if (acc.live.balance === balance) return false
acc.live.balance = balance
touch(acc)
return true
}
const addGain = (acc, gained, balance, source) => {
if (!acc) return false
let changed = false
if (balance != null && acc.live.balance !== balance) {
acc.live.balance = balance
changed = true
}
if (gained > 0) {
acc.live.gained += gained
acc.live.bySource[source] = (acc.live.bySource[source] || 0) + gained
changed = true
}
if (changed) touch(acc)
return changed
}
let m
if ((entry.title === 'SEARCH-BING' || entry.title === 'SEARCH-BONUS') && (m = msg.match(RE.searchStart))) {
return setBalance(target(), num(m[1]))
}
if (entry.title === 'FLOW' && (m = msg.match(RE.flowCollected))) {
const acc = target(m[3])
if (!acc) return false
const total = Number(m[1])
const balance = Number(m[2])
const changed = acc.live.gained !== total || acc.live.balance !== balance
acc.live.gained = total
acc.live.balance = balance
if (changed) touch(acc)
return changed
}
const source = pointEventSource(entry.title, msg)
if (!source) return false
const gained = numericField(msg, 'pointsGained')
const balance = numericField(msg, 'currentBalance')
if (gained == null && balance == null) return false
return addGain(target(), gained ?? 0, balance, source)
}
function applyEdgeBrowsing(state, entry) {
if (entry.title !== 'EDGE-BROWSING') return null
const account = ensureAccount(state, accountEmailForEntry(state, entry))
if (!account) return null
const message = entry.message ?? ''
const finalReports = message.startsWith('后台 Edge 浏览活动结束')
? numericField(message, 'reports')
: null
const progress =
fractionField(message, 'reportsCompleted') ??
fractionField(message, 'report') ??
(finalReports != null ? { current: finalReports, total: finalReports } : null)
const previous = account.edgeBrowsing ?? {
status: 'pending',
targetMinutes: null,
serverIntervalMinutes: null,
reportsCompleted: 0,
reportsTotal: null,
reportsRemaining: null,
scheduledMinutesCovered: 0,
nextReportInSeconds: null,
estimatedRemainingMinutes: null,
elapsedMinutes: null,
accepted: 0,
duplicates: 0,
failed: 0,
waitingForBackground: false,
updatedAt: null
}
if (message.startsWith('开始后台 Edge 浏览活动')) {
previous.status = 'running'
previous.targetMinutes = numericField(message, 'targetMinutes')
previous.serverIntervalMinutes = numericField(message, 'serverIntervalMinutes')
previous.reportsTotal = numericField(message, 'reports')
previous.reportsRemaining = previous.reportsTotal
previous.scheduledMinutesCovered = 0
previous.estimatedRemainingMinutes = numericField(message, 'estimatedDurationMinutes')
previous.waitingForBackground = false
} else if (message.startsWith('Edge 浏览进度')) {
previous.status = 'running'
} else if (message.startsWith('已提交 Edge 浏览报告')) {
previous.status = 'running'
previous.nextReportInSeconds = null
} else if (message.startsWith('后台 Edge 浏览活动结束')) {
const duplicates = numericField(message, 'duplicates') ?? previous.duplicates
const failed = numericField(message, 'failed') ?? previous.failed
const serverCompleteMatch = message.match(/(?:^| \| )serverComplete=(true|false)(?= \| |$)/)
const serverComplete = serverCompleteMatch ? serverCompleteMatch[1] === 'true' : null
if (serverComplete === true) previous.status = 'complete'
else if (serverComplete === false) previous.status = 'partial'
else previous.status = duplicates > 0 || failed > 0 ? 'partial' : 'complete'
previous.reportsRemaining = serverComplete === false ? previous.reportsRemaining : 0
previous.nextReportInSeconds = null
previous.estimatedRemainingMinutes = 0
previous.waitingForBackground = false
} else if (message === 'Edge 浏览连击(Browsing Streak)已完成') {
previous.status = 'complete'
previous.reportsRemaining = 0
previous.nextReportInSeconds = null
previous.estimatedRemainingMinutes = 0
previous.waitingForBackground = false
} else if (
message === '该账户无法使用 Edge 浏览连击(Browsing Streak)' ||
message.startsWith('跳过:')
) {
previous.status = 'skipped'
previous.waitingForBackground = false
} else if (
message.startsWith('后台 Edge 浏览活动失败') ||
message.startsWith('意外的后台任务失败')
) {
previous.status = 'failed'
previous.waitingForBackground = false
} else if (message === '后台活动已取消') {
previous.status = 'cancelled'
previous.waitingForBackground = false
} else if (message.startsWith('前台活动已完成;')) {
previous.waitingForBackground = true
} else {
return null
}
if (progress?.current != null) previous.reportsCompleted = progress.current
if (progress?.total != null) previous.reportsTotal = progress.total
const reportsRemaining = numericField(message, 'reportsRemaining')
const scheduledMinutes = fractionField(message, 'scheduledMinutesCovered')
const nextReportInSeconds = numericField(message, 'nextReportInSeconds')
const estimatedRemainingMinutes = numericField(message, 'estimatedRemainingMinutes')
const elapsedMinutes = numericField(message, 'elapsedMinutes')
const accepted = numericField(message, 'accepted')
const duplicates = numericField(message, 'duplicates')
const failed = numericField(message, 'failed')
if (reportsRemaining != null) previous.reportsRemaining = reportsRemaining
if (scheduledMinutes?.current != null) previous.scheduledMinutesCovered = scheduledMinutes.current
if (nextReportInSeconds != null) previous.nextReportInSeconds = nextReportInSeconds
if (estimatedRemainingMinutes != null) previous.estimatedRemainingMinutes = estimatedRemainingMinutes
if (elapsedMinutes != null) previous.elapsedMinutes = elapsedMinutes
if (accepted != null) previous.accepted = accepted
if (duplicates != null) previous.duplicates = duplicates
if (failed != null) previous.failed = failed
previous.updatedAt = eventTime(entry)
account.edgeBrowsing = previous
return 'edge-browsing'
}
export function applyLogToRunState(state, entry) {
const msg = entry.message ?? ''
if (entry.level === 'error' || entry.level === 'warn') {
state.errors.push({
ts: entry.ts,
level: entry.level,
title: entry.title,
message: msg
})
if (state.errors.length > 200) state.errors.shift()
const ff = msg.match(RE.flowFailed)
if (ff) {
const acc = ensureAccount(state, ff[1])
if (acc) {
acc.error = msg
acc.success = acc.success === true ? true : false
}
}
}
if (!entry.parsed) return null
if (applyLivePoints(state, entry)) return 'points'
const edgeBrowsingEvent = applyEdgeBrowsing(state, entry)
if (edgeBrowsingEvent) return edgeBrowsingEvent
let m
switch (entry.title) {
case 'RUN-START':
if ((m = msg.match(RE.runStart))) {
state.version = m[1]
state.accountsTotal = Number(m[2])
state.clusters = Number(m[3])
state.finished = false
state.pendingDelay = null
return 'run-start'
}
break
case 'ACCOUNT-START':
if ((m = msg.match(RE.accountStart))) {
const acc = ensureAccount(state, m[1])
if (acc) {
acc.geoLocale = m[2].trim()
acc.locale = m[3] || null
acc.cachedRegion = m[4] || null
}
state.currentEmail = m[1]
if (entry.user) state.userToEmail[entry.user] = m[1] // map localpart -> full email
state.pendingDelay = null
return 'account-start'
}
break
case 'ACCOUNT-DELAY':
if ((m = msg.match(RE.accountDelay))) {
state.pendingDelay = {
seconds: Number(m[1]),
nextEmail: m[2] || null,
sinceTs: eventTime(entry)
}
return 'account-delay'
}
break
case 'POINTS':
if ((m = msg.match(RE.earnable))) {
const email = m[4]
const acc = ensureAccount(state, email)
if (acc) {
acc.earnable = { mobile: Number(m[1]), browser: Number(m[2]), app: Number(m[3]) }
acc.locale ??= m[5]
}
state.currentEmail = email
}
break
case 'SEARCH-MANAGER':
if ((m = msg.match(RE.searchSummary))) {
const acc = ensureAccount(state, accountEmailForEntry(state, entry))
if (acc) {
acc.searchSummary = {
mobile: Number(m[1]),
desktop: Number(m[2]),
bonus: Number(m[3]),
total: Number(m[4])
}
}
}
break
case 'REACT-PARSE':
if ((m = msg.match(RE.streakProtection))) {
const acc = ensureAccount(state, m[8])
if (acc) {
acc.streakProtection = {
enabled: m[4] === 'null' ? null : m[4] === 'true',
remainingDays: m[5] === 'null' ? null : Number(m[5]),
streakCounter: m[6] === 'null' ? null : Number(m[6]),
updatedAt: eventTime(entry)
}
}
return 'streak-protection'
}
break
case 'ACCOUNT-END':
if ((m = msg.match(RE.accountEnd))) {
const acc = ensureAccount(state, m[1])
if (acc) {
acc.collectedPoints = Number(m[2])
acc.initialPoints = Number(m[3])
acc.finalPoints = Number(m[4])
acc.durationSeconds = Number(m[5])
acc.success = true
acc.live.gained = Number(m[2])
acc.live.balance = Number(m[4])
const at = eventTime(entry)
acc.live.lastUpdateTs = at
state.lastPointUpdateAt = at
}
return 'account-end'
}
break
case 'ACCOUNT-ERROR':
if ((m = msg.match(RE.accountError))) {
const acc = ensureAccount(state, m[1])
if (acc) {
acc.error = m[2].trim()
acc.success = false
}
return 'account-error'
}
break
case 'RUN-END':
if ((m = msg.match(RE.runEnd))) {
state.totals = {
accountsProcessed: Number(m[1]),
collected: Number(m[2]),
oldTotal: Number(m[3]),
newTotal: Number(m[4]),
runtimeMinutes: Number(m[5])
}
state.finished = true
state.pendingDelay = null
return 'run-end'
}
break
default:
break
}
return null
}
function accountCollected(a) {
if (typeof a.collectedPoints === 'number') return a.collectedPoints
return a.live?.gained ?? 0
}
export function summarizeRunState(state) {
const accounts = state.order.map(email => state.accounts[email])
const collected = state.totals?.collected ?? accounts.reduce((sum, a) => sum + accountCollected(a), 0)
const current = state.currentEmail ? state.accounts[state.currentEmail] : null
return {
version: state.version,
clusters: state.clusters,
accountsTotal: state.accountsTotal,
accountsSeen: accounts.length,
collected,
totals: state.totals,
finished: state.finished,
pendingDelay: state.pendingDelay,
live: {
currentAccount: state.currentEmail,
currentBalance: current?.live?.balance ?? null,
gained: collected,
updatedAt: state.lastPointUpdateAt
},
accounts
}
}