From ea6977f43cc0e5ed11b0561d9454901ad315c006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Benko?= Date: Mon, 14 Aug 2023 09:07:14 +0000 Subject: [PATCH] API: add eid filter on `GET /entity/{etype}` endpoint --- dp3/api/routers/entity.py | 6 ++++-- dp3/database/database.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/dp3/api/routers/entity.py b/dp3/api/routers/entity.py index 23455c5c..97f61a8a 100644 --- a/dp3/api/routers/entity.py +++ b/dp3/api/routers/entity.py @@ -32,15 +32,17 @@ async def check_etype(etype: str): @router.get("/{etype}") async def list_entity_type_eids( - etype: str, skip: NonNegativeInt = 0, limit: PositiveInt = 20 + etype: str, eid_filter: str = "", skip: NonNegativeInt = 0, limit: PositiveInt = 20 ) -> EntityEidList: """List latest snapshots of all `id`s present in database under `etype`. Contains only latest snapshot. Uses pagination. + + If `eid_filter` is not empty, returns only `id`s containing substring `eid_filter`. """ - cursor = DB.get_latest_snapshots(etype).skip(skip).limit(limit) + cursor = DB.get_latest_snapshots(etype, eid_filter).skip(skip).limit(limit) time_created = None diff --git a/dp3/database/database.py b/dp3/database/database.py index f00f6a80..6840f9a8 100644 --- a/dp3/database/database.py +++ b/dp3/database/database.py @@ -329,10 +329,12 @@ def get_latest_snapshot(self, etype: str, eid: str) -> dict: snapshot_col = self._snapshots_col_name(etype) return self._db[snapshot_col].find_one({"eid": eid}, sort=[("_id", -1)]) or {} - def get_latest_snapshots(self, etype: str) -> pymongo.cursor.Cursor: + def get_latest_snapshots(self, etype: str, eid_filter: str = "") -> pymongo.cursor.Cursor: """Get latest snapshots of given `etype`. This method is useful for displaying data on web. + + If `eid_filter` is not empty, returns only `eid`s containing substring `eid_filter`. """ # Check `etype` self._assert_etype_exists(etype) @@ -343,7 +345,11 @@ def get_latest_snapshots(self, etype: str) -> pymongo.cursor.Cursor: return self._db[snapshot_col].find() latest_snapshot_date = latest_snapshot["_time_created"] - return self._db[snapshot_col].find({"_time_created": latest_snapshot_date}) + query = {"_time_created": latest_snapshot_date} + if eid_filter != "": + query["eid"] = {"$regex": eid_filter} + + return self._db[snapshot_col].find(query) def get_snapshots( self, etype: str, eid: str, t1: Optional[datetime] = None, t2: Optional[datetime] = None