fix(performance-conversion): count missing days as zero in intent averages

This commit is contained in:
江林铖 2026-10-23 10:08:19 +08:00
parent 8b982ff3b8
commit 112f25aba7
5 changed files with 106 additions and 6 deletions

View File

@ -35,7 +35,7 @@ web
- “按时交付”只统计对应主管/规则审核节点在本月完成、审批状态为已完结且审批结论未拒绝的美术设计与内容视频流程;待计算流程不进入分母。详情页可在全部流程、美术设计流程和内容视频流程之间筛选,公式、数量与明细必须共用当前筛选口径。
- “首轮通过”读取作品过稿数据,分子为 V1 版本审核通过的作品数,分母为当前已通过的作品数;详情页列出部门范围内所有含 V1 的作品,搜索只过滤明细,不改变顶部完整口径。
- “价值指标”直接读取素材资产库的可见素材,按资产类型展示下载过的资产数、该类型总数与复用率,并按下载次数从高到低列出该类型资产明细。
- “业绩转化”按亿家达、卓禾、欧意朗、顾家、索菲亚、朴美六个品牌展示 2026 年 4 月 1 日以后发起且已批准完结的美术设计流程;商品经营关联键优先取流程表单“商品ID”,兼容分支字段“现商品ID”,空值和非数字占位内容不参与关联。每条“流程 × 商品ID”独立保留审批编号、流程实例 ID、完结时间和六个月监控窗口,并按需读取该商品流程完结后的每日点击率、支付转化率、收藏、加购、展现、访客、推广 ROI 与支付金额;缺失日保持缺失,不补成零。
- “业绩转化”按亿家达、卓禾、欧意朗、顾家、索菲亚、朴美六个品牌展示 2026 年 4 月 1 日以后发起且已批准完结的美术设计流程;商品经营关联键优先取流程表单“商品ID”,兼容分支字段“现商品ID”,空值和非数字占位内容不参与关联。每条“流程 × 商品ID”独立保留审批编号、流程实例 ID、完结时间和六个月监控窗口,并按需读取该商品流程完结后的每日点击率、支付转化率、收藏、加购、展现、访客、推广 ROI 与支付金额;每日明细和趋势中的缺失日保持缺失,不改写源数据。仅在完结后 7 日、15 日意向率平均值中,将缺失或无法计算的日意向率按 0 计入,并固定除以 7 或 15;未满对应自然天数仍显示“——”,7 日平均值为 0 时不计算环比。
- “品牌知识库”参照素材资产库的浏览工作面建立搜索、知识类型切换、排序与内容网格结构;真实知识数据和上传能力未接入前必须显示明确空状态,不虚构知识条目或数量。
- “团队负荷”统计品牌中心在职人员当前进行中的流程负荷,排除指定管理人员和商品企划部;该部门及其人员不进入流程归属、部门明细、人员分母或平均负荷。
- “团队产能”仅展示品牌创意A部、品牌创意B部、综合支持部、品牌设计部、空间视觉部五个二级部门,并按该顺序展示近 8 个上海自然周的当前在职人数、收到流程数和完结流程数;人数统一读取当前钉钉组织架构,不还原历史人员快照。每个部门再按美术设计流程、内容视频流程分别展示近 8 周趋势与明细;流程按当前任务负责人部门归属映射,但美术设计流程不计“品牌策划主管分配任务/品牌策划主管审核”,内容视频流程不计“内容主管分配任务/内容主管审核”等管理节点,点击周次查看进行中与已完结明细。

View File

@ -1,6 +1,7 @@
import type { ReactNode } from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import PerformanceConversionDetail from './PerformanceConversionDetail'
import type { PerformanceConversionDailyMetric } from './performance-conversion'
jest.mock('./DepartmentShell', () => ({
__esModule: true,
@ -170,4 +171,33 @@ describe('PerformanceConversionDetail', () => {
expect(screen.getByText('27.67%')).toBeInTheDocument()
expect(screen.getByText('20.29%')).toBeInTheDocument()
})
it('shows zero-filled averages while keeping missing daily data unchanged', async () => {
const daily: PerformanceConversionDailyMetric[] = Array.from({ length: 15 }, (_, index) => ({
date: `2026-10-${String(index + 7).padStart(2, '0')}`,
impressions: null,
visitors: [3, 10, 11].includes(index) ? null : 100,
clickRate: null,
favorites: [3, 10, 11].includes(index) ? null : 10,
cartUsers: [3, 10, 11].includes(index) ? null : 10,
paymentConversionRate: [3, 10, 11].includes(index) ? null : 0.03,
paymentAmount: null,
roi: null,
}))
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({ success: true, data: { ...detailData, daily } }),
} as Response)
render(<PerformanceConversionDetail workflowId="flow-1" productId="1041432226288" returnHref="/workspaces/business/performance-conversion" />)
const summary = await screen.findByRole('region', { name: '流程完结后 7日 / 15日平均值' })
expect(within(summary).getByText('19.71%')).toBeInTheDocument()
expect(within(summary).getByText('18.40%')).toBeInTheDocument()
expect(within(summary).getByText('-6.67%')).toBeInTheDocument()
expect(within(summary).getByText(/日意向率按 0 计入/)).toBeInTheDocument()
const missingDayRow = screen.getByRole('row', { name: /2026-10-10/ })
expect(missingDayRow).toHaveTextContent('—')
expect(daily[3].visitors).toBeNull()
})
})

View File

@ -149,7 +149,7 @@ function IntentRateSummary({ daily }: { daily: PerformanceConversionDailyMetric[
<section className="border-t border-slate-200 bg-white" aria-labelledby="intent-rate-summary-title">
<header className="px-5 py-4 sm:px-7">
<h2 id="intent-rate-summary-title" className="font-bold text-slate-950">流程完结后 7日 / 15日平均值</h2>
<p className="mt-1 text-xs text-slate-500">从流程完结日开始计算,窗口内每天数据完整时显示,缺少天数或字段显示“——”</p>
<p className="mt-1 text-xs leading-5 text-slate-500">从流程完结日开始计算,缺失或无法计算的日意向率按 0 计入,分别除以 7 或 15;未满对应天数显示“——”</p>
</header>
<dl className="grid gap-px border-y border-slate-200 bg-slate-200 sm:grid-cols-3">
<div className="bg-white px-5 py-4 sm:px-7">

View File

@ -7,6 +7,7 @@ import {
extractValidProductIds,
isValidDateKey,
toShanghaiDateKey,
type PerformanceConversionDailyMetric,
} from './performance-conversion'
describe('performance conversion helpers', () => {
@ -63,6 +64,75 @@ describe('performance conversion helpers', () => {
expect(summary.intentRateChange).toBeCloseTo(0.20289855072463767, 12)
})
function dailyMetrics(length: number): PerformanceConversionDailyMetric[] {
return Array.from({ length }, (_, index) => ({
date: `2026-10-${String(index + 7).padStart(2, '0')}`,
impressions: null,
visitors: 100,
clickRate: null,
favorites: 10,
cartUsers: 10,
paymentConversionRate: 0.03,
paymentAmount: null,
roi: null,
}))
}
it('counts missing dates as zero and divides by the full calendar window', () => {
const daily = dailyMetrics(15)
// The October 7 completion has gaps on October 10, 17 and 18.
for (const index of [3, 10, 11]) {
daily[index] = { ...daily[index], favorites: null, cartUsers: null, visitors: null, paymentConversionRate: null }
}
const summary = buildIntentRateWindowSummary(daily)
const sevenDayAverage = 0.23 * 6 / 7
const fifteenDayAverage = 0.23 * 12 / 15
expect(summary.sevenDayIntentRate).toBeCloseTo(sevenDayAverage, 12)
expect(summary.fifteenDayIntentRate).toBeCloseTo(fifteenDayAverage, 12)
expect(summary.intentRateChange).toBeCloseTo((fifteenDayAverage - sevenDayAverage) / sevenDayAverage, 12)
expect(daily[3].visitors).toBeNull()
expect(daily[10].favorites).toBeNull()
})
it('counts an unavailable daily rate as zero when fields are missing or visitors are zero', () => {
const daily = dailyMetrics(7)
daily[2].favorites = null
daily[4].visitors = 0
expect(buildIntentRateWindowSummary(daily)).toEqual({
sevenDayIntentRate: expect.closeTo(0.23 * 5 / 7, 12),
fifteenDayIntentRate: null,
intentRateChange: null,
})
})
it('keeps unavailable windows until 7 or 15 calendar days have elapsed', () => {
expect(buildIntentRateWindowSummary(dailyMetrics(6)).sevenDayIntentRate).toBeNull()
for (const length of [7, 14]) {
const summary = buildIntentRateWindowSummary(dailyMetrics(length))
expect(summary.sevenDayIntentRate).toBeCloseTo(0.23, 12)
expect(summary.fifteenDayIntentRate).toBeNull()
expect(summary.intentRateChange).toBeNull()
}
expect(buildIntentRateWindowSummary([])).toEqual({
sevenDayIntentRate: null,
fifteenDayIntentRate: null,
intentRateChange: null,
})
})
it('returns zero averages but no change when all elapsed dates are missing', () => {
const daily = dailyMetrics(15).map((day) => ({
...day, favorites: null, cartUsers: null, visitors: null, paymentConversionRate: null,
}))
expect(buildIntentRateWindowSummary(daily)).toEqual({
sevenDayIntentRate: 0,
fifteenDayIntentRate: 0,
intentRateChange: null,
})
})
it('returns unavailable values when a window is incomplete or its 7-day rate is zero', () => {
const fiveDays = Array.from({ length: 5 }, (_, index) => ({
date: `2026-07-${String(index + 1).padStart(2, '0')}`,

View File

@ -106,9 +106,9 @@ export function calculateIntentRate(day: Pick<PerformanceConversionDailyMetric,
function averageIntentRate(daily: PerformanceConversionDailyMetric[], windowSize: number) {
const window = daily.slice(0, windowSize)
if (window.length !== windowSize) return null
const values = window.map(calculateIntentRate)
if (values.some((value) => value === null)) return null
return values.reduce<number>((total, value) => total + (value ?? 0), 0) / windowSize
// The API includes every elapsed calendar day, with null metrics for gaps.
// Count unavailable daily rates as zero without modifying the source data.
return window.reduce((total, day) => total + (calculateIntentRate(day) ?? 0), 0) / windowSize
}
export function buildIntentRateWindowSummary(daily: PerformanceConversionDailyMetric[]): PerformanceConversionWindowSummary {