implemented endpoint for expired origins (origins without active leases)

This commit is contained in:
Oscar Krause 2024-02-27 20:53:32 +01:00
parent 128425e057
commit ffcf0d084e
4 changed files with 28 additions and 3 deletions

View File

@ -159,7 +159,13 @@ async def _origins_delete(request: Request):
return Response(status_code=201) 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): async def _origins_delete_origin_ref(request: Request, origin_ref: str):
if Origin.delete(db, [origin_ref]) == 1: if Origin.delete(db, [origin_ref]) == 1:
return Response(status_code=201) return Response(status_code=201)
@ -182,13 +188,13 @@ async def _leases(request: Request, origin: bool = False):
return JSONr(response) 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): async def _lease_delete_expired(request: Request):
Lease.delete_expired(db) Lease.delete_expired(db)
return Response(status_code=201) 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): async def _lease_delete(request: Request, lease_ref: str):
if Lease.delete(db, lease_ref) == 1: if Lease.delete(db, lease_ref) == 1:
return Response(status_code=201) return Response(status_code=201)

View File

@ -66,6 +66,16 @@ class Origin(Base):
session.close() session.close()
return deletions 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): class Lease(Base):
__tablename__ = "lease" __tablename__ = "lease"

View File

@ -116,6 +116,12 @@ async function deleteOrigin(origin_ref) {
xhr.send(); xhr.send();
} }
async function deleteExpiredOrigins() {
let xhr = new XMLHttpRequest();
xhr.open("DELETE", `/-/origins/expired`, true);
xhr.send();
}
async function deleteLease(lease_ref) { async function deleteLease(lease_ref) {
if (lease_ref === undefined) if (lease_ref === undefined)
lease_ref = window.prompt("Please enter 'lease_ref' which should be deleted"); lease_ref = window.prompt("Please enter 'lease_ref' which should be deleted");

View File

@ -13,6 +13,9 @@
<button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteOrigin().finally(() => load())"> <button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteOrigin().finally(() => load())">
delete origin delete origin
</button> </button>
<button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteExpiredOrigins().finally(() => load())">
delete all expired origins
</button>
<button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteOriginsWrapper()"> <button type="button" class="btn btn-sm btn-outline-danger" onclick="deleteOriginsWrapper()">
delete all delete all
</button> </button>