From ffcf0d084e54a362f7979f428032fd683af08501 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Tue, 27 Feb 2024 20:53:32 +0100 Subject: [PATCH] implemented endpoint for expired origins (origins without active leases) --- app/main.py | 12 +++++++++--- app/orm.py | 10 ++++++++++ app/static/assets/js/helper.js | 6 ++++++ app/templates/views/dashboard_origins.html | 3 +++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 93c6212..7f2bb2f 100644 --- a/app/main.py +++ b/app/main.py @@ -159,7 +159,13 @@ async def _origins_delete(request: Request): return Response(status_code=201) -@app.delete('/-/origins/{origin_ref}', summary='* Origins') +@app.delete('/-/origins/expired', summary='* Delete all Origins without active Lease') +async def _origins_delete_expired(request: Request): + Origin.delete_expired(db) + return Response(status_code=201) + + +@app.delete('/-/origins/{origin_ref}', summary='* Delete specific Origin') async def _origins_delete_origin_ref(request: Request, origin_ref: str): if Origin.delete(db, [origin_ref]) == 1: return Response(status_code=201) @@ -182,13 +188,13 @@ async def _leases(request: Request, origin: bool = False): return JSONr(response) -@app.delete('/-/leases/expired', summary='* Leases') +@app.delete('/-/leases/expired', summary='* Delete all expired Leases') async def _lease_delete_expired(request: Request): Lease.delete_expired(db) return Response(status_code=201) -@app.delete('/-/lease/{lease_ref}', summary='* Lease') +@app.delete('/-/lease/{lease_ref}', summary='* Delete specific Lease') async def _lease_delete(request: Request, lease_ref: str): if Lease.delete(db, lease_ref) == 1: return Response(status_code=201) diff --git a/app/orm.py b/app/orm.py index 25e641b..95b0612 100644 --- a/app/orm.py +++ b/app/orm.py @@ -66,6 +66,16 @@ class Origin(Base): session.close() return deletions + @staticmethod + def delete_expired(engine: Engine) -> int: + session = sessionmaker(bind=engine)() + origins = session.query(Origin).join(Lease, Origin.origin_ref == Lease.origin_ref, isouter=True).filter(Lease.lease_ref.is_(None)).all() + origin_refs = [origin.origin_ref for origin in origins] + deletions = session.query(Origin).filter(Origin.origin_ref.in_(origin_refs)).delete() + session.commit() + session.close() + return deletions + class Lease(Base): __tablename__ = "lease" diff --git a/app/static/assets/js/helper.js b/app/static/assets/js/helper.js index 2f40de4..b7ab7ee 100644 --- a/app/static/assets/js/helper.js +++ b/app/static/assets/js/helper.js @@ -116,6 +116,12 @@ async function deleteOrigin(origin_ref) { xhr.send(); } +async function deleteExpiredOrigins() { + let xhr = new XMLHttpRequest(); + xhr.open("DELETE", `/-/origins/expired`, true); + xhr.send(); +} + async function deleteLease(lease_ref) { if (lease_ref === undefined) lease_ref = window.prompt("Please enter 'lease_ref' which should be deleted"); diff --git a/app/templates/views/dashboard_origins.html b/app/templates/views/dashboard_origins.html index be53086..e165720 100644 --- a/app/templates/views/dashboard_origins.html +++ b/app/templates/views/dashboard_origins.html @@ -13,6 +13,9 @@ +