Compare commits

..

4 commits

Author SHA1 Message Date
Csaba 213967511d Merge branch 'fastapi-enhancer' of git.gerhardt.io:amarillo/amarillo-core into fastapi-enhancer
Some checks failed
Amarillo/amarillo-gitea/amarillo-core/pipeline/head There was a failure building this commit
2024-05-30 13:05:30 +02:00
Csaba 233f7528df Call enhancer in background task 2024-05-30 12:45:42 +02:00
Csaba 461046df6a Call enhancer in background task
Some checks failed
Amarillo/amarillo-gitea/amarillo-core/pipeline/head There was a failure building this commit
2024-05-17 12:58:02 +02:00
Csaba 53ad2a3f62 Updated Jenkinsfile to use grfs-exporter 2024-05-03 13:50:09 +02:00
5 changed files with 30 additions and 5 deletions

2
Jenkinsfile vendored
View file

@ -7,7 +7,7 @@ pipeline {
DOCKER_REGISTRY_URL = 'https://git.gerhardt.io' DOCKER_REGISTRY_URL = 'https://git.gerhardt.io'
OWNER = 'amarillo' OWNER = 'amarillo'
IMAGE_NAME = 'amarillo' IMAGE_NAME = 'amarillo'
AMARILLO_DISTRIBUTION = '0.2' AMARILLO_DISTRIBUTION = '0.1'
TAG = "${AMARILLO_DISTRIBUTION}.${BUILD_NUMBER}" TAG = "${AMARILLO_DISTRIBUTION}.${BUILD_NUMBER}"
PLUGINS = 'amarillo-metrics amarillo-enhancer amarillo-grfs-exporter' PLUGINS = 'amarillo-metrics amarillo-enhancer amarillo-grfs-exporter'
DEPLOY_WEBHOOK_URL = 'http://amarillo.mfdz.de:8888/mitanand' DEPLOY_WEBHOOK_URL = 'http://amarillo.mfdz.de:8888/mitanand'

View file

@ -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.")
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: try:
from amarillo.plugins.metrics import trips_deleted_counter from amarillo.plugins.metrics import trips_deleted_counter

View file

@ -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')

View file

@ -20,7 +20,7 @@ from amarillo.services.secrets import secrets
SECRET_KEY = secrets.secret_key SECRET_KEY = secrets.secret_key
ALGORITHM = "HS256" ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 7*24*60 ACCESS_TOKEN_EXPIRE_MINUTES = 30
logging.config.fileConfig('logging.conf', disable_existing_loggers=False) logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger("main") logger = logging.getLogger("main")

View file

@ -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