fix(qa-dashboard): 已结束月份不再拉伸到当前,区域统计跟随所选区间
build / build (push) Failing after 24s

This commit is contained in:
Zhero & 助理
2026-12-03 22:45:34 +08:00
committed by 姚章浩
parent 6826f37bbd
commit d7a3aed80a
5 changed files with 61 additions and 6 deletions
+8 -1
View File
@@ -249,6 +249,13 @@ func (s *Store) readDashboard(ctx context.Context, q dobotaiops.RecordQuery, mas
out.Regions = append(out.Regions, r)
}
inPeriod := 0
regionFrom := out.MonthStart
if q.Granularity != "" {
// 区域统计跟随所选区间:按天/周/月/自定义看板里,"这个区间用了多少"才是
// 用户要的数字。老周看板(granularity 为空)保持原来的自然月累计不变,
// 周报生成链路逐字段不变。分母仍是月度通讯录人数,由前端注明快照月份。
regionFrom = q.Window.Start
}
for _, r := range records {
if q.Granularity == "" {
if !r.at.Before(out.WeekStart) {
@@ -269,7 +276,7 @@ func (s *Store) readDashboard(ctx context.Context, q dobotaiops.RecordQuery, mas
products[dashboardCategory(v.Effective.ProductCategory)]++
types[dashboardCategory(v.Effective.ProblemType)]++
}
if !r.at.Before(out.MonthStart) {
if !r.at.Before(regionFrom) {
person := attribution.Resolve(r.identity.StaffID, r.identity.UnionID, r.fields["asker"], r.fields["trigger_person"])
if index, ok := regionIndex[person.Region]; ok && person.PersonID != "" {
out.Regions[index].Questions++
+21 -5
View File
@@ -467,11 +467,13 @@ function RegionTable({
regions,
frequency,
month,
caption,
}: {
box: Box
regions: DashboardRegion[]
frequency: boolean
month: string
caption: string
}) {
const rows = [...regions].sort((a, b) => {
const av = frequency ? a.frequency : a.usage_coverage.percent,
@@ -504,7 +506,7 @@ function RegionTable({
y += h
return (
<g key={r.id}>
<title>{`${r.name};${month}月内提问 ${r.questions} 次;去重使用 ${r.users ?? '未知'} 人 / 通讯录归属 ${r.roster_people ?? '未知'} 人`}</title>
<title>{`${r.name};${caption};提问 ${r.questions} 次;去重使用 ${r.users ?? '未知'} 人 / ${month} 月通讯录归属 ${r.roster_people ?? '未知'} 人`}</title>
{i % 2 === 0 ? <rect x={11} y={at - 15} width={box.w - 22} height={h} fill="#f7faff" /> : null}
{value !== null && i < 3 ? (
<circle cx={30} cy={at - 4} r={9} fill={['#e9bd54', '#a8b6c9', '#dc9c73'][i]} />
@@ -520,7 +522,7 @@ function RegionTable({
)
})}
<text x={box.w - 17} y={box.h - 17} textAnchor="end" fontSize={9.5} fill={muted}>
{month}月累计
{caption}
</text>
</Panel>
)
@@ -545,7 +547,7 @@ export function QADashboardDrawing({
week = currentQAWeek(new Date(data.week_start)),
filterLines = wrap(data.keyword ? `关键词:${data.keyword}` : '', inner - 16, 11),
heading = data.period
? `${data.period.label} · 全部指标按所选范围统计 · 区域统计 ${new Date(Date.parse(data.month_start) + 8 * 3600000).toISOString().slice(0, 7)} 月累计`
? `${data.period.label} · 全部指标按所选范围统计 · 区域统计按所选区间(分母为 ${new Date(Date.parse(data.month_start) + 8 * 3600000).toISOString().slice(0, 7)} 月通讯录)`
: `${data.week_start.slice(0, 10)} 起的自然周${data.quality_period ? ` · 质量 ${data.quality_period.month} 月内累计` : ''} · 区域统计 ${new Date(Date.parse(data.month_start) + 8 * 3600000).toISOString().slice(0, 7)} 月累计`,
headingLines = (mobile ? [heading.split(' · ')[0], heading.split(' · ').slice(1).join(' · ')] : [heading]).flatMap(
(row) => wrap(row, inner - 16, 10.5)
@@ -606,6 +608,8 @@ export function QADashboardDrawing({
height = mobile ? cursor + 540 + gap + pad : cursor + 270 + pad
const frozen = (correctness: boolean): PlotPoint[] => historicalTrend(data, correctness)
const month = new Date(Date.parse(data.month_start) + 8 * 3600000).toISOString().slice(0, 7)
// 有 granularity 时区域面板跟所选区间走,分母仍是月度通讯录;老周看板保持"月累计"。
const regionCaption = data.period ? `所选区间提问 / 去重使用 ÷ ${month} 月通讯录` : `${month}月累计`
const tipWidth = Math.min(334, inner),
tipLines = wrap(tip ? definitions[tip.id] : '', tipWidth - 26, 11.5),
tipHeight = tipLines.length * 15.525 + 26,
@@ -700,8 +704,20 @@ export function QADashboardDrawing({
id="issue-trend"
points={data.daily.map((p) => ({ label: p.date.slice(5), value: p.count, hint: `${p.date}:${p.count} 条` }))}
/>
<RegionTable box={freq} regions={data.regions} frequency month={month} />
<RegionTable box={usage} regions={data.regions} frequency={false} month={month} />
<RegionTable
box={freq}
regions={data.regions}
frequency
month={month}
caption={regionCaption}
/>
<RegionTable
box={usage}
regions={data.regions}
frequency={false}
month={month}
caption={regionCaption}
/>
<Plot
box={coverage}
title="覆盖率趋势"
+10
View File
@@ -545,6 +545,16 @@ export function parseQADashboard(value: unknown): QADashboardData {
export function liveDashboardQuery(query: QAQuery, now: Date, granularity?: string): QAQuery {
// A custom or single-day range is an explicit slice: never stretch it to "now".
if (granularity === 'custom' || granularity === 'day') return query
const beijingMonth = (at: number) => new Date(at + 8 * 3_600_000).toISOString().slice(0, 7)
// A chosen month is a closed accounting period: only the month that contains
// "now" may advance with the clock. Stretching a finished month to now pulled
// the region panels (natural-month cumulative) into the NEXT month, so an
// 11 月 selection showed 12 月 usage.
if (granularity === 'month') {
if (beijingMonth(Date.parse(query.end) - 1) !== beijingMonth(now.getTime())) return query
if (Date.parse(query.end) >= now.getTime()) return query
return { ...query, end: now.toISOString() }
}
const last = currentQAWeek(new Date(Date.parse(query.end) - 1)),
current = currentQAWeek(now)
if (
+13
View File
@@ -23,3 +23,16 @@ it('never stretches an explicit custom range to now', () => {
const custom = { ...query, start: '2026-10-01T00:00:00+08:00', end: '2026-10-03T00:00:00+08:00' }
expect(liveDashboardQuery(custom, new Date('2026-10-20T09:00:00+08:00'), 'custom')).toEqual(custom)
})
it('never stretches a finished month into the next month', () => {
const closedMonth = { ...query, start: '2026-11-01T00:00:00+08:00', end: '2026-12-01T00:00:00+08:00' }
const now = new Date('2026-12-03T22:45:00+08:00')
expect(liveDashboardQuery(closedMonth, now, 'month')).toEqual(closedMonth)
})
it('keeps the current month live up to now', () => {
const openMonth = { ...query, start: '2026-12-01T00:00:00+08:00', end: '2026-12-03T22:00:00+08:00' }
const next = liveDashboardQuery(openMonth, new Date('2026-12-03T22:45:00+08:00'), 'month')
expect(next.end).toBe('2026-12-03T14:45:00.000Z')
expect(next.start).toBe(openMonth.start)
})
+9
View File
@@ -57,6 +57,15 @@ describe('monthly dashboard range', () => {
expect(html).toContain('66.7%')
})
it('cuts the region panels from the selected range and names the roster month', () => {
const data = parseQADashboard(monthFixture)
const html = renderToStaticMarkup(<QADashboardView data={data} loading={false} error="" onRetry={vi.fn()} />)
expect(html).toContain('区域统计按所选区间')
expect(html).toContain('分母为 2026-11 月通讯录')
expect(html).toContain('所选区间提问 / 去重使用 ÷ 2026-11 月通讯录')
expect(html).not.toContain('2026-11月累计')
})
it('rejects a payload that claims a month granularity without a matching period', () => {
const { period: _period, ...rest } = monthFixture
expect(() => parseQADashboard(rest)).toThrow()