Files
o2o-weather/backend/services/hero_stats.py
T
minaChoi 4096ce0441 feat: 히어로 두 칸을 상황별로 고르고 아이콘·색을 상태에 맞춤
- 비 예보 → 미세먼지 나쁨 → 자외선 높음(낮) → 일교차 10° → 체감 쌀쌀 → 불쾌지수 75 순서, 모자라면 비 없음·미세먼지·자외선·달
- 조심(노랑)·비(파랑) 강조, 미세먼지 나쁨은 마스크 아이콘(Fluent Emoji)
- Hero.stats 추가, 자외선은 openmeteo_hourly 지금 시각 값
2026-10-07 16:25:01 +09:00

89 lines
3.4 KiB
Python

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
rain_known: bool = False
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.rain_known and x.rain_at is None:
fills.append(HeroStatItem("rain", "moon" if x.night else "sun", "2시간 동안 비", "안 와요"))
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