Aggregate current and recent drift signals for the weekly report.
(profile: Dict[str, Any], drift_updates: List[Dict[str, Any]])
| 405 | |
| 406 | def _build_drift_summary(profile: Dict[str, Any], drift_updates: List[Dict[str, Any]]) -> Dict[str, Any]: |
| 407 | """Aggregate current and recent drift signals for the weekly report.""" |
| 408 | drift_state = (profile or {}).get("drift_state", {}) or {} |
| 409 | status = drift_state.get("status", "stable") |
| 410 | max_score = float(drift_state.get("score", 0.0) or 0.0) |
| 411 | explanation = drift_state.get("explanation", "") |
| 412 | top_shift_topics = list(drift_state.get("top_shift_topics", []) or []) |
| 413 | detected_at = drift_state.get("detected_at") |
| 414 | |
| 415 | for update in drift_updates: |
| 416 | metadata = update.get("metadata", {}) or {} |
| 417 | max_score = max(max_score, float(metadata.get("drift_score", update.get("drift_score", 0.0)) or 0.0)) |
| 418 | if not explanation and metadata.get("explanation"): |
| 419 | explanation = metadata.get("explanation") |
| 420 | if not top_shift_topics and metadata.get("top_shift_topics"): |
| 421 | top_shift_topics = list(metadata.get("top_shift_topics") or []) |
| 422 | if not detected_at and metadata.get("drift_status") == "shifting": |
| 423 | detected_at = update.get("timestamp") |
| 424 | |
| 425 | return { |
| 426 | "status": status, |
| 427 | "status_label": { |
| 428 | "stable": "稳定", |
| 429 | "shifting": "迁移中", |
| 430 | "recovered": "已恢复", |
| 431 | }.get(status, "稳定"), |
| 432 | "max_score": round(max_score, 4), |
| 433 | "detected_at": detected_at, |
| 434 | "top_shift_topics": top_shift_topics[:3], |
| 435 | "explanation": explanation or "近期兴趣稳定,系统继续以长期画像为主。", |
| 436 | } |
| 437 | |
| 438 | |
| 439 | def generate_weekly_report(user_id: str, days: int = 7) -> Dict[str, Any]: |
| 440 | """Generate the weekly report, including drift-awareness summary.""" |
no test coverage detected