feat(scan-diag): 上报降噪 - 正常自愈不上行、健康快照改异常触发

背景:现场"扫码突然没反应"已定位为硬件问题(音响从平板 USB 口取电冲掉内部 USB Hub),
诊断/上报链路是唯一证据链,不能整体移除,因此只对"上行"降噪,本地证据全保留。

- QUIET_HEAL_REASONS=['event-type-lock-idle-reset']:设计内的正常自愈(每次扫码都会发生
  的事件类型锁重置)只写本地日志/logcat,不再进 setDiagSink,避免每 60s 给服务端刷一条噪音。
  diagHeal 内新增 reportedHeals 只统计真自愈。
- 健康快照:REPORT_HEALTH_INTERVAL_MS 10 分钟 -> 30 分钟,并改为"异常触发"才外送:
  ① 主线程卡顿(stall);② 真自愈;③ 活动突然中断(上一区间还在扫、本区间一个按键都没到,
  且门控 pageActive/paused 正常)。payload 带 trigger=stall|heal|activity-stop 与 stallDelta/healDelta。
- 保留:scan-stall / scan-drop(带条码) / scan-anomaly / 诊断面板手动上报 / 开关 clientErrorLog:disabled。
- 本地环形日志(160 条)、logcat(WMSScan/WMSPage)、诊断面板(标题 3 秒 5 连点) 均不变。
This commit is contained in:
liufumingren 2026-09-25 17:05:42 +08:00
parent a34b194211
commit 02db6c8a5a
2 changed files with 65 additions and 20 deletions

View File

@ -8,7 +8,10 @@
* 上报内容:
* 1. 运行时错误:Vue 渲染/生命周期异常、window error/unhandledrejection、uni 未处理 Promise;
* 2. 扫码链路异常(由 scan-diag 通过 setDiagSink 外送):
* scan-stall(主线程卡顿)/ scan-heal(自愈动作)/ scan-drop(扫码被丢弃,带收到的码);
* scan-stall(主线程卡顿)/ scan-heal(真自愈)/ scan-drop(扫码被丢弃,带收到的码)
* / scan-health(异常触发的健康快照,带 trigger 标明是卡顿、真自愈还是“活动突然中断”);
* 注:设计内的正常自愈(scan-diag 的 QUIET_HEAL_REASONS,如每次扫码都会发生的锁重置)
* 在 scan-diag 一侧就被拦下,不会进到这里(2026-09-25 上行降噪);
* 3. 现场手动上报(诊断面板「上报」按钮):scan-manual,带完整精简快照。
*
* 设计约束(务必保持):

View File

@ -8,6 +8,10 @@
* 2. 真机同步写 logcat(tag: WMSScan),release 包也能 `adb logcat -s WMSScan` 事后追踪。
* 3. 60 秒健康心跳:记录区间内按键数 / 丢弃增量 / 门控快照,
* 可精确定位「哪一分钟开始收不到按键」以及当时的门控状态。
* 4. 上行降噪(2026-09-25 用户要求):本地日志保留全部事实,**外送服务端只保留异常** ——
* 设计内的正常自愈(QUIET_HEAL_REASONS,如每次扫码都会发生的锁重置)不上行;
* 健康快照由「每 10 分钟定时」改成「出现异常才外送(最短 30 分钟间隔)」。
* 诊断面板 / logcat / 本地环形日志不受影响,仍是完整证据链。
*
* 约定:本模块只做记录,不做任何业务决策,任何异常都不得向上抛。
*/
@ -34,8 +38,13 @@ const KEY_RESUME_GAP_MS = 60 * 1000
const KEY_BURST_GAP_MS = 500
// 心跳定时器迟到判定:实际间隔 > 设定周期 * 该倍数 → 主线程被卡住
const STALL_FACTOR = 2.5
// 服务端健康快照间隔:服务端拿到连续轨迹,而不只是异常点
const REPORT_HEALTH_INTERVAL_MS = 10 * 60 * 1000
// 服务端健康快照的**最短间隔**:正常运行时基本不上行,只有区间内出现异常才带快照外送
// (异常判据见 diagHeartbeat:主线程卡顿 / 真自愈 / “上一区间还在扫、这一区间一个键都没到”)
const REPORT_HEALTH_INTERVAL_MS = 30 * 60 * 1000
// 属于“设计内正常行为”的自愈原因:只写本地日志/logcat,不作为异常外送服务端。
// event-type-lock-idle-reset 每次扫码都会发生(空闲 1.2s 后重置事件类型锁),
// 上报只会给服务端刷噪音并掩盖真正的自愈记录。
const QUIET_HEAL_REASONS = ['event-type-lock-idle-reset']
const state = {
scope: 'unknown',
@ -51,6 +60,10 @@ const state = {
healthKeys: 0,
healthDrops: 0,
healthHeals: 0,
reportedHeals: 0, // 真正外送服务端的自愈次数(不含 QUIET_HEAL_REASONS)
healthReportStalls: 0, // 上次外送快照时的 stallCount
healthReportHeals: 0, // 上次外送快照时的 reportedHeals
lastIntervalKeys: 0, // 上一个 60s 心跳区间的按键数(判断“活动突然中断”)
dropLoggedAt: {},
lastTickAt: 0,
maxTickGapMs: 0,
@ -386,6 +399,12 @@ export function diagFlush(info) {
export function diagHeal(reason, data) {
state.counters.heals += 1
push('heal', `reason=${reason} count=${state.counters.heals}`, data)
// 设计内的正常自愈(如每次扫码都会发生的锁重置)只留本地日志/logcat,
// 外送服务端只保留“真的把链路救回来”的那些,避免正常行为刷满上报库
if (QUIET_HEAL_REASONS.indexOf(String(reason || '')) >= 0) {
return
}
state.reportedHeals += 1
emitSink('heal', { reason, data })
}
@ -468,25 +487,44 @@ export function diagHeartbeat(options) {
`最近按键=${idleAgo} tick=${tickGap || intervalMs}ms 最大tick=${state.maxTickGapMs}ms 卡顿=${state.stallCount}`,
state.gates
)
// 服务端健康快照:本会话确实收到过按键后,每 10 分钟外送一次,
// 这样"某一刻开始收不到按键"即使无人上报也能在服务端看出来(轨迹而不是只有异常点)
// 服务端健康快照(降噪后):正常运行时不上行,只有区间内出现“值得事后追查”的异常才带快照外送:
// ① 主线程卡顿(stall);② 真自愈(非 QUIET_HEAL_REASONS);③ 活动突然中断
// (上一区间还在扫、本区间一个键都没到,且页面在用/未暂停——正是现场“扫不上”的形态)。
// 最短间隔 REPORT_HEALTH_INTERVAL_MS,避免异常持续时刷屏。
if (state.counters.keys > 0 && now - state.lastHealthReportAt >= REPORT_HEALTH_INTERVAL_MS) {
const keysSinceReport = state.counters.keys - state.healthReportKeys
state.lastHealthReportAt = now
state.healthReportKeys = state.counters.keys
emitSink('health', {
keysSinceReport,
keysTotal: state.counters.keys,
flushes: state.counters.flushes,
drops: state.counters.drops,
heals: state.counters.heals,
lastKeyAgoMs: state.lastKeyAt ? now - state.lastKeyAt : null,
stallCount: state.stallCount,
maxTickGapMs: state.maxTickGapMs,
gates: state.gates,
context: state.context,
})
const stallDelta = state.stallCount - state.healthReportStalls
const healDelta = state.reportedHeals - state.healthReportHeals
const activityStopped =
keys === 0 &&
state.lastIntervalKeys > 0 &&
state.gates.pageActive !== false &&
state.gates.paused !== true
if (stallDelta > 0 || healDelta > 0 || activityStopped) {
const keysSinceReport = state.counters.keys - state.healthReportKeys
state.lastHealthReportAt = now
state.healthReportKeys = state.counters.keys
state.healthReportStalls = state.stallCount
state.healthReportHeals = state.reportedHeals
emitSink('health', {
trigger: stallDelta > 0 ? 'stall' : healDelta > 0 ? 'heal' : 'activity-stop',
stallDelta,
healDelta,
keysSinceReport,
keysTotal: state.counters.keys,
flushes: state.counters.flushes,
drops: state.counters.drops,
heals: state.counters.heals,
reportedHeals: state.reportedHeals,
lastKeyAgoMs: state.lastKeyAt ? now - state.lastKeyAt : null,
stallCount: state.stallCount,
maxTickGapMs: state.maxTickGapMs,
gates: state.gates,
context: state.context,
})
}
}
// 记住本区间的按键数,下一次心跳用它判断“活动是否突然中断”
state.lastIntervalKeys = keys
}
/** 立即落盘(页面 onHide/onUnload 时调用,避免节流窗口内丢日志) */
@ -582,6 +620,10 @@ export function diagClear() {
state.healthDrops = 0
state.healthHeals = 0
state.lastHealthAt = 0
state.reportedHeals = 0
state.healthReportStalls = state.stallCount
state.healthReportHeals = 0
state.lastIntervalKeys = 0
state.dropLoggedAt = {}
push('note', 'log-cleared')
diagPersistNow()