|
| 1 | +from uuid import UUID |
| 2 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 3 | +from sqlalchemy import select, extract, func |
| 4 | +from sqlalchemy.orm import selectinload |
| 5 | +from app.models.category import Todo, Category |
| 6 | +from app.schemas.chart import DailyAchievementResponse |
| 7 | +from collections import defaultdict |
| 8 | +from typing import List |
| 9 | + |
| 10 | + |
| 11 | +async def get_daily_achievement( |
| 12 | + db: AsyncSession, user_id: UUID, year: int |
| 13 | +) -> List[DailyAchievementResponse]: |
| 14 | + # 해당 연도의 투두 불러오기 |
| 15 | + result = await db.execute( |
| 16 | + select(Todo) |
| 17 | + .join(Category) |
| 18 | + .options(selectinload(Todo.category)) |
| 19 | + .where(Todo.user_id == user_id, extract("year", Todo.start_date) == year) |
| 20 | + ) |
| 21 | + todos = result.scalars().all() |
| 22 | + |
| 23 | + # 일자별 → 카테고리별 → [완료된 수, 전체 수] 누적 |
| 24 | + data = defaultdict( |
| 25 | + lambda: {"english": [0, 0], "exercise": [0, 0], "coding": [0, 0]} |
| 26 | + ) |
| 27 | + |
| 28 | + for todo in todos: |
| 29 | + key_date = todo.start_date |
| 30 | + category = todo.category.category_name.value # "영어", "운동", "코딩" |
| 31 | + |
| 32 | + if category == "영어": |
| 33 | + key = "english" |
| 34 | + elif category == "운동": |
| 35 | + key = "exercise" |
| 36 | + elif category == "코딩": |
| 37 | + key = "coding" |
| 38 | + else: |
| 39 | + continue # 혹시 모를 예외 |
| 40 | + |
| 41 | + data[key_date][key][1] += 1 # 전체 수 |
| 42 | + if todo.is_completed: |
| 43 | + data[key_date][key][0] += 1 # 완료 수 |
| 44 | + |
| 45 | + # 응답 데이터 구성 |
| 46 | + responses: List[DailyAchievementResponse] = [] |
| 47 | + |
| 48 | + for date_key in sorted(data.keys()): |
| 49 | + entry = data[date_key] |
| 50 | + responses.append( |
| 51 | + DailyAchievementResponse( |
| 52 | + date=date_key, |
| 53 | + english=( |
| 54 | + round((entry["english"][0] / entry["english"][1] * 100), 2) |
| 55 | + if entry["english"][1] |
| 56 | + else 0 |
| 57 | + ), |
| 58 | + exercise=( |
| 59 | + round((entry["exercise"][0] / entry["exercise"][1] * 100), 2) |
| 60 | + if entry["exercise"][1] |
| 61 | + else 0 |
| 62 | + ), |
| 63 | + coding=( |
| 64 | + round((entry["coding"][0] / entry["coding"][1] * 100), 2) |
| 65 | + if entry["coding"][1] |
| 66 | + else 0 |
| 67 | + ), |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + return responses |
| 72 | + |
| 73 | + |
| 74 | +from app.models.category import Todo, Category |
| 75 | +from app.schemas.chart import MonthlyAchievementResponse |
| 76 | +from sqlalchemy import extract, select |
| 77 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 78 | +from sqlalchemy.orm import selectinload |
| 79 | +from uuid import UUID |
| 80 | +from collections import defaultdict |
| 81 | +from typing import List |
| 82 | + |
| 83 | + |
| 84 | +async def get_monthly_achievement( |
| 85 | + db: AsyncSession, user_id: UUID, year: int |
| 86 | +) -> List[MonthlyAchievementResponse]: |
| 87 | + # 1. 연도에 해당하는 Todo 불러오기 (Category 조인 포함) |
| 88 | + result = await db.execute( |
| 89 | + select(Todo) |
| 90 | + .join(Category) |
| 91 | + .options(selectinload(Todo.category)) |
| 92 | + .where(Todo.user_id == user_id, extract("year", Todo.start_date) == year) |
| 93 | + ) |
| 94 | + todos = result.scalars().all() |
| 95 | + |
| 96 | + # 2. 월별 데이터 누적용 딕셔너리 초기화 |
| 97 | + data = defaultdict( |
| 98 | + lambda: { |
| 99 | + "english": [0, 0], |
| 100 | + "exercise": [0, 0], |
| 101 | + "coding": [0, 0], |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + # 3. 데이터 누적 |
| 106 | + for todo in todos: |
| 107 | + month_str = todo.start_date.strftime("%Y-%m") # 예: "2025-01" |
| 108 | + category = todo.category.category_name.value # "코딩", "영어", "운동" |
| 109 | + |
| 110 | + if category == "영어": |
| 111 | + key = "english" |
| 112 | + elif category == "운동": |
| 113 | + key = "exercise" |
| 114 | + elif category == "코딩": |
| 115 | + key = "coding" |
| 116 | + else: |
| 117 | + continue |
| 118 | + |
| 119 | + data[month_str][key][1] += 1 # 전체 수 증가 |
| 120 | + if todo.is_completed: |
| 121 | + data[month_str][key][0] += 1 # 완료 수 증가 |
| 122 | + |
| 123 | + # 4. 응답 스키마에 맞게 가공 |
| 124 | + responses: List[MonthlyAchievementResponse] = [] |
| 125 | + for month_key in sorted(data.keys()): |
| 126 | + entry = data[month_key] |
| 127 | + responses.append( |
| 128 | + MonthlyAchievementResponse( |
| 129 | + month=month_key, |
| 130 | + english=( |
| 131 | + round((entry["english"][0] / entry["english"][1] * 100), 2) |
| 132 | + if entry["english"][1] |
| 133 | + else 0 |
| 134 | + ), |
| 135 | + exercise=( |
| 136 | + round((entry["exercise"][0] / entry["exercise"][1] * 100), 2) |
| 137 | + if entry["exercise"][1] |
| 138 | + else 0 |
| 139 | + ), |
| 140 | + coding=( |
| 141 | + round((entry["coding"][0] / entry["coding"][1] * 100), 2) |
| 142 | + if entry["coding"][1] |
| 143 | + else 0 |
| 144 | + ), |
| 145 | + ) |
| 146 | + ) |
| 147 | + |
| 148 | + return responses |
0 commit comments