- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가 - 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시) - 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리 - 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리 - pytest 66개 (테스트 전용 DB·Redis 사용) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
import statistics
|
|
from dataclasses import dataclass
|
|
from datetime import datetime, timedelta
|
|
|
|
from common.enums import Confidence, SkyGrade, SkyType
|
|
|
|
DISTANCES_KM = (20, 50, 100)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Layers:
|
|
low: float
|
|
mid: float
|
|
high: float
|
|
|
|
|
|
def score(near20: Layers, near50: Layers, far100: Layers) -> int:
|
|
"""머리 위(20·50km)는 중·상층 구름이 알맞게 있어야 물들고, 먼 쪽(100km)이 막히면 햇빛이 못 들어와 전체가 깎인다."""
|
|
canvas = (max(near20.mid, near20.high) + max(near50.mid, near50.high)) / 2
|
|
f_canvas = 0.3 if canvas < 10 else max(0.0, 1 - abs(canvas - 50) / 50)
|
|
f_near = 1 - 0.8 * (near20.low + near50.low) / 200
|
|
light = 1 - min(far100.low + 0.5 * far100.mid, 100) / 100
|
|
return round(100 * (0.75 * f_canvas + 0.25 * f_near) * (0.25 + 0.75 * light))
|
|
|
|
|
|
def grade(value: int) -> SkyGrade:
|
|
if value >= 80:
|
|
return SkyGrade.RED
|
|
if value >= 60:
|
|
return SkyGrade.GLOW
|
|
if value >= 40:
|
|
return SkyGrade.SOFT
|
|
return SkyGrade.GRAY
|
|
|
|
|
|
def confidence(model_scores: list[int]) -> Confidence:
|
|
"""모델끼리 점수가 비슷할수록 확신이 높다."""
|
|
if len(model_scores) < 2:
|
|
return Confidence.LOW
|
|
spread = statistics.pstdev(model_scores)
|
|
return Confidence.HIGH if spread < 8 else Confidence.NORMAL if spread < 15 else Confidence.LOW
|
|
|
|
|
|
def best_times(kind: SkyType, sun_at: datetime, twilight_at: datetime, high_cloud: float) -> list[dict]:
|
|
"""노을은 해 진 뒤 10~20분이 가장 붉다. 높은 구름이 많을수록 늦게까지 물든다."""
|
|
peak_min = 10 + round(min(high_cloud, 100) / 10)
|
|
sun_at, twilight_at = sun_at.replace(second=0, microsecond=0), twilight_at.replace(second=0, microsecond=0)
|
|
if kind is SkyType.SUNSET:
|
|
return [
|
|
{"at": sun_at.isoformat(), "kind": "sun", "label": "해 져요"},
|
|
{"at": (sun_at + timedelta(minutes=peak_min)).isoformat(), "kind": "peak", "label": "가장 붉을 때"},
|
|
{"at": twilight_at.isoformat(), "kind": "twilight", "label": "보랏빛 하늘"},
|
|
]
|
|
return [
|
|
{"at": twilight_at.isoformat(), "kind": "twilight", "label": "푸른 새벽"},
|
|
{"at": (sun_at - timedelta(minutes=peak_min)).isoformat(), "kind": "peak", "label": "가장 붉을 때"},
|
|
{"at": sun_at.isoformat(), "kind": "sun", "label": "해 떠요"},
|
|
]
|
|
|
|
|
|
GRADE_LABEL = {SkyGrade.RED: "붉음", SkyGrade.GLOW: "고움", SkyGrade.SOFT: "은은함", SkyGrade.GRAY: "흐림"}
|