5가지 출력 이상 동시 해결:
1. stats_aggregator (이상 1, 5)
- aggregate_hourly에 by_category, by_zone JSON 집계 추가
- hour_start를 KST 기준으로 변경 (대시보드 표기와 boundary 일치)
2. event_generator 룰 정리 (이상 2, 3, 4)
- critical_risk 임계값 90→70 (risk.py CRITICAL 분류와 일치)
- territorial_sea_violation, contiguous_zone_high_risk, special_zone_entry 신설
(실측 zone_code: TERRITORIAL_SEA/CONTIGUOUS_ZONE/ZONE_*)
- 잘못된 NLL/SPECIAL_FISHING_* 룰 제거
- HIGH_RISK_VESSEL 신규 카테고리 (50~69 MEDIUM, 70+ CRITICAL)
- break 제거: 한 분석결과가 여러 카테고리에 동시 매칭 가능
3. dedup window prime 분산 (이상 5)
- 30/60/120/360분 → 33/67/127/367분
- 5분 사이클 boundary와 LCM 회피하여 정시 일제 만료 패턴 완화
4. lightweight path 신호 보강 (이상 2, 3, 4 근본 해결)
- vessel_store._tracks의 24h 누적 궤적으로 dark/spoof/speed_jump 산출
- 6,500 vessels(전체 93%)의 is_dark, spoofing_score가 비로소 채워짐
- compute_lightweight_risk_score에 dark gap, spoofing 가점 추가
(max 60→100 가능, CRITICAL 도달 가능)
시간 처리 원칙 적용:
- DB 컬럼은 모두 timestamptz 확인 완료
- aggregate_hourly KST aware datetime 사용
- pandas Timestamp는 source-internal 비교만 (안전)
145 lines
3.7 KiB
Python
145 lines
3.7 KiB
Python
from typing import Optional, Tuple
|
|
|
|
import pandas as pd
|
|
from algorithms.location import classify_zone
|
|
from algorithms.fishing_pattern import detect_fishing_segments, detect_trawl_uturn
|
|
from algorithms.dark_vessel import detect_ais_gaps
|
|
from algorithms.spoofing import detect_teleportation
|
|
|
|
|
|
def compute_lightweight_risk_score(
|
|
zone_info: dict,
|
|
sog: float,
|
|
is_permitted: Optional[bool] = None,
|
|
is_dark: bool = False,
|
|
gap_duration_min: int = 0,
|
|
spoofing_score: float = 0.0,
|
|
) -> Tuple[int, str]:
|
|
"""위치·허가·다크/스푸핑 기반 경량 위험도 (파이프라인 미통과 선박용).
|
|
|
|
pipeline path의 compute_vessel_risk_score와 동일한 임계값(70/50/30)을 사용해
|
|
분류 결과의 일관성을 유지한다. dark/spoofing 신호를 추가하여 max 100점 도달 가능.
|
|
|
|
Returns: (risk_score, risk_level)
|
|
"""
|
|
score = 0
|
|
|
|
# 1. 위치 기반 (최대 40점)
|
|
zone = zone_info.get('zone', '')
|
|
if zone == 'TERRITORIAL_SEA':
|
|
score += 40
|
|
elif zone == 'CONTIGUOUS_ZONE':
|
|
score += 10
|
|
elif zone.startswith('ZONE_'):
|
|
if is_permitted is not None and not is_permitted:
|
|
score += 25
|
|
|
|
# 2. 다크 베셀 (최대 25점)
|
|
if is_dark:
|
|
if gap_duration_min >= 60:
|
|
score += 25
|
|
elif gap_duration_min >= 30:
|
|
score += 10
|
|
|
|
# 3. 스푸핑 (최대 15점)
|
|
if spoofing_score > 0.7:
|
|
score += 15
|
|
elif spoofing_score > 0.5:
|
|
score += 8
|
|
|
|
# 4. 허가 이력 (최대 20점)
|
|
if is_permitted is not None and not is_permitted:
|
|
score += 20
|
|
|
|
score = min(score, 100)
|
|
|
|
if score >= 70:
|
|
level = 'CRITICAL'
|
|
elif score >= 50:
|
|
level = 'HIGH'
|
|
elif score >= 30:
|
|
level = 'MEDIUM'
|
|
else:
|
|
level = 'LOW'
|
|
|
|
return score, level
|
|
|
|
|
|
def compute_vessel_risk_score(
|
|
mmsi: str,
|
|
df_vessel: pd.DataFrame,
|
|
zone_info: Optional[dict] = None,
|
|
is_permitted: Optional[bool] = None,
|
|
) -> Tuple[int, str]:
|
|
"""선박별 종합 위반 위험도 (0~100점).
|
|
|
|
Returns: (risk_score, risk_level)
|
|
"""
|
|
if len(df_vessel) == 0:
|
|
return 0, 'LOW'
|
|
|
|
score = 0
|
|
|
|
# 1. 위치 기반 (최대 40점)
|
|
if zone_info is None:
|
|
last = df_vessel.iloc[-1]
|
|
zone_info = classify_zone(last['lat'], last['lon'])
|
|
|
|
zone = zone_info.get('zone', '')
|
|
if zone == 'TERRITORIAL_SEA':
|
|
score += 40
|
|
elif zone == 'CONTIGUOUS_ZONE':
|
|
score += 10
|
|
elif zone.startswith('ZONE_'):
|
|
# 특정어업수역 내 — 무허가면 가산
|
|
if is_permitted is not None and not is_permitted:
|
|
score += 25
|
|
|
|
# 2. 조업 행위 (최대 30점)
|
|
segs = detect_fishing_segments(df_vessel)
|
|
ts_fishing = [s for s in segs if s.get('in_territorial_sea')]
|
|
if ts_fishing:
|
|
score += 20
|
|
elif segs:
|
|
score += 5
|
|
|
|
uturn = detect_trawl_uturn(df_vessel)
|
|
if uturn.get('trawl_suspected'):
|
|
score += 10
|
|
|
|
# 3. AIS 조작 (최대 35점)
|
|
teleports = detect_teleportation(df_vessel)
|
|
if teleports:
|
|
score += 20
|
|
|
|
from algorithms.spoofing import count_speed_jumps
|
|
jumps = count_speed_jumps(df_vessel)
|
|
if jumps >= 3:
|
|
score += 10
|
|
elif jumps >= 1:
|
|
score += 5
|
|
|
|
gaps = detect_ais_gaps(df_vessel)
|
|
critical_gaps = [g for g in gaps if g['gap_min'] >= 60]
|
|
if critical_gaps:
|
|
score += 15
|
|
elif gaps:
|
|
score += 5
|
|
|
|
# 4. 허가 이력 (최대 20점)
|
|
if is_permitted is not None and not is_permitted:
|
|
score += 20
|
|
|
|
score = min(score, 100)
|
|
|
|
if score >= 70:
|
|
level = 'CRITICAL'
|
|
elif score >= 50:
|
|
level = 'HIGH'
|
|
elif score >= 30:
|
|
level = 'MEDIUM'
|
|
else:
|
|
level = 'LOW'
|
|
|
|
return score, level
|