구름 - 노을·일출 점수를 구 중심 20·100km 두 지점으로 계산 (50km 제외), 모델은 JMA·ICON 두 개만 사용 - cloud_hourly를 (구 코드, 방향, 거리, 시각, 모델) 키로 바꾸고 점수에 쓰는 시각만 저장 보조값 - openmeteo_hourly 신규: 자외선·가시거리·돌풍·기압·지표면 온도(끝 시각 값), 일조(3시간 합), 일사(평균·최대) - worker가 03·09·15·21시 10분에 구 256곳을 요청 11번으로 일괄 수집, 7일 지난 행 정리 - 새 응답에서 값이 비어 오면 이전 값을 유지(COALESCE) 지수 - 새 축 uv·sun·view 추가 (index_definition/index_score 칼럼, 비중 있는 지수에만 상세에 표시) - 빨래·멍뭉산책·피크닉·등산·러닝·가드닝·별보기·사진산책에 비중 배분, 자전거·캠핑·멍뭉산책에 돌풍·지표면 온도 0점 조건 - 자외선·가시거리 등 순간값은 가장 가까운 3시간 구간, 일조는 덮는 구간을 계산 창에 연결 - 보조값이 새로 오면 지수를 다시 계산하도록 재계산 기준에 fetched_at 추가 테스트 102개 통과 (PGSSLMODE=disable, JWT는 테스트용 환경변수).
57 lines
2.9 KiB
Python
57 lines
2.9 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Path, Query
|
|
|
|
from common.enums import ErrorType, IndexCategory
|
|
from common.models.errors import error_info
|
|
from router.v1.index.protocol import Factor, IndexSummary, Res_IndexDetail, Res_IndexList, TrendPoint
|
|
from services.calc.index_calc import AXES, BASE_AXES, zero_rules_text
|
|
from services.index_service import IndexItem, IndexService
|
|
|
|
router = APIRouter(prefix="/v1/index", tags=["index"])
|
|
|
|
_AXIS_LABEL = {"temp": "기온", "rain": "비", "wind": "바람", "humidity": "습도", "air": "공기", "uv": "자외선", "sun": "햇빛", "view": "가시거리", "time": "시간대"}
|
|
|
|
|
|
def _summary(i: IndexItem) -> IndexSummary:
|
|
d = i.definition
|
|
return IndexSummary(
|
|
index_id=d.index_id, code=d.code, name=d.name, icon=d.icon, category=IndexCategory(d.category), is_fun=d.is_fun,
|
|
is_core=d.is_core, score=i.score.score, rank=i.rank, summary=i.score.summary,
|
|
trend=[TrendPoint(day=t.score_date, score=t.score) for t in i.trend],
|
|
)
|
|
|
|
|
|
@router.get("/list", response_model=Res_IndexList, response_model_exclude_none=True, summary="지수 목록 (점수순, 1~3위, 오늘 날씨 점수)")
|
|
async def list_indices(
|
|
region: str = Query(..., description="구 행정코드"),
|
|
category: Optional[IndexCategory] = Query(None),
|
|
core: bool = Query(False, description="이번 계절 핵심 지수만"),
|
|
service: IndexService = Depends(),
|
|
) -> Res_IndexList:
|
|
err, board = await service.board(region)
|
|
if err is not ErrorType.SUCCESS:
|
|
return Res_IndexList(result=error_info(err))
|
|
items = [_summary(i) for i in board.items]
|
|
filtered = [x for x in items if (category is None or x.category is category) and (not core or x.is_core)]
|
|
return Res_IndexList(region_code=board.region.code, day=board.day, weather_score=board.weather_score, top3=items[:3], items=filtered)
|
|
|
|
|
|
@router.get("/{index_id}", response_model=Res_IndexDetail, response_model_exclude_none=True, summary="지수 계산 근거")
|
|
async def get_index(index_id: int = Path(...), region: str = Query(..., description="구 행정코드"), service: IndexService = Depends()) -> Res_IndexDetail:
|
|
err, board = await service.board(region)
|
|
if err is not ErrorType.SUCCESS:
|
|
return Res_IndexDetail(result=error_info(err))
|
|
item = next((i for i in board.items if i.definition.index_id == index_id), None)
|
|
if item is None:
|
|
return Res_IndexDetail(result=error_info(ErrorType.NOT_FOUND))
|
|
d, s = item.definition, item.score
|
|
return Res_IndexDetail(
|
|
index=_summary(item), description=d.description,
|
|
factors=[
|
|
Factor(axis=a, label=_AXIS_LABEL[a], value=getattr(s, f"{a}_score"), weight=getattr(d, f"weight_{a}"), note=s.notes.get(a, ""))
|
|
for a in AXES if a in BASE_AXES or getattr(d, f"weight_{a}") > 0
|
|
],
|
|
zero_rules=zero_rules_text(d.zero_rules), zero_reason=s.zero_reason, limit=d.limit_description,
|
|
)
|