feat(api): add trend analysis and extended periods to dashboard summary

- Add `include_trends` query parameter to enable trend calculation
- Implement trend comparison logic (delta % and direction) against previous periods
- Add support for `last7d`, `last30d`, and `all` time period options
- Update `DashboardSummaryResponse` to include optional `trends` field
- Add helper functions for custom time window aggregation
- Add unit tests for trend calculation and period window logic
- Update feature documentation with new parameters and response schemas
This commit is contained in:
zenfun
2026-01-02 22:30:38 +08:00
parent 5b2b176a55
commit 08a8a1e42f
3 changed files with 498 additions and 9 deletions

View File

@@ -72,15 +72,39 @@
| 参数 | 类型 | 说明 |
|------|------|------|
| `period` | string | 预设周期: `today`, `week`, `month` |
| `period` | string | 预设周期: `today`, `week`, `month`, `last7d`, `last30d`, `all` |
| `since` | int | 自定义起始时间 (Unix 秒) |
| `until` | int | 自定义结束时间 (Unix 秒) |
| `include_trends` | bool | 是否包含趋势数据 (与前一周期对比) |
> **已知限制** (待 fix-dashboard-summary-api 实现后解决):
> - `period=week` 使用自然周起点 (周一),非滚动 7 天
> - `period=month` 使用自然月起点 (1号),非滚动 30 天
> - 趋势数据 (delta/trend) 当前不返回,前端需隐藏趋势指示器
> - 建议使用 `since`/`until` 实现精确的滚动时间窗口
**周期类型说明**:
| 周期值 | 时间窗口 | 趋势对比周期 |
|--------|----------|--------------|
| `today` | 今日 00:00 UTC → 当前 | 昨日 |
| `week` | 本周一 00:00 UTC → 当前 | 上一自然周 |
| `month` | 本月 1 日 00:00 UTC → 当前 | 上一自然月 |
| `last7d` | 当前 - 7 天 → 当前 | 14 天前 → 7 天前 |
| `last30d` | 当前 - 30 天 → 当前 | 60 天前 → 30 天前 |
| `all` | 无时间过滤 | 不支持趋势 |
**趋势数据** (当 `include_trends=true` 时返回):
```json
{
"trends": {
"requests": { "delta": 12.5, "direction": "up" },
"tokens": { "delta": -1.1, "direction": "down" },
"error_rate": { "delta": 0.0, "direction": "stable" },
"latency": { "delta": -5.2, "direction": "down" }
}
}
```
| 字段 | 说明 |
|------|------|
| `delta` | 与前一周期相比的百分比变化 (四舍五入到 1 位小数) |
| `direction` | `up` (增长 >0.5%), `down` (下降 <-0.5%), `stable`, `new` (无基准数据) |
### 3.4 告警摘要