Skip to content

Commit

Permalink
API, DB: add document counting for entity overview request
Browse files Browse the repository at this point in the history
Extends return value of `get_latest_snapshots` method in `Database`
and extends API to provide that information.
  • Loading branch information
DavidB137 committed Aug 24, 2023
1 parent c8695eb commit 01d2965
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion dp3/api/internal/entity_response_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class EntityState(BaseModel):
class EntityEidList(BaseModel):
"""List of entity eids and their data based on latest snapshot
Includes timestamp of latest snapshot creation.
Includes timestamp of latest snapshot creation, count of returned documents
and total count of documents available under specified filter.
Data does not include history of observations attributes and timeseries.
"""

time_created: Optional[datetime]
count: int
total_count: int
data: list[dict]


Expand Down
9 changes: 6 additions & 3 deletions dp3/api/routers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,21 @@ async def list_entity_type_eids(
If `eid_filter` is not empty, returns only `id`s containing substring `eid_filter`.
"""
cursor = DB.get_latest_snapshots(etype, eid_filter).skip(skip).limit(limit)
cursor, total_count = DB.get_latest_snapshots(etype, eid_filter)
cursor_page = cursor.skip(skip).limit(limit)

time_created = None

# Remove _id field
result = list(cursor)
result = list(cursor_page)
for r in result:
time_created = r["_time_created"]
del r["_time_created"]
del r["_id"]

return EntityEidList(time_created=time_created, data=result)
return EntityEidList(
time_created=time_created, count=len(result), total_count=total_count, data=result
)


@router.get("/{etype}/{eid}")
Expand Down
8 changes: 6 additions & 2 deletions dp3/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,16 @@ 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, eid_filter: str = "") -> pymongo.cursor.Cursor:
def get_latest_snapshots(
self, etype: str, eid_filter: str = ""
) -> (pymongo.cursor.Cursor, int):
"""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`.
Also returns total document count (after filtering).
"""
# Check `etype`
self._assert_etype_exists(etype)
Expand All @@ -349,7 +353,7 @@ def get_latest_snapshots(self, etype: str, eid_filter: str = "") -> pymongo.curs
if eid_filter != "":
query["eid"] = {"$regex": eid_filter}

return self._db[snapshot_col].find(query)
return self._db[snapshot_col].find(query), self._db[snapshot_col].count_documents(query)

def get_snapshots(
self, etype: str, eid: str, t1: Optional[datetime] = None, t2: Optional[datetime] = None
Expand Down

0 comments on commit 01d2965

Please sign in to comment.