feat: 노을 탭 3일 하늘 타일 — 히어로 문장·타일 색·피드 카드를 점수 등급 기준으로(잿빛이면 거의 안 보인다고), 타일 누르면 그날로, 다른 날 안내도 등급 기준
This commit is contained in:
1 parent
118a7b29ff
commit
52b79c6de9
9 files changed
+152
-90
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import math
|
||||
import statistics
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
|
||||
@@ -21,7 +21,7 @@ from crud.region_crud import RegionCRUD
|
||||
from crud.sky_crud import SkyCRUD
|
||||
from services.calc.sky_score import (
|
||||
DISTANCES_KM, GRADE_LABEL, RAIN_BEFORE_HOURS, SITUATION_VERDICT, VERDICT_LINE, VERDICT_RANK, Air, Layers, air_factor, best_times,
|
||||
air_note, confidence, grade, mean_layers, plain_line, reason, sky_title, topic, score, situation, sunrise_timeline, sunset_timeline, verdict,
|
||||
GRADE_RANK, air_note, confidence, grade, mean_layers, plain_line, reason, sky_title, score, situation, sunrise_timeline, sunset_timeline, verdict,
|
||||
)
|
||||
from services.calc.sun import SunTimes, sun_times
|
||||
from services.external.open_meteo import OpenMeteoRequestFailed, fetch_clouds, parse_clouds
|
||||
@@ -72,6 +72,7 @@ class SkyView:
|
||||
days: list[SkyScore]
|
||||
spots: list[Spot]
|
||||
plan: Optional[SkyPlan] = None
|
||||
plans: list[SkyPlan] = field(default_factory=list) # plan 과 같은 종류(노을·일출)로 아직 안 끝난 날 최대 3일. 날짜 타일용
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -115,16 +116,14 @@ def peak_of(x: SkyScore) -> Optional[datetime]:
|
||||
|
||||
|
||||
def outlook(target: SkyScore, days: list[SkyScore], today: date) -> str:
|
||||
"""뒤의 날 중 결론이 더 좋은 날이 있을 때만 한 줄 (내일은 하늘이 온통 붉게 물들어요 (18:12)). 없으면 빈 문자열."""
|
||||
later = [x for x in days if x.score_date > target.score_date and VERDICT_RANK[day_verdict(x)] > VERDICT_RANK[day_verdict(target)]]
|
||||
"""뒤의 날 중 점수 등급이 더 높은 날이 있을 때만 그날 문장 (18:07, 모레 노을이 은은하게 물들어요). 없으면 빈 문자열."""
|
||||
rank = lambda x: GRADE_RANK[SkyGrade(x.grade)] # noqa: E731
|
||||
later = [x for x in days if x.score_date > target.score_date and x.grade is not None and rank(x) > rank(target)]
|
||||
if not later:
|
||||
return ""
|
||||
best = max(later, key=lambda x: (VERDICT_RANK[day_verdict(x)], x.score))
|
||||
sit = SkySituation(best.situation) if best.situation else None
|
||||
word = "일출" if best.sky_type == SkyType.SUNRISE.value else "노을"
|
||||
line = "하늘이 온통 붉게 물들어요" if sit is SkySituation.GLOW and air_note(best.air) is None else f"{word}이 보여요"
|
||||
peak = peak_of(best)
|
||||
return f"{topic(_word(best.score_date, today))} {line}" + (f" ({peak:%H:%M})" if peak else "")
|
||||
best = max(later, key=lambda x: (rank(x), x.score))
|
||||
kind = SkyType.SUNRISE if best.sky_type == SkyType.SUNRISE.value else SkyType.SUNSET
|
||||
return sky_title(SkyGrade(best.grade), kind, _word(best.score_date, today), peak_of(best))
|
||||
|
||||
|
||||
def build_plan(
|
||||
@@ -138,11 +137,11 @@ def build_plan(
|
||||
peak_at = datetime.fromisoformat(peak["at"]) if peak else None
|
||||
return SkyPlan(
|
||||
kind=kind, target=target,
|
||||
title=sky_title(SkySituation(target.situation) if target.situation else None, kind, when, peak_at) or f"{when} {VERDICT_LINE[v]}",
|
||||
title=sky_title(SkyGrade(target.grade), kind, when, peak_at),
|
||||
day=target.score_date, when=when, verdict=v, headline=VERDICT_LINE[v],
|
||||
reason=". ".join(filter(None, [reason(*layers, kind) if layers else _FALLBACK_REASON[v], air_note(target.air)])),
|
||||
score=target.score, grade_label=GRADE_LABEL[SkyGrade(target.grade)],
|
||||
situation_label=plain_line(SkySituation(target.situation) if target.situation else None, target.air),
|
||||
situation_label=plain_line(SkyGrade(target.grade), SkySituation(target.situation) if target.situation else None, target.air, kind),
|
||||
models_total=target.models_total, models_agree=target.models_agree,
|
||||
best_at=peak_at, spot=spots[0] if spots and v != "rest" and not rise else None,
|
||||
timeline=(
|
||||
@@ -169,6 +168,23 @@ def next_event(sunsets: list[SkyScore], sunrises: list[SkyScore], lat: float, lo
|
||||
return kind, target, st
|
||||
|
||||
|
||||
def day_plans(
|
||||
plan: Optional[SkyPlan], sunsets: list[SkyScore], sunrises: list[SkyScore], spots: list[Spot], lat: float, lon: float, now: datetime,
|
||||
) -> list[SkyPlan]:
|
||||
"""plan 과 같은 종류로 아직 끝나지 않은 날들의 결론. plan 의 날은 plan 그대로."""
|
||||
if plan is None:
|
||||
return []
|
||||
rise = plan.kind is SkyType.SUNRISE
|
||||
same = sunrises if rise else sunsets
|
||||
out = []
|
||||
for x in same:
|
||||
st = sun_times(x.score_date, lat, lon)
|
||||
if (st.sunrise_at if rise else st.dusk_end_at) <= now:
|
||||
continue
|
||||
out.append(plan if x.score_date == plan.day else build_plan(x, st, None, spots, same, now.date(), plan.kind))
|
||||
return out[:DAYS]
|
||||
|
||||
|
||||
def _slot(now: datetime) -> str:
|
||||
"""노을은 13시, 일출은 21시 이후 예보로 다시 계산한다."""
|
||||
return "am" if now.hour < 13 else "pm" if now.hour < 21 else "night"
|
||||
@@ -288,6 +304,7 @@ class SkyService:
|
||||
sunrises = [x for x in scores if x.sky_type == SkyType.SUNRISE.value][:DAYS]
|
||||
plan = await self._plan(region, sunsets, sunrises, near, now)
|
||||
return ErrorType.SUCCESS, SkyView(
|
||||
plans=day_plans(plan, sunsets, sunrises, near, lat, lon, now),
|
||||
region=region,
|
||||
sunset_azimuth=first.sunset_azimuth,
|
||||
sunset=next((x for x in sunsets if x.score_date == now.date()), None),
|
||||
|
||||
Reference in new issue
Block a user