- 하늘 상황 판정·테스트를 구름 지점 2곳(20·100km), 모델 2개(JMA·ICON) 기준으로 - Open-Meteo 보조값 호출(fetch_weather)도 장애 대처(8초 끊기·2분 건너뛰기) 적용 - worker: 행사 수집 스케줄과 Open-Meteo 보조값 스케줄 둘 다 유지 - 지수 seed: main 의 한계 설명 + main-2 의 점수 구간 설명 - 옛 data-pipeline 문서는 삭제 유지, 내용은 openmeteo-data.md·데이터_흐름.html 에 반영
49 lines
2.4 KiB
Python
49 lines
2.4 KiB
Python
from datetime import date, datetime
|
|
|
|
from sqlalchemy import delete, select
|
|
from sqlalchemy.dialects.postgresql import insert
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from common.database.model.models import CloudHourly, Region, SkyScore, SunsetSpot
|
|
|
|
_CHUNK = 2000
|
|
|
|
|
|
class SkyCRUD:
|
|
async def upsert_clouds(self, s: AsyncSession, rows: list[dict]) -> None:
|
|
for i in range(0, len(rows), _CHUNK):
|
|
stmt = insert(CloudHourly).values(rows[i:i + _CHUNK])
|
|
await s.execute(stmt.on_conflict_do_update(
|
|
index_elements=["region_code", "kind", "distance_km", "forecast_at", "model"],
|
|
set_={k: stmt.excluded[k] for k in ("cloud_low", "cloud_mid", "cloud_high")},
|
|
))
|
|
|
|
async def clouds_at(self, s: AsyncSession, region_code: str, at: datetime) -> list[CloudHourly]:
|
|
stmt = select(CloudHourly).where(CloudHourly.region_code == region_code, CloudHourly.forecast_at == at)
|
|
return list((await s.scalars(stmt)).all())
|
|
|
|
async def upsert_scores(self, s: AsyncSession, rows: list[dict]) -> None:
|
|
if not rows:
|
|
return
|
|
stmt = insert(SkyScore).values(rows)
|
|
await s.execute(stmt.on_conflict_do_update(
|
|
index_elements=["region_code", "score_date", "sky_type"],
|
|
set_={k: stmt.excluded[k] for k in ("score", "grade", "confidence", "best_times", "computed_at", "situation", "models_total", "models_agree")},
|
|
))
|
|
|
|
async def scores(self, s: AsyncSession, region_codes: list[str], since: date) -> list[SkyScore]:
|
|
stmt = select(SkyScore).where(SkyScore.region_code.in_(region_codes), SkyScore.score_date >= since)
|
|
return list((await s.scalars(stmt.order_by(SkyScore.score_date))).all())
|
|
|
|
async def spots_in_box(self, s: AsyncSession, lat: float, lon: float, dlat: float, dlon: float) -> list[SunsetSpot]:
|
|
stmt = select(SunsetSpot).where(SunsetSpot.lat.between(lat - dlat, lat + dlat), SunsetSpot.lon.between(lon - dlon, lon + dlon))
|
|
return list((await s.scalars(stmt)).all())
|
|
|
|
async def replace_spots(self, s: AsyncSession, rows: list[dict]) -> None:
|
|
await s.execute(delete(SunsetSpot))
|
|
if rows:
|
|
await s.execute(insert(SunsetSpot).values(rows))
|
|
|
|
async def ranking_regions(self, s: AsyncSession) -> list[Region]:
|
|
return list((await s.scalars(select(Region).where(Region.is_ranking_spot.is_(True)))).all())
|