Files
Jia Suiandfanlusky 2f7335be3e 统计增加首词命中率 (#963)
* feat(engine): webview 契约给统计概览增加首词命中率字段并支持 null 分支

* feat(server,ui-html): 统计新增首词命中率(决策点记录、stats v2 迁移与设置页展示)

* fix(server): 首词命中计数补日语标点排除与并发迁移复查

* fix(ui-html): 统计卡片改用自适应列宽,避免长数字被省略号截断

* fix(server,installer): 修复首词命中率随附测试在 CI 的失败

三条 server 用例是首词命中率改动新加、但从未跑绿:RawDb 在迁移用例里对尚不存在的库开只读写连接,补上 SQLITE_OPEN_CREATE;09-19 写了一次次选,按契约它是一次真实测量(0%),断言却要求 nullopt,改为断言 0.0,并把 nullopt 覆盖移到迁移用例(total==0 的那天)。安装器 preserve-user-data 从 stats_pipe.cpp 取库名,而 EnsureStatsStoreOpen 连同 stats.db 字面量已挪到 stats_candidate_capture.cpp,改到新位置。

* fix(server,ui-html): 首词命中率写库移出按键路径,修正迁移与统计口径

- 选词计数改由后台 SerialTaskRunner 写库,标点收尾与选词路径不再同步等 SQLite 锁
- 迁移以表结构为准,不再被版本号挡住;缺列时在 IMMEDIATE 事务里逐列补齐
- 标点在日期入口上收尾时按实际上屏的首条候选计数
- 五笔四码唯一自动上屏不计入;多段造词任一段非首位即不算命中
- 只有选词计数的日线不算打字日,不影响天数、日均与连续天数
- 卡片复用 formatFirstWordRatio 并展示累计命中率,概览复用 DailyRow::FirstWordRatio

---------

Co-authored-by: fanlusky <1730976608@qq.com>
2026-10-15 22:09:50 +08:00

55 lines
3.0 KiB
JavaScript

// Shared runtime; generated schema.js must be loaded first in classic pages.
(function (root) {
'use strict';
const schema = typeof module !== 'undefined' && module.exports
? require('./schema.js') : root.MsimeMessageSchema;
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
function matches(value, rule) {
if (rule.anyOf) return rule.anyOf.some(branch => matches(value, branch));
if (rule.type === 'array') {
if (!Array.isArray(value) || !value.every(item => matches(item, rule.items))) return false;
} else if (rule.type === 'object') {
if (!object(value)) return false;
const properties = rule.properties || {};
if ((rule.required || []).some(key => !Object.hasOwn(value, key))) return false;
for (const key of Object.keys(value)) {
if (Object.hasOwn(properties, key)) {
if (!matches(value[key], properties[key])) return false;
} else if (rule.additionalProperties === false) return false;
}
} else if (rule.type === 'integer') {
if (!Number.isSafeInteger(value)) return false;
} else if (rule.type === 'number') {
if (typeof value !== 'number' || !Number.isFinite(value)) return false;
} else if (rule.type === 'null') {
if (value !== null) return false;
} else if (rule.type && typeof value !== rule.type) return false;
if (rule.enum && !rule.enum.includes(value)) return false;
if (rule.minimum !== undefined && value < rule.minimum) return false;
if (rule.maximum !== undefined && value > rule.maximum) return false;
return true;
}
function validate(message, direction, surface) {
if (!object(message) || typeof message.type !== 'string') return false;
if (message.protocolVersion !== undefined && message.protocolVersion !== schema.version) return false;
const rules = schema[direction];
if (!rules || !Object.hasOwn(rules, message.type)) return false;
const spec = rules[message.type];
if (spec.surfaces && !spec.surfaces.includes(surface)) return false;
if (spec.data && (!Object.hasOwn(message, 'data') || !matches(message.data, spec.data))) return false;
if (!spec.data && Object.hasOwn(message, 'data')) return false;
return matches(message, { type: 'object', properties: {
type: { type: 'string' }, protocolVersion: { type: 'integer' },
...(spec.data ? {data: spec.data} : {}), ...(spec.properties || {})
}, required: spec.required || [], additionalProperties: spec.additionalProperties === true });
}
function serializeClientMessage(message, surface) {
const normalized = JSON.parse(JSON.stringify({...message, protocolVersion: schema.version}));
if (!validate(normalized, 'client', surface)) throw new TypeError('Invalid MSIME ' + surface + ' message');
return JSON.stringify(normalized);
}
const api = { validate, serializeClientMessage, version: schema.version };
root.MsimeProtocol = api;
if (typeof module !== 'undefined' && module.exports) module.exports = api;
})(globalThis);