41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from app.models.mongodb.device_log import DeviceLog
|
|
|
|
|
|
class DeviceLogRepository:
|
|
async def create(self, log: DeviceLog) -> DeviceLog:
|
|
return await log.insert()
|
|
|
|
async def get_by_device(
|
|
self,
|
|
device_id: str,
|
|
event_type: str | None = None,
|
|
since: datetime | None = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> list[DeviceLog]:
|
|
query: dict = {"device_id": device_id}
|
|
if event_type:
|
|
query["event_type"] = event_type
|
|
if since:
|
|
query["timestamp"] = {"$gte": since}
|
|
|
|
return await (
|
|
DeviceLog.find(query)
|
|
.sort("-timestamp")
|
|
.skip(skip)
|
|
.limit(limit)
|
|
.to_list()
|
|
)
|
|
|
|
async def count_by_device(
|
|
self, device_id: str, event_type: str | None = None
|
|
) -> int:
|
|
query: dict = {"device_id": device_id}
|
|
if event_type:
|
|
query["event_type"] = event_type
|
|
return await DeviceLog.find(query).count()
|