Skip to content

Commit

Permalink
feat: start api (uvicorn bloom.services.api:app --host 0.0.0.0 --reload)
Browse files Browse the repository at this point in the history
  • Loading branch information
herve.le-bars committed Apr 22, 2024
1 parent 944934a commit 767d35c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
7 changes: 7 additions & 0 deletions backend/bloom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class Settings(BaseSettings):
data_folder:str=Field(default=str(Path(__file__).parent.parent.parent.joinpath('./data')))
db_url:str=Field(default='')

vessel_data:str=Field(default=str(Path(__file__).parent.parent.parent.joinpath('./data')\
.joinpath('./geometries/all_vessels.json')))
tracks_by_vessel_and_voyage_data:str=Field(default=str(Path(__file__).parent.parent.parent.joinpath('./data')\
.joinpath('./geometries/all_tracks_by_vessel_and_voyage.geo.json')))
vessel_last_position_data:str=Field(default=str(Path(__file__).parent.parent.parent.joinpath('./data')\
.joinpath('./geometries/vessels_latest_positions.json')))

logging_level:str=Field(
default="INFO",
pattern=r'NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL'
Expand Down
8 changes: 8 additions & 0 deletions backend/bloom/infra/repositories/repository_vessel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dependency_injector.providers import Callable
from sqlalchemy import func, select, update
from sqlalchemy.orm import Session
from sqlalchemy import select


class VesselRepository:
Expand All @@ -17,6 +18,13 @@ def __init__(

def get_vessel_by_id(self, session: Session, vessel_id: int) -> Union[Vessel | None]:
return session.get(sql_model.Vessel, vessel_id)

def get_vessel_by_mmsi(self, session: Session, vessel_mmsi: int) -> Union[Vessel | None]:
stmt=select(sql_model.Vessel).where(sql_model.Vessel.mmsi == vessel_mmsi)
e=session.execute(stmt).scalars()
if not e:
return []
return [VesselRepository.map_to_domain(vessel) for vessel in e]

def get_vessels_list(self, session: Session) -> list[Vessel]:
"""
Expand Down
49 changes: 49 additions & 0 deletions backend/bloom/services/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import Annotated

from fastapi import FastAPI, File, UploadFile

from bloom.config import settings
from bloom.container import UseCases
from bloom.domain.vessel import Vessel
from fastapi.encoders import jsonable_encoder

from bloom.logger import logger

import json

app = FastAPI()

@app.get("/vessels")
async def read_vessels():
with open(settings.vessel_data) as file:
return json.load(file)

@app.get("/vessels/*/lastPosition")
async def read_vessels_last_positions():
with open(settings.vessel_last_position_data) as file:
return json.load(file)

@app.get("/vessels/tracks")
async def read_vessels_tracks():
with open(settings.tracks_by_vessel_and_voyage_data) as file:
return json.load(file)

@app.get("/vessels/tracks/by-mmsi/{mmsi}")
async def read_vessels_tracks(mmsi:int):
with open(settings.tracks_by_vessel_and_voyage_data) as file:
return list(filter(lambda d: d ['properties']['vessel_mmsi'] in [mmsi],json.load(file)))


@app.get("/vessels/by-mmsi/{mmsi}/lastPosition")
async def read_vessels_last_positions(mmsi:int):
with open(settings.vessel_last_position_data) as file:
return list(filter(lambda d: d['vessel_mmsi'] in [mmsi],json.load(file)))

@app.get("/vessels/by-mmsi/{mmsi}")
async def read_vessels_by_mmsi(mmsi: int):
use_cases = UseCases()
vessel_repository = use_cases.vessel_repository()
db = use_cases.db()
with db.session() as session:
db = use_cases.db()
return vessel_repository.get_vessel_by_mmsi(session,mmsi)
70 changes: 68 additions & 2 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ scipy = "1.12.0"
slack-sdk = "~3.27"
SQLAlchemy = "~2.0"
pycountry = "23.12.11"
fastapi = "^0.110.2"
uvicorn = "^0.29.0"


[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
POSTGRES_PORT: 5432
STREAMLIT_SERVER_ADDRESS: ${STREAMLIT_SERVER_ADDRESS:-0.0.0.0}
ports:
- 8501:8501
- 8000:8000
networks:
- bloom_net
depends_on:
Expand Down

0 comments on commit 767d35c

Please sign in to comment.