Files
ai-kefu-admin/admin/src/utils/statsRange.ts
T

42 lines
1.6 KiB
TypeScript
Raw Normal View History

/**
* 统计时间快捷范围(Phase 3 Task 4 仪表盘;Task 8 统计分析复用)
*
* 口径事实源:app/platform/routes/stats_routes.py 的 from/to 解析
* (data_routes._parse_dt)——前端只传本地日期串 YYYY-MM-DD:
* - 日期-only 的 to 后端视为含当日全天(23:59:59.999999,边界含);
* - 边界是 naive UTC(与库内 created_at 同一对比空间),前端不做时区换算;
* - "近 N 天" = 含今天往回共 N 天(今天 / 近7天 / 近30天)。
*/
export type StatsQuickRange = 'today' | 'last7' | 'last30'
/** 快捷范围清单(顺序即切换器展示序) */
export const STATS_QUICK_RANGES: Array<{
value: StatsQuickRange
label: string
/** 窗口天数(含今天) */
days: number
}> = [
{ value: 'today', label: '今天', days: 1 },
{ value: 'last7', label: '近7天', days: 7 },
{ value: 'last30', label: '近30天', days: 30 }
]
/** Date → 本地日期串 YYYY-MM-DD(月/日两位补零) */
function toDateKey(date: Date): string {
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${date.getFullYear()}-${month}-${day}`
}
/** 快捷范围 → {from, to} 查询参数(本地日期;now 可注入便于测试) */
export function rangeToParams(
range: StatsQuickRange,
now: Date = new Date()
): { from: string; to: string } {
const days = STATS_QUICK_RANGES.find((item) => item.value === range)?.days ?? 1
return {
from: toDateKey(new Date(now.getFullYear(), now.getMonth(), now.getDate() - (days - 1))),
to: toDateKey(now)
}
}