Skip to content

Commit

Permalink
API: add eid filter on GET /entity/{etype} endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidB137 committed Aug 14, 2023
1 parent facea00 commit ea6977f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions dp3/api/routers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions dp3/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit ea6977f

Please sign in to comment.