- 서브클러스터별 독립 폴리곤/센터/center trail 렌더링 - 반경 밖 이탈 선박 강제 감쇠 (OUT_OF_RANGE) - Backend correlation API에 sub_cluster_id 추가 - 모델 패널 5개 항상 표시, 드롭다운 기본값 70% - DISPLAY_STALE_SEC (time_bucket 기반) 폴리곤 노출 필터 - AIS 수집 bbox 122~132E/31~39N 확장 - historyActive 시 deck.gl 이중 렌더링 수정 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
190 lines
5.6 KiB
Python
190 lines
5.6 KiB
Python
import logging
|
|
from contextlib import contextmanager
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
import pandas as pd
|
|
import psycopg2
|
|
from psycopg2 import pool
|
|
|
|
from config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_pool: Optional[pool.ThreadedConnectionPool] = None
|
|
|
|
|
|
def init_pool():
|
|
global _pool
|
|
_pool = pool.ThreadedConnectionPool(
|
|
minconn=1,
|
|
maxconn=3,
|
|
host=settings.SNPDB_HOST,
|
|
port=settings.SNPDB_PORT,
|
|
dbname=settings.SNPDB_NAME,
|
|
user=settings.SNPDB_USER,
|
|
password=settings.SNPDB_PASSWORD,
|
|
)
|
|
logger.info('snpdb connection pool initialized')
|
|
|
|
|
|
def close_pool():
|
|
global _pool
|
|
if _pool:
|
|
_pool.closeall()
|
|
_pool = None
|
|
logger.info('snpdb connection pool closed')
|
|
|
|
|
|
@contextmanager
|
|
def get_conn():
|
|
conn = _pool.getconn()
|
|
try:
|
|
yield conn
|
|
finally:
|
|
_pool.putconn(conn)
|
|
|
|
|
|
def check_health() -> bool:
|
|
try:
|
|
with get_conn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute('SELECT 1')
|
|
return True
|
|
except Exception as e:
|
|
logger.error('snpdb health check failed: %s', e)
|
|
return False
|
|
|
|
|
|
def fetch_all_tracks(hours: int = 24) -> pd.DataFrame:
|
|
"""한국 해역 전 선박의 궤적 포인트를 조회한다.
|
|
|
|
LineStringM 지오메트리에서 개별 포인트를 추출하며,
|
|
한국 해역(122-132E, 31-39N) 내 최근 N시간 데이터를 반환한다.
|
|
"""
|
|
query = f"""
|
|
SELECT
|
|
t.mmsi,
|
|
to_timestamp(ST_M((dp).geom)) as timestamp,
|
|
t.time_bucket,
|
|
ST_Y((dp).geom) as lat,
|
|
ST_X((dp).geom) as lon,
|
|
CASE
|
|
WHEN (dp).path[1] = 1 THEN (t.start_position->>'sog')::float
|
|
ELSE COALESCE((t.end_position->>'sog')::float, t.avg_speed::float)
|
|
END as raw_sog
|
|
FROM signal.t_vessel_tracks_5min t,
|
|
LATERAL ST_DumpPoints(t.track_geom) dp
|
|
WHERE t.time_bucket >= NOW() - INTERVAL '{hours} hours'
|
|
AND t.track_geom && ST_MakeEnvelope(122, 31, 132, 39, 4326)
|
|
ORDER BY t.mmsi, to_timestamp(ST_M((dp).geom))
|
|
"""
|
|
|
|
try:
|
|
with get_conn() as conn:
|
|
df = pd.read_sql_query(query, conn)
|
|
logger.info(
|
|
'fetch_all_tracks: %d rows, %d vessels (last %dh)',
|
|
len(df),
|
|
df['mmsi'].nunique() if len(df) > 0 else 0,
|
|
hours,
|
|
)
|
|
return df
|
|
except Exception as e:
|
|
logger.error('fetch_all_tracks failed: %s', e)
|
|
return pd.DataFrame(columns=['mmsi', 'timestamp', 'lat', 'lon', 'raw_sog'])
|
|
|
|
|
|
def fetch_incremental(last_bucket: datetime) -> pd.DataFrame:
|
|
"""last_bucket 이후의 신규 궤적 포인트를 조회한다.
|
|
|
|
스케줄러 증분 업데이트에 사용되며, time_bucket > last_bucket 조건으로
|
|
이미 처리한 버킷을 건너뛴다.
|
|
"""
|
|
query = """
|
|
SELECT
|
|
t.mmsi,
|
|
to_timestamp(ST_M((dp).geom)) as timestamp,
|
|
t.time_bucket,
|
|
ST_Y((dp).geom) as lat,
|
|
ST_X((dp).geom) as lon,
|
|
CASE
|
|
WHEN (dp).path[1] = 1 THEN (t.start_position->>'sog')::float
|
|
ELSE COALESCE((t.end_position->>'sog')::float, t.avg_speed::float)
|
|
END as raw_sog
|
|
FROM signal.t_vessel_tracks_5min t,
|
|
LATERAL ST_DumpPoints(t.track_geom) dp
|
|
WHERE t.time_bucket > %s
|
|
AND t.track_geom && ST_MakeEnvelope(122, 31, 132, 39, 4326)
|
|
ORDER BY t.mmsi, to_timestamp(ST_M((dp).geom))
|
|
"""
|
|
|
|
try:
|
|
with get_conn() as conn:
|
|
df = pd.read_sql_query(query, conn, params=(last_bucket,))
|
|
logger.info(
|
|
'fetch_incremental: %d rows, %d vessels (since %s)',
|
|
len(df),
|
|
df['mmsi'].nunique() if len(df) > 0 else 0,
|
|
last_bucket.isoformat(),
|
|
)
|
|
return df
|
|
except Exception as e:
|
|
logger.error('fetch_incremental failed: %s', e)
|
|
return pd.DataFrame(columns=['mmsi', 'timestamp', 'lat', 'lon', 'raw_sog'])
|
|
|
|
|
|
def fetch_static_info(mmsi_list: list[str]) -> dict[str, dict]:
|
|
"""MMSI 목록에 해당하는 선박 정적 정보를 조회한다.
|
|
|
|
DISTINCT ON (mmsi)로 최신 레코드만 반환한다.
|
|
"""
|
|
query = """
|
|
SELECT DISTINCT ON (mmsi) mmsi, name, vessel_type, length, width
|
|
FROM signal.t_vessel_static
|
|
WHERE mmsi = ANY(%s)
|
|
ORDER BY mmsi, time_bucket DESC
|
|
"""
|
|
|
|
try:
|
|
with get_conn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(query, (mmsi_list,))
|
|
rows = cur.fetchall()
|
|
result = {
|
|
row[0]: {
|
|
'name': row[1],
|
|
'vessel_type': row[2],
|
|
'length': row[3],
|
|
'width': row[4],
|
|
}
|
|
for row in rows
|
|
}
|
|
logger.info('fetch_static_info: %d vessels resolved', len(result))
|
|
return result
|
|
except Exception as e:
|
|
logger.error('fetch_static_info failed: %s', e)
|
|
return {}
|
|
|
|
|
|
def fetch_permit_mmsis() -> set[str]:
|
|
"""중국 허가어선 MMSI 목록을 조회한다.
|
|
|
|
signal.t_chnprmship_positions 테이블에서 DISTINCT mmsi를 반환한다.
|
|
"""
|
|
query = """
|
|
SELECT DISTINCT mmsi FROM signal.t_chnprmship_positions
|
|
"""
|
|
|
|
try:
|
|
with get_conn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(query)
|
|
rows = cur.fetchall()
|
|
result = {row[0] for row in rows}
|
|
logger.info('fetch_permit_mmsis: %d permitted vessels', len(result))
|
|
return result
|
|
except Exception as e:
|
|
logger.error('fetch_permit_mmsis failed: %s', e)
|
|
return set()
|