From 85aaa1e6b1f340bb4564d7fbf3bc261dbf57e61c Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Mon, 19 Feb 2024 11:13:23 +0100 Subject: [PATCH] CRUD metrics --- amarillo/routers/carpool.py | 24 +++++++++++++++++++++--- amarillo/services/carpools.py | 5 +++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/amarillo/routers/carpool.py b/amarillo/routers/carpool.py index 0003fcc..f793578 100644 --- a/amarillo/routers/carpool.py +++ b/amarillo/routers/carpool.py @@ -38,9 +38,7 @@ async def post_carpool(carpool: Carpool = Body(..., examples=examples), logger.info(f"POST trip {carpool.agency}:{carpool.id}.") await assert_agency_exists(carpool.agency) - await set_lastUpdated_if_unset(carpool) - - await save_carpool(carpool) + await store_carpool(carpool) return carpool @@ -92,10 +90,30 @@ async def _delete_carpool(agency_id: str, carpool_id: str): logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.") os.remove(f"data/carpool/{agency_id}/{carpool_id}.json") + try: + from amarillo.plugins.metrics import trips_deleted_counter + trips_deleted_counter.inc() + except ImportError: + pass + + async def store_carpool(carpool: Carpool) -> Carpool: + carpool_exists = os.path.exists(f"data/carpool/{carpool.agency}/{carpool.id}.json") + 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): diff --git a/amarillo/services/carpools.py b/amarillo/services/carpools.py index 7790cbc..7b6fd39 100644 --- a/amarillo/services/carpools.py +++ b/amarillo/services/carpools.py @@ -37,6 +37,11 @@ class CarpoolService(): 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}")