import type { QARecord, QAQuery } from './qaOps' import { getPayload } from './qaOps' import { parseQualitySummary } from './qaQuality' export type QACountPoint = { label: string; count: number } const day = 24 * 60 * 60 * 1000, offset = 8 * 60 * 60 * 1000 export function qaDailySeries(items: QARecord[], query: QAQuery): QACountPoint[] { const start = Date.parse(query.start), end = Date.parse(query.end) if (!Number.isFinite(start) || !Number.isFinite(end) || start >= end || end - start > 90 * day) throw Error('统计时间范围无效') const counts = new Map() for (let t = Math.floor((start + offset) / day) * day; t <= Math.floor((end - 1 + offset) / day) * day; t += day) counts.set(new Date(t).toISOString().slice(0, 10), 0) for (const item of items) { const stamp = Date.parse(item.asked_at) if (stamp >= start && stamp < end) { const key = new Date(stamp + offset).toISOString().slice(0, 10) counts.set(key, (counts.get(key) || 0) + 1) } } return [...counts].map(([label, count]) => ({ label, count })) } export function qaCategorySeries(items: QARecord[], key: 'product_category' | 'problem_type'): QACountPoint[] { const counts = new Map() for (const item of items) { const label = (item.quality?.state === 'stale' ? '' : item.quality?.effective[key] || item[key] || '').trim() || '未分类' counts.set(label, (counts.get(label) || 0) + 1) } return [...counts] .map(([label, count]) => ({ label, count })) .sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN')) } export async function fetchQADashboardQuality(query: QAQuery, signal?: AbortSignal) { const value = parseQualitySummary( await getPayload('/api/v1/qa-ops/quality/query', signal, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Governance-Action': 'qa-quality' }, body: JSON.stringify({ start: query.start, end: query.end, keyword: query.keyword }), }) ) if ( Date.parse(value.start) !== Date.parse(query.start) || Date.parse(value.end) !== Date.parse(query.end) || value.keyword !== query.keyword ) throw Error('质量统计与当前查询范围不一致') return value }