feat: enhance weekly report and realtime notifications

Weekly Report (fix-weekly-report):
- Remove 5-task limit, show all tasks per category
- Add blocked tasks with blocker_reason and blocked_since
- Add next week tasks (due in coming week)
- Add assignee_name, completed_at, days_overdue to task details
- Frontend collapsible sections for each task category
- 8 new tests for enhanced report content

Realtime Notifications (fix-realtime-notifications):
- SQLAlchemy event-based notification publishing
- Redis Pub/Sub for multi-process broadcast
- Fix soft rollback handler stacking issue
- Fix ping scheduling drift (send immediately when interval expires)
- Frontend NotificationContext with WebSocket reconnection

Spec Fixes:
- Add missing ## Purpose sections to 5 specs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beabigegg
2025-12-30 20:52:08 +08:00
parent 10db2c9d1f
commit 64874d5425
25 changed files with 1034 additions and 140 deletions

View File

@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
from sqlalchemy import func
from app.models import (
User, Task, Project, ScheduledReport, ReportHistory
User, Task, Project, ScheduledReport, ReportHistory, Blocker
)
from app.services.notification_service import NotificationService
@@ -29,11 +29,15 @@ class ReportService:
Get weekly task statistics for a user's projects.
Returns stats for all projects where the user is the owner.
Includes: completed, in_progress, overdue, blocked, and next_week tasks.
"""
if week_start is None:
week_start = ReportService.get_week_start()
week_end = week_start + timedelta(days=7)
next_week_start = week_end
next_week_end = next_week_start + timedelta(days=7)
now = datetime.utcnow()
# Get projects owned by the user
projects = db.query(Project).filter(Project.owner_id == user_id).all()
@@ -47,36 +51,71 @@ class ReportService:
"completed_count": 0,
"in_progress_count": 0,
"overdue_count": 0,
"blocked_count": 0,
"next_week_count": 0,
"total_tasks": 0,
}
}
project_ids = [p.id for p in projects]
# Get all tasks for these projects
# Get all tasks for these projects with assignee info
all_tasks = db.query(Task).filter(Task.project_id.in_(project_ids)).all()
# Get active blockers (unresolved) for these projects
active_blockers = db.query(Blocker).join(Task).filter(
Task.project_id.in_(project_ids),
Blocker.resolved_at.is_(None)
).all()
# Map task_id to blocker info
blocker_map: Dict[str, Blocker] = {b.task_id: b for b in active_blockers}
blocked_task_ids = set(blocker_map.keys())
# Categorize tasks
completed_tasks = []
in_progress_tasks = []
overdue_tasks = []
now = datetime.utcnow()
blocked_tasks = []
next_week_tasks = []
for task in all_tasks:
status_name = task.status.name.lower() if task.status else ""
is_done = status_name in ["done", "completed", "完成"]
# Check if completed (updated this week)
if status_name in ["done", "completed", "完成"]:
if is_done:
if task.updated_at and task.updated_at >= week_start:
completed_tasks.append(task)
# Check if in progress
elif status_name in ["in progress", "進行中", "doing"]:
in_progress_tasks.append(task)
else:
# Check if in progress
if status_name in ["in progress", "進行中", "doing"]:
in_progress_tasks.append(task)
# Check if overdue
if task.due_date and task.due_date < now and status_name not in ["done", "completed", "完成"]:
overdue_tasks.append(task)
# Check if overdue
if task.due_date and task.due_date < now:
overdue_tasks.append(task)
# Check if blocked
if task.id in blocked_task_ids:
blocked_tasks.append(task)
# Check if due next week
if task.due_date and next_week_start <= task.due_date < next_week_end:
next_week_tasks.append(task)
# Helper to get assignee name
def get_assignee_name(task: Task) -> Optional[str]:
if task.assignee:
return task.assignee.name
return None
# Helper to calculate days overdue
def get_days_overdue(task: Task) -> int:
if task.due_date:
delta = now - task.due_date
return max(0, delta.days)
return 0
# Build project details
project_details = []
@@ -85,6 +124,8 @@ class ReportService:
project_completed = [t for t in completed_tasks if t.project_id == project.id]
project_in_progress = [t for t in in_progress_tasks if t.project_id == project.id]
project_overdue = [t for t in overdue_tasks if t.project_id == project.id]
project_blocked = [t for t in blocked_tasks if t.project_id == project.id]
project_next_week = [t for t in next_week_tasks if t.project_id == project.id]
project_details.append({
"project_id": project.id,
@@ -92,9 +133,57 @@ class ReportService:
"completed_count": len(project_completed),
"in_progress_count": len(project_in_progress),
"overdue_count": len(project_overdue),
"blocked_count": len(project_blocked),
"next_week_count": len(project_next_week),
"total_tasks": len(project_tasks),
"completed_tasks": [{"id": t.id, "title": t.title} for t in project_completed[:5]],
"overdue_tasks": [{"id": t.id, "title": t.title, "due_date": t.due_date.isoformat() if t.due_date else None} for t in project_overdue[:5]],
# Full task lists with detailed fields
"completed_tasks": [
{
"id": t.id,
"title": t.title,
"completed_at": t.updated_at.isoformat() if t.updated_at else None,
"assignee_name": get_assignee_name(t),
}
for t in project_completed
],
"in_progress_tasks": [
{
"id": t.id,
"title": t.title,
"assignee_name": get_assignee_name(t),
"due_date": t.due_date.isoformat() if t.due_date else None,
}
for t in project_in_progress
],
"overdue_tasks": [
{
"id": t.id,
"title": t.title,
"due_date": t.due_date.isoformat() if t.due_date else None,
"days_overdue": get_days_overdue(t),
"assignee_name": get_assignee_name(t),
}
for t in project_overdue
],
"blocked_tasks": [
{
"id": t.id,
"title": t.title,
"blocker_reason": blocker_map[t.id].reason if t.id in blocker_map else None,
"blocked_since": blocker_map[t.id].created_at.isoformat() if t.id in blocker_map else None,
"assignee_name": get_assignee_name(t),
}
for t in project_blocked
],
"next_week_tasks": [
{
"id": t.id,
"title": t.title,
"due_date": t.due_date.isoformat() if t.due_date else None,
"assignee_name": get_assignee_name(t),
}
for t in project_next_week
],
})
return {
@@ -106,6 +195,8 @@ class ReportService:
"completed_count": len(completed_tasks),
"in_progress_count": len(in_progress_tasks),
"overdue_count": len(overdue_tasks),
"blocked_count": len(blocked_tasks),
"next_week_count": len(next_week_tasks),
"total_tasks": len(all_tasks),
}
}
@@ -161,10 +252,18 @@ class ReportService:
completed = summary.get("completed_count", 0)
in_progress = summary.get("in_progress_count", 0)
overdue = summary.get("overdue_count", 0)
blocked = summary.get("blocked_count", 0)
next_week = summary.get("next_week_count", 0)
message = f"本週完成 {completed} 項任務,進行中 {in_progress}"
if overdue > 0:
message += f",逾期 {overdue}需關注"
message += f",逾期 {overdue}"
if blocked > 0:
message += f",阻礙 {blocked}"
if overdue > 0 or blocked > 0:
message += " 需關注"
if next_week > 0:
message += f"。下週預計 {next_week}"
NotificationService.create_notification(
db=db,