from dataclasses import dataclass from datetime import datetime from typing import Literal, Optional from common.enums import AirGrade from common.utils.kma_time import KST from common.utils.text import hour_label from services.air_service import GRADE_LABEL Tone = Literal["normal", "warn", "rain"] UV_HIGH = 6.0 TEMP_SWING_C = 10.0 WIND_CHILL_C = 3.0 MUGGY = 75 SHOWN = 2 @dataclass(frozen=True) class HeroStatItem: key: str icon: str label: str value: str tone: Tone = "normal" @dataclass(frozen=True) class HeroInput: now: datetime night: bool rain_at: Optional[datetime] = None air: Optional[AirGrade] = None uv: Optional[float] = None temp_min: Optional[float] = None temp_max: Optional[float] = None temp: Optional[float] = None feels: Optional[float] = None discomfort: Optional[int] = None moon: Optional[str] = None def uv_label(uv: float) -> str: return "낮음" if uv < 3 else "보통" if uv < 6 else "높음" if uv < 8 else "매우 높음" if uv < 11 else "위험" def _discomfort_label(v: int) -> str: return "보통" if v < 75 else "높음" if v < 80 else "매우 높음" def _rain_value(now: datetime, at: datetime) -> str: return "지금 와요" if at <= now else f"{hour_label(at.astimezone(KST).hour)}쯤 와요" def hero_stats(x: HeroInput) -> list[HeroStatItem]: """히어로 두 칸. 지금 챙겨야 할 것(경고)을 먼저, 모자라면 기본값으로 채운다.""" alerts: list[HeroStatItem] = [] if x.rain_at is not None: alerts.append(HeroStatItem("rain", "umbrella", "지금 비", _rain_value(x.now, x.rain_at), "rain")) if x.air in (AirGrade.BAD, AirGrade.VERY_BAD): alerts.append(HeroStatItem("air", "mask", "미세먼지", GRADE_LABEL[x.air], "warn")) if not x.night and x.uv is not None and x.uv >= UV_HIGH: alerts.append(HeroStatItem("uv", "glasses", "자외선", f"{round(x.uv)} · {uv_label(x.uv)}", "warn")) if x.temp_min is not None and x.temp_max is not None and x.temp_max - x.temp_min >= TEMP_SWING_C: alerts.append(HeroStatItem("swing", "coldwind", f"일교차 {round(x.temp_max - x.temp_min)}°", "아침엔 겉옷")) if x.temp is not None and x.feels is not None and x.temp - x.feels >= WIND_CHILL_C: alerts.append(HeroStatItem("chill", "coldwind", f"체감 {round(x.feels)}°", "바람 쌀쌀")) if x.discomfort is not None and x.discomfort >= MUGGY: alerts.append(HeroStatItem("muggy", "muggy", "불쾌지수", f"{x.discomfort} · {_discomfort_label(x.discomfort)}", "warn")) # 비는 올 때만 알린다 (안 온다는 칸은 두지 않음) fills: list[HeroStatItem] = [] if x.air is not None: fills.append(HeroStatItem("air", "vent", "미세먼지", GRADE_LABEL[x.air])) if not x.night and x.uv is not None: fills.append(HeroStatItem("uv", "glasses", "자외선", f"{round(x.uv)} · {uv_label(x.uv)}")) if x.night and x.moon: fills.append(HeroStatItem("moon", "moon", "오늘 밤 달", x.moon)) picked: list[HeroStatItem] = [] for item in alerts + fills: if item.key not in {p.key for p in picked} and not (item.icon == "coldwind" and any(p.icon == "coldwind" for p in picked)): picked.append(item) if len(picked) == SHOWN: break return picked