- Event-based triggers (Phase 1): - Trigger/TriggerLog models with field_change type - TriggerService for condition evaluation and action execution - Trigger CRUD API endpoints - Task integration (status, assignee, priority changes) - Frontend: TriggerList, TriggerForm components - Weekly reports (Phase 2): - ScheduledReport/ReportHistory models - ReportService for stats generation - APScheduler for Friday 16:00 job - Report preview/generate/history API - Frontend: WeeklyReportPreview, ReportHistory components - Tests: 23 new tests (14 triggers + 9 reports) - OpenSpec: add-automation change archived 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
229 lines
7.9 KiB
Python
229 lines
7.9 KiB
Python
import uuid
|
|
from datetime import datetime, timedelta
|
|
from typing import Dict, Any, List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import func
|
|
|
|
from app.models import (
|
|
User, Task, Project, ScheduledReport, ReportHistory
|
|
)
|
|
from app.services.notification_service import NotificationService
|
|
|
|
|
|
class ReportService:
|
|
"""Service for generating and managing scheduled reports."""
|
|
|
|
@staticmethod
|
|
def get_week_start(date: Optional[datetime] = None) -> datetime:
|
|
"""Get the start of the week (Monday) for a given date."""
|
|
if date is None:
|
|
date = datetime.utcnow()
|
|
# Get Monday of the current week
|
|
days_since_monday = date.weekday()
|
|
week_start = date - timedelta(days=days_since_monday)
|
|
return week_start.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
@staticmethod
|
|
def get_weekly_stats(db: Session, user_id: str, week_start: Optional[datetime] = None) -> Dict[str, Any]:
|
|
"""
|
|
Get weekly task statistics for a user's projects.
|
|
|
|
Returns stats for all projects where the user is the owner.
|
|
"""
|
|
if week_start is None:
|
|
week_start = ReportService.get_week_start()
|
|
|
|
week_end = week_start + timedelta(days=7)
|
|
|
|
# Get projects owned by the user
|
|
projects = db.query(Project).filter(Project.owner_id == user_id).all()
|
|
|
|
if not projects:
|
|
return {
|
|
"week_start": week_start.isoformat(),
|
|
"week_end": week_end.isoformat(),
|
|
"projects": [],
|
|
"summary": {
|
|
"completed_count": 0,
|
|
"in_progress_count": 0,
|
|
"overdue_count": 0,
|
|
"total_tasks": 0,
|
|
}
|
|
}
|
|
|
|
project_ids = [p.id for p in projects]
|
|
|
|
# Get all tasks for these projects
|
|
all_tasks = db.query(Task).filter(Task.project_id.in_(project_ids)).all()
|
|
|
|
# Categorize tasks
|
|
completed_tasks = []
|
|
in_progress_tasks = []
|
|
overdue_tasks = []
|
|
|
|
now = datetime.utcnow()
|
|
|
|
for task in all_tasks:
|
|
status_name = task.status.name.lower() if task.status else ""
|
|
|
|
# Check if completed (updated this week)
|
|
if status_name in ["done", "completed", "完成"]:
|
|
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)
|
|
|
|
# Check if overdue
|
|
if task.due_date and task.due_date < now and status_name not in ["done", "completed", "完成"]:
|
|
overdue_tasks.append(task)
|
|
|
|
# Build project details
|
|
project_details = []
|
|
for project in projects:
|
|
project_tasks = [t for t in all_tasks if t.project_id == project.id]
|
|
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_details.append({
|
|
"project_id": project.id,
|
|
"project_title": project.title,
|
|
"completed_count": len(project_completed),
|
|
"in_progress_count": len(project_in_progress),
|
|
"overdue_count": len(project_overdue),
|
|
"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]],
|
|
})
|
|
|
|
return {
|
|
"week_start": week_start.isoformat(),
|
|
"week_end": week_end.isoformat(),
|
|
"generated_at": datetime.utcnow().isoformat(),
|
|
"projects": project_details,
|
|
"summary": {
|
|
"completed_count": len(completed_tasks),
|
|
"in_progress_count": len(in_progress_tasks),
|
|
"overdue_count": len(overdue_tasks),
|
|
"total_tasks": len(all_tasks),
|
|
}
|
|
}
|
|
|
|
@staticmethod
|
|
def generate_weekly_report(db: Session, user_id: str) -> Optional[ReportHistory]:
|
|
"""
|
|
Generate a weekly report for a user and save to history.
|
|
"""
|
|
# Get or create scheduled report for this user
|
|
scheduled_report = db.query(ScheduledReport).filter(
|
|
ScheduledReport.recipient_id == user_id,
|
|
ScheduledReport.report_type == "weekly",
|
|
).first()
|
|
|
|
if not scheduled_report:
|
|
scheduled_report = ScheduledReport(
|
|
id=str(uuid.uuid4()),
|
|
report_type="weekly",
|
|
recipient_id=user_id,
|
|
is_active=True,
|
|
)
|
|
db.add(scheduled_report)
|
|
db.flush()
|
|
|
|
# Generate report content
|
|
content = ReportService.get_weekly_stats(db, user_id)
|
|
|
|
# Save to history
|
|
report_history = ReportHistory(
|
|
id=str(uuid.uuid4()),
|
|
report_id=scheduled_report.id,
|
|
content=content,
|
|
status="sent",
|
|
)
|
|
db.add(report_history)
|
|
|
|
# Update last_sent_at
|
|
scheduled_report.last_sent_at = datetime.utcnow()
|
|
|
|
db.commit()
|
|
|
|
return report_history
|
|
|
|
@staticmethod
|
|
def send_report_notification(
|
|
db: Session,
|
|
user_id: str,
|
|
report_content: Dict[str, Any],
|
|
) -> None:
|
|
"""Send a notification with the weekly report summary."""
|
|
summary = report_content.get("summary", {})
|
|
completed = summary.get("completed_count", 0)
|
|
in_progress = summary.get("in_progress_count", 0)
|
|
overdue = summary.get("overdue_count", 0)
|
|
|
|
message = f"本週完成 {completed} 項任務,進行中 {in_progress} 項"
|
|
if overdue > 0:
|
|
message += f",逾期 {overdue} 項需關注"
|
|
|
|
NotificationService.create_notification(
|
|
db=db,
|
|
user_id=user_id,
|
|
notification_type="status_change",
|
|
reference_type="report",
|
|
reference_id="weekly",
|
|
title="週報:專案進度彙整",
|
|
message=message,
|
|
)
|
|
|
|
@staticmethod
|
|
async def generate_all_weekly_reports(db: Session) -> List[str]:
|
|
"""
|
|
Generate weekly reports for all active subscriptions.
|
|
Called by the scheduler on Friday 16:00.
|
|
"""
|
|
generated_for = []
|
|
|
|
# Get all active scheduled reports
|
|
active_reports = db.query(ScheduledReport).filter(
|
|
ScheduledReport.is_active == True,
|
|
ScheduledReport.report_type == "weekly",
|
|
).all()
|
|
|
|
for scheduled_report in active_reports:
|
|
try:
|
|
# Generate report
|
|
content = ReportService.get_weekly_stats(db, scheduled_report.recipient_id)
|
|
|
|
# Save history
|
|
history = ReportHistory(
|
|
id=str(uuid.uuid4()),
|
|
report_id=scheduled_report.id,
|
|
content=content,
|
|
status="sent",
|
|
)
|
|
db.add(history)
|
|
|
|
# Update last_sent_at
|
|
scheduled_report.last_sent_at = datetime.utcnow()
|
|
|
|
# Send notification
|
|
ReportService.send_report_notification(db, scheduled_report.recipient_id, content)
|
|
|
|
generated_for.append(scheduled_report.recipient_id)
|
|
|
|
except Exception as e:
|
|
# Log failure
|
|
history = ReportHistory(
|
|
id=str(uuid.uuid4()),
|
|
report_id=scheduled_report.id,
|
|
content={},
|
|
status="failed",
|
|
error_message=str(e),
|
|
)
|
|
db.add(history)
|
|
|
|
db.commit()
|
|
|
|
return generated_for
|