Call enhancer in background task
Some checks failed
Amarillo/amarillo-gitea/amarillo-core/pipeline/head There was a failure building this commit
Some checks failed
Amarillo/amarillo-gitea/amarillo-core/pipeline/head There was a failure building this commit
This commit is contained in:
parent
53ad2a3f62
commit
461046df6a
|
|
@ -5,7 +5,8 @@ import os.path
|
||||||
import re
|
import re
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
from fastapi import APIRouter, Body, HTTPException, status, Depends
|
from fastapi import APIRouter, Body, HTTPException, status, Depends, BackgroundTasks
|
||||||
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from amarillo.models.Carpool import Carpool
|
from amarillo.models.Carpool import Carpool
|
||||||
|
|
@ -13,6 +14,8 @@ from amarillo.models.User import User
|
||||||
from amarillo.services.oauth2 import get_current_user, verify_permission
|
from amarillo.services.oauth2 import get_current_user, verify_permission
|
||||||
from amarillo.tests.sampledata import examples
|
from amarillo.tests.sampledata import examples
|
||||||
|
|
||||||
|
from amarillo.services.config import config
|
||||||
|
from amarillo.utils.utils import assert_folder_exists
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -21,6 +24,20 @@ router = APIRouter(
|
||||||
tags=["carpool"]
|
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))
|
||||||
|
|
||||||
|
#TODO: use data/enhanced directory
|
||||||
|
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("/",
|
@router.post("/",
|
||||||
operation_id="addcarpool",
|
operation_id="addcarpool",
|
||||||
summary="Add a new or update existing carpool",
|
summary="Add a new or update existing carpool",
|
||||||
|
|
@ -32,7 +49,7 @@ router = APIRouter(
|
||||||
"description": "Agency does not exist"},
|
"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_user: User = Depends(get_current_user)) -> Carpool:
|
requesting_user: User = Depends(get_current_user)) -> Carpool:
|
||||||
verify_permission(f"{carpool.agency}:write", requesting_user)
|
verify_permission(f"{carpool.agency}:write", requesting_user)
|
||||||
|
|
||||||
|
|
@ -41,6 +58,8 @@ async def post_carpool(carpool: Carpool = Body(..., examples=examples),
|
||||||
|
|
||||||
await store_carpool(carpool)
|
await store_carpool(carpool)
|
||||||
|
|
||||||
|
background_tasks.add_task(enhance_trip, carpool)
|
||||||
|
|
||||||
return carpool
|
return carpool
|
||||||
|
|
||||||
# TODO 403
|
# TODO 403
|
||||||
|
|
@ -91,7 +110,11 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
|
||||||
# load and store, to receive pyinotify events and have file timestamp updated
|
# load and store, to receive pyinotify events and have file timestamp updated
|
||||||
await save_carpool(cp, 'data/trash')
|
await save_carpool(cp, 'data/trash')
|
||||||
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
|
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
|
||||||
|
try:
|
||||||
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
|
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
|
||||||
|
os.remove(f"data/enhanced/{agency_id}/{carpool_id}.json", )
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from amarillo.plugins.metrics import trips_deleted_counter
|
from amarillo.plugins.metrics import trips_deleted_counter
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,6 @@ class Config(BaseSettings):
|
||||||
env: str = 'DEV'
|
env: str = 'DEV'
|
||||||
graphhopper_base_url: str = 'https://api.mfdz.de/gh'
|
graphhopper_base_url: str = 'https://api.mfdz.de/gh'
|
||||||
stop_sources_file: str = 'data/stop_sources.json'
|
stop_sources_file: str = 'data/stop_sources.json'
|
||||||
|
enhancer_url: str = 'http://localhost:8001'
|
||||||
|
|
||||||
config = Config(_env_file='config', _env_file_encoding='utf-8')
|
config = Config(_env_file='config', _env_file_encoding='utf-8')
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,7 @@ class UserService:
|
||||||
logger.error(message)
|
logger.error(message)
|
||||||
raise HTTPException(status_code=400, detail=message)
|
raise HTTPException(status_code=400, detail=message)
|
||||||
|
|
||||||
|
#TODO: fix duplicate None api key
|
||||||
def add(self, user_conf: User):
|
def add(self, user_conf: User):
|
||||||
|
|
||||||
user_id = user_conf.user_id
|
user_id = user_conf.user_id
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue