Skip to content

Commit

Permalink
création de fct_metrics, ajout des colonnes mmsi & ship_name, premier…
Browse files Browse the repository at this point in the history
… remplissage de la table lors de l'execution de create_updateexcursions_segments.py
  • Loading branch information
ejamet73 committed Oct 22, 2024
1 parent c5a707e commit 211b1a0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
4 changes: 3 additions & 1 deletion backend/alembic/versions/c32d65d6e6fd_create_fct_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def upgrade() -> None:
'fct_metrics',
sa.Column('timestamp', sa.DateTime, primary_key=True), #granule
sa.Column('vessel_id', sa.Integer, nullable=False), #vessel id
sa.Column('type', sa.String, nullable=False), #in_mpa or at_sea
sa.Column('vessel_mmsi', sa.Integer, nullable=False), #vessel mmsi
sa.Column('ship_name', sa.String, nullable=False), #vessel name
sa.Column('type', sa.String, nullable=False), #IN_MPA or AT_SEA
sa.Column('duration_total', sa.DateTime, nullable=False), #time spent at sea at timestamp
sa.Column('duration_fishing', sa.DateTime, nullable=True), #time spent fishing
sa.Column("mpa_name", sa.String, nullable=True), # if type is in_mpa, write here the mpa name
Expand Down
16 changes: 16 additions & 0 deletions backend/bloom/domain/metrics_new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from pydantic import BaseModel, ConfigDict
from typing import Generic,TypeVar, List
from typing_extensions import Annotated, Literal, Optional
from datetime import datetime, timedelta

class Metrics(BaseModel) :
model_config = ConfigDict(arbitrary_types_allowed=True)
timestamp : datetime
vessel_id: Optional[int]
vessel_mmsi: int
ship_name: str
type : str
duration_total : timedelta
duration_fishing: Optional[timedelta] = None
mpa_name : Optional[str] = None

11 changes: 11 additions & 0 deletions backend/bloom/infra/database/sql_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,14 @@ class MetricsVesselInActivity(Base):
#vessel_id: Mapped[Optional[int]]
#total_time_at_sea: Mapped[Optional[timedelta]]


class metrics(Base):
__tablename__ = "fct_metrics"
timestamp = Column("timestamp", DateTime(timezone=True), primary_key=True)
vessel_id= Column("vessel_id", Integer, ForeignKey("dim_vessel.id"), nullable=False)
type= Column("type", String, nullable= False)
duration_total= Column("duration_total", DateTime(timezone=True), nullable= False)
duration_fishing= Column("duration_fishing", DateTime(timezone=True))
mpa_name= Column("mpa_name", String, ForeignKey("dim_zone.name"))


18 changes: 18 additions & 0 deletions backend/bloom/infra/repositories/repository_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
stmt = select(
sql_model.Vessel.id,
#sql_model.Segment.excursion_id,
sql_model.Vessel.mmsi
sql_model.Segment.in_amp_zone #segment dans une zone ou non
sql_model.Segment.type #default d'ais ou pas

).join(
sql_model.Excursion,
sql_model.Segment.excursion_id == sql_model.Excursion.id

).join(
sql_model.Vessel,
sql_model.Excursion.vessel_id == sql_model.Vessel.id

).filter(
sql_model.Segment.last_vessel_segment == True
)
23 changes: 22 additions & 1 deletion backend/bloom/tasks/create_update_excursions_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from bloom.logger import logger
from bloom.domain.rel_segment_zone import RelSegmentZone
from bloom.infra.repositories.repository_rel_segment_zone import RelSegmentZoneRepository
from bloom.domain.metrics_new import Metrics #1

warnings.filterwarnings("ignore")

Expand Down Expand Up @@ -93,7 +94,7 @@ def run():
vessel_position_repository = use_cases.vessel_position_repository()
port_repository = use_cases.port_repository()
excursion_repository = use_cases.excursion_repository()

metrics_repository = use_cases.metrics_repository() #1
nb_created_excursion = 0
nb_closed_excursion = 0

Expand Down Expand Up @@ -299,20 +300,38 @@ def get_time_of_departure():
new_rels = []
excursions = {}
segments = []
new_metricss=[]
max_created_updated = point_in_time
for segment, zones in result.items():
segment_in_zone = False
df= metrics_repository.get_vessel_excursion_segment_by_id(session,segment.id) #1
for zone in zones:
segment_in_zone = True
new_rels.append(RelSegmentZone(segment_id=segment.id, zone_id=zone.id))
type='AT_SEA' #1
if zone.category == "amp":
segment.in_amp_zone = True
type='IN_AMP' #1
elif zone.category.startswith("Fishing coastal waters"):
segment.in_costal_waters = True
elif zone.category == "Territorial seas":
segment.in_territorial_waters = True

new_metrics= Metrics(#1
timestamp = point_in_time, #1
vessel_id = df['vessel_id'], #1
vessel_mmsi = df['vessel_mmsi'], #1
ship_name = df['ship_name'], #1
type = type, #1
duration_total = None, #1
duration_fishing = segment.segment_duration if segment.type== 'FISHING' else None, #1
mpa_name = zone.name #1
) #1
if segment_in_zone:
segments.append(segment)

new_metricss.append(new_metrics) #1

# Mise à jour de l'excursion avec le temps passé dans chaque type de zone
excursion = excursions.get(segment.excursion_id,
excursion_repository.get_excursion_by_id(session, segment.excursion_id))
Expand Down Expand Up @@ -353,6 +372,8 @@ def get_time_of_departure():
logger.info(f"{len(segments)} segments mis à jour")
RelSegmentZoneRepository.batch_create_rel_segment_zone(session, new_rels)
logger.info(f"{len(new_rels)} associations(s) créées")
metrics_repository.batch_create_metrics(session, new_metricss) #1
logger.info(f"{len(new_metricss)} metrics(s) créés") #1
vessels_ids = set(exc.vessel_id for exc in excursions.values())
nb_last = segment_repository.update_last_segments(session, vessels_ids)
logger.info(f"{nb_last} derniers segments mis à jour")
Expand Down

0 comments on commit 211b1a0

Please sign in to comment.