Call enhancer in background task
Some checks failed
Amarillo/amarillo-gitea/amarillo-core/pipeline/head There was a failure building this commit

This commit is contained in:
Csaba 2024-05-17 12:58:02 +02:00
parent 53ad2a3f62
commit 461046df6a
3 changed files with 28 additions and 3 deletions

View file

@ -5,7 +5,8 @@ import os.path
import re
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 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.tests.sampledata import examples
from amarillo.services.config import config
from amarillo.utils.utils import assert_folder_exists
logger = logging.getLogger(__name__)
@ -21,6 +24,20 @@ router = APIRouter(
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("/",
operation_id="addcarpool",
summary="Add a new or update existing carpool",
@ -32,7 +49,7 @@ router = APIRouter(
"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:
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)
background_tasks.add_task(enhance_trip, carpool)
return carpool
# 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
await save_carpool(cp, 'data/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/enhanced/{agency_id}/{carpool_id}.json", )
except FileNotFoundError:
pass
try:
from amarillo.plugins.metrics import trips_deleted_counter

View file

@ -8,5 +8,6 @@ class Config(BaseSettings):
env: str = 'DEV'
graphhopper_base_url: str = 'https://api.mfdz.de/gh'
stop_sources_file: str = 'data/stop_sources.json'
enhancer_url: str = 'http://localhost:8001'
config = Config(_env_file='config', _env_file_encoding='utf-8')

View file

@ -65,6 +65,7 @@ class UserService:
logger.error(message)
raise HTTPException(status_code=400, detail=message)
#TODO: fix duplicate None api key
def add(self, user_conf: User):
user_id = user_conf.user_id