release: bump version to 0.52.0
This commit is contained in:
@@ -6,6 +6,7 @@ import json
|
||||
from typing import Any, Iterable
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import Float
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.vessel import AISConflictRecord, AISRawObservation, AISSourceHealth
|
||||
@@ -17,6 +18,10 @@ from app.services.vessel_types import normalize_vessel_type_name
|
||||
|
||||
VESSEL_AIS_SCHEMA = "vessel_ais"
|
||||
DEFAULT_AGGREGATION_WINDOW_HOURS = 24
|
||||
DEFAULT_SNAPSHOT_WINDOW_MINUTES = 60
|
||||
MAX_SNAPSHOT_LIMIT = 5000
|
||||
MAX_SNAPSHOT_CANDIDATE_MULTIPLIER = 20
|
||||
MAX_SNAPSHOT_CANDIDATE_OBSERVATIONS = 100_000
|
||||
BARENTSWATCH_DELIVERY_MODE = "polling"
|
||||
BARENTSWATCH_TRANSPORT = "http"
|
||||
AISSTREAM_DELIVERY_MODE = "realtime_stream"
|
||||
@@ -564,6 +569,46 @@ async def get_aggregated_vessels(
|
||||
return vessels
|
||||
|
||||
|
||||
async def get_aggregated_vessels_snapshot(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
bbox: tuple[float, float, float, float],
|
||||
limit: int = 1000,
|
||||
observed_since: datetime | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Return a bounded viewport snapshot without loading the global AIS window."""
|
||||
|
||||
observed_since = observed_since or (
|
||||
datetime.now(UTC) - timedelta(minutes=DEFAULT_SNAPSHOT_WINDOW_MINUTES)
|
||||
)
|
||||
safe_limit = min(max(int(limit or 1000), 1), MAX_SNAPSHOT_LIMIT)
|
||||
candidate_limit = min(
|
||||
max(safe_limit * MAX_SNAPSHOT_CANDIDATE_MULTIPLIER, safe_limit),
|
||||
MAX_SNAPSHOT_CANDIDATE_OBSERVATIONS,
|
||||
)
|
||||
lon_min, lat_min, lon_max, lat_max = bbox
|
||||
payload_lon = AISRawObservation.normalized_payload["lon"].as_string().cast(Float)
|
||||
payload_lat = AISRawObservation.normalized_payload["lat"].as_string().cast(Float)
|
||||
|
||||
stmt = (
|
||||
select(AISRawObservation)
|
||||
.where(AISRawObservation.target_schema == VESSEL_AIS_SCHEMA)
|
||||
.where(AISRawObservation.observed_at >= observed_since)
|
||||
.where(payload_lon >= lon_min)
|
||||
.where(payload_lon <= lon_max)
|
||||
.where(payload_lat >= lat_min)
|
||||
.where(payload_lat <= lat_max)
|
||||
.order_by(AISRawObservation.observed_at.desc(), AISRawObservation.id.desc())
|
||||
.limit(candidate_limit)
|
||||
)
|
||||
|
||||
result = await db.execute(stmt)
|
||||
if not hasattr(result, "scalars"):
|
||||
return []
|
||||
vessels = await aggregate_vessel_observations(db, result.scalars().all())
|
||||
return vessels[:safe_limit]
|
||||
|
||||
|
||||
async def get_aggregated_vessel(db: AsyncSession, mmsi: int) -> dict[str, Any] | None:
|
||||
observations = await get_vessel_raw_observations(db, mmsi, limit=1000)
|
||||
vessels = await aggregate_vessel_observations(db, observations)
|
||||
|
||||
Reference in New Issue
Block a user