Skip to content

Commit

Permalink
Call enhancer in background task
Browse files Browse the repository at this point in the history
  • Loading branch information
Francia Csaba committed Aug 1, 2024
1 parent 4af9cf3 commit 7db9706
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions amarillo/routers/carpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import re
from glob import glob

from fastapi import APIRouter, Body, Header, HTTPException, status, Depends
from fastapi import APIRouter, Body, Header, HTTPException, status, Depends, BackgroundTasks
import requests
from datetime import datetime

from amarillo.models.Carpool import Carpool
from amarillo.routers.agencyconf import verify_api_key, verify_permission_for_same_agency_or_admin
from amarillo.tests.sampledata import examples

from amarillo.services.config import config
from amarillo.utils.utils import assert_folder_exists

logger = logging.getLogger(__name__)

Expand All @@ -20,6 +23,19 @@
tags=["carpool"]
)

#TODO: housekeeping for outdated trips

def enhance_trip(carpool: Carpool):
response = requests.post(f"{config.enhancer_url}", carpool.model_dump_json())
enhanced_carpool = Carpool(**json.loads(response.content))

folder = f'data/enhanced/{carpool.agency}'
filename = f'{folder}/{carpool.id}.json'

assert_folder_exists(folder)
with open(filename, 'w', encoding='utf-8') as f:
f.write(enhanced_carpool.model_dump_json())

@router.post("/",
operation_id="addcarpool",
summary="Add a new or update existing carpool",
Expand All @@ -31,7 +47,7 @@
"description": "Agency does not exist"},
})
async def post_carpool(carpool: Carpool = Body(..., examples=examples),
async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Body(..., examples=examples),
requesting_agency_id: str = Depends(verify_api_key)) -> Carpool:
await verify_permission_for_same_agency_or_admin(carpool.agency, requesting_agency_id)

Expand All @@ -40,6 +56,8 @@ async def post_carpool(carpool: Carpool = Body(..., examples=examples),

await store_carpool(carpool)

background_tasks.add_task(enhance_trip, carpool)

return carpool

# TODO 403
Expand Down Expand Up @@ -88,7 +106,11 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
# load and store, to receive pyinotify events and have file timestamp updated
await save_carpool(cp, 'data/trash')
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
try:
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
os.remove(f"data/enhanced/{agency_id}/{carpool_id}.json", )
except FileNotFoundError:
pass

try:
from amarillo.plugins.metrics import trips_deleted_counter
Expand Down
1 change: 1 addition & 0 deletions amarillo/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Config(BaseSettings):
env: str = 'DEV'
graphhopper_base_url: str = 'https://api.mfdz.de/gh'
stop_sources_file: str = 'conf/stop_sources.json'
enhancer_url: str = 'http://localhost:8001'

model_config = ConfigDict(extra='allow') # Allow plugins to add extra values

Expand Down

0 comments on commit 7db9706

Please sign in to comment.