Files
ai-platform/src/modules/welltrust-qap/lib/replay.ts
T
2026-10-18 15:27:02 +08:00

117 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 回放投影的公共口径。
*
* 中央六个页签的源表是**当期快照**(2026-10-16):DS-09 的样本、DS-14 的负荷、
* DS-15 的告警池都只有这一刻。时间轴向前回放时,不能把这些数字原样复制到过去,
* 也不能另造一套互相矛盾的数。这里用一条确定性的「质量成熟度」曲线做投影:
*
* - 游标 11(2026-10)恒等于 1,投影后的读数与源表逐位一致;
* - 越往前,缺陷率越高、良率越低、告警积压越多、检验缺口越大;
* - 负荷另走季节项(年末新品集中放行),当期最高;
* - 同 (period) 永远得到同一结果,演示与复盘都可复现。
*
* 投影只改「回放期的读数」,不改源表明细;明细表仍是 2026-10 当期,
* 用于口径对账。页签上的标记会写明「回放推算 · 源表 2026-10 反推」。
*/
import { CURRENT_PERIOD, periodDate, periodLabel, periodParts, PERIOD_COUNT } from './time';
export interface ReplayFrame {
period: number;
/** 是否停在源表当期 */
isCurrent: boolean;
/** 质量成熟度:0 = 窗口起点,1 = 源表当期 */
maturity: number;
/** 缺陷率乘数(≥1,越早越高) */
defect: number;
/** 良率「距 100% 的差值」乘数(≥1,越早良率越低) */
yieldHeadroom: number;
/** 告警积压乘数(≥1,越早越多) */
alert: number;
/** 检验标准缺口乘数(≥1,越早缺口越大) */
gap: number;
/** 工序负荷乘数(季节项,当期=1,越早越低) */
load: number;
/** 参数漂移乘数(≤1,越早偏移越小) */
drift: number;
}
/** 两频叠加的成熟度曲线:平滑抬升 + 轻微起伏,锚点固定在当期 */
const curve = (p: number) =>
0.58 + 0.42 * Math.pow(p / (PERIOD_COUNT - 1), 1.08) + 0.028 * Math.sin(p * 0.95 + 0.6);
const CURVE_START = curve(0);
const CURVE_CURRENT = curve(CURRENT_PERIOD);
export function replayFrame(period: number): ReplayFrame {
const p = Math.max(0, Math.min(PERIOD_COUNT - 1, period));
const maturity = +(curve(p) / CURVE_CURRENT).toFixed(4);
const early = 1 - maturity;
const isCurrent = p === CURRENT_PERIOD;
/* 负荷季节项:年末新品集中放行,当期最高;再除以当期值保证 p=11 时正好为 1 */
const season = (1 + 0.1 * (p - 5.5) / 5.5) / 1.1;
return {
period: p,
isCurrent,
maturity,
defect: isCurrent ? 1 : +(1 + 0.62 * early).toFixed(4),
yieldHeadroom: isCurrent ? 1 : +(1 + 0.55 * early).toFixed(4),
alert: isCurrent ? 1 : +(1 + 1.15 * early).toFixed(4),
gap: isCurrent ? 1 : +(1 + 0.9 * early).toFixed(4),
load: isCurrent ? 1 : +season.toFixed(4),
drift: isCurrent ? 1 : +(0.72 + 0.28 * maturity).toFixed(4),
};
}
/** 良率类读数投影:只压缩/放大距 100% 的差值,避免把 88.9% 直接乘成负数 */
export function projectYield(value: number, frame: ReplayFrame): number {
if (frame.isCurrent) return value;
return +(100 - (100 - value) * frame.yieldHeadroom).toFixed(1);
}
/** 不良率类读数投影 */
export function projectDefect(value: number, frame: ReplayFrame): number {
if (frame.isCurrent) return value;
return +(value * frame.defect).toFixed(1);
}
/** 时间轴背景柱:由同一成熟度曲线合成,不再借用经营指标 OV。
* 55 → 97:窗口起点红档、中段黄档、当期绿档,和六页签的读数方向一致。 */
export function replayHealth(period: number): number {
const frame = replayFrame(period);
if (frame.isCurrent) return 97;
const norm = (frame.maturity - CURVE_START / CURVE_CURRENT) / (1 - CURVE_START / CURVE_CURRENT);
return Math.round(55 + 42 * norm + 1.5 * Math.sin(period * 0.8));
}
/** 页签标记文案 */
export const replayTag = (period: number): string =>
period === CURRENT_PERIOD ? '源表现算快照' : `回放推算 · ${periodLabel(period)}`;
/** 回放期的数据日(用于告警池基准日等文案) */
export const replayBaseDate = (period: number): string => periodDate(period);
const pad = (n: number) => String(n).padStart(2, '0');
const daysInMonth = (y: number, m: number) => new Date(y, m, 0).getDate();
/**
* 把源表日期(YYYY-MM-DD 或 MM-DD)平移到回放期。
* 当期原样返回;历史期按 (period - 11) 个月整体平移,日号越界时压到月末。
* 月份用 UTC 计算,避免本地时区把 1 号挤到上个月。
*/
export function shiftMonthDate(date: string, period: number): string {
if (period === CURRENT_PERIOD) return date;
const offset = period - CURRENT_PERIOD;
const full = date.match(/^(\d{4})-(\d{2})-(\d{2})$/);
const short = date.match(/^(\d{2})-(\d{2})$/);
if (!full && !short) return date;
const baseYear = full ? Number(full[1]) : periodParts(CURRENT_PERIOD).year;
const baseMonth = Number(full ? full[2] : short![1]);
const baseDay = Number(full ? full[3] : short![2]);
const idx = baseYear * 12 + (baseMonth - 1) + offset;
const year = Math.floor(idx / 12);
const month = (idx % 12) + 1;
const day = Math.min(baseDay, daysInMonth(year, month));
return full ? `${year}-${pad(month)}-${pad(day)}` : `${pad(month)}-${pad(day)}`;
}