From 6b42501fa89c0c622f61369be28e21c040514a74 Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Thu, 18 Jul 2024 11:43:00 +0200 Subject: [PATCH] Carpool event hooks --- amarillo/routers/carpool.py | 27 ++++++++------------------- amarillo/services/carpools.py | 5 ----- amarillo/services/hooks.py | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 amarillo/services/hooks.py diff --git a/amarillo/routers/carpool.py b/amarillo/routers/carpool.py index c741aa9..08c9382 100644 --- a/amarillo/routers/carpool.py +++ b/amarillo/routers/carpool.py @@ -12,7 +12,7 @@ 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.hooks import run_on_create, run_on_delete from amarillo.services.config import config from amarillo.utils.utils import assert_folder_exists @@ -51,6 +51,8 @@ async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Bod requesting_agency_id: str = Depends(verify_api_key)) -> Carpool: await verify_permission_for_same_agency_or_admin(carpool.agency, requesting_agency_id) + background_tasks.add_task(run_on_create, carpool) + logger.info(f"POST trip {carpool.agency}:{carpool.id}.") await assert_agency_exists(carpool.agency) @@ -90,12 +92,14 @@ async def get_carpool(agency_id: str, carpool_id: str, api_key: str = Depends(ve "description": "Carpool or agency not found"}, }, ) -async def delete_carpool(agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(verify_api_key)): +async def delete_carpool(background_tasks: BackgroundTasks, agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(verify_api_key)): await verify_permission_for_same_agency_or_admin(agency_id, requesting_agency_id) logger.info(f"Delete trip {agency_id}:{carpool_id}.") await assert_agency_exists(agency_id) await assert_carpool_exists(agency_id, carpool_id) + cp = await load_carpool(agency_id, carpool_id) + background_tasks.add_task(run_on_delete, cp) return await _delete_carpool(agency_id, carpool_id) @@ -111,12 +115,6 @@ async def _delete_carpool(agency_id: str, carpool_id: str): os.remove(f"data/enhanced/{agency_id}/{carpool_id}.json", ) except FileNotFoundError: pass - - try: - from amarillo.plugins.metrics import trips_deleted_counter - trips_deleted_counter.inc() - except ImportError: - pass async def store_carpool(carpool: Carpool) -> Carpool: @@ -125,17 +123,6 @@ async def store_carpool(carpool: Carpool) -> Carpool: await set_lastUpdated_if_unset(carpool) await save_carpool(carpool) - try: - from amarillo.plugins.metrics import trips_created_counter, trips_updated_counter - if(carpool_exists): - # logger.info("Incrementing trips updated") - trips_updated_counter.inc() - else: - # logger.info("Incrementing trips created") - trips_created_counter.inc() - except ImportError: - pass - return carpool async def set_lastUpdated_if_unset(carpool): @@ -176,4 +163,6 @@ async def delete_agency_carpools_older_than(agency_id, timestamp): if os.path.getmtime(carpool_file_name) < timestamp: m = re.search(r'([a-zA-Z0-9_-]+)\.json$', carpool_file_name) # TODO log deletion + cp = await load_carpool(agency_id, m[1]) + run_on_delete(cp) await _delete_carpool(agency_id, m[1]) diff --git a/amarillo/services/carpools.py b/amarillo/services/carpools.py index 7b6fd39..7790cbc 100644 --- a/amarillo/services/carpools.py +++ b/amarillo/services/carpools.py @@ -37,11 +37,6 @@ def purge_outdated_offers(self): if cp and self.is_outdated(cp): logger.info("Purge outdated offer %s", key) self.delete(cp.agency, cp.id) - try: - from amarillo.plugins.metrics import trips_deleted_counter - trips_deleted_counter.inc() - except ImportError: - pass def get(self, agency_id: str, carpool_id: str): return self.carpools.get(f"{agency_id}:{carpool_id}") diff --git a/amarillo/services/hooks.py b/amarillo/services/hooks.py new file mode 100644 index 0000000..5713585 --- /dev/null +++ b/amarillo/services/hooks.py @@ -0,0 +1,27 @@ +from typing import List +from amarillo.models.Carpool import Carpool + +class CarpoolEvents: + def on_create(cp : Carpool): + pass + def on_update(cp : Carpool): + pass + def on_delete(cp : Carpool): + pass + +carpool_event_listeners : List[CarpoolEvents] = [] + +def register_carpool_event_listener(cpe : CarpoolEvents): + carpool_event_listeners.append(cpe) + +def run_on_create(cp: Carpool): + for cpe in carpool_event_listeners: + cpe.on_create(cp) + +def run_on_update(cp: Carpool): + for cpe in carpool_event_listeners: + cpe.on_update(cp) + +def run_on_delete(cp: Carpool): + for cpe in carpool_event_listeners: + cpe.on_delete(cp) \ No newline at end of file