초기 커밋
This commit is contained in:
40
app/repositories/device_log_repo.py
Normal file
40
app/repositories/device_log_repo.py
Normal file
@@ -0,0 +1,40 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user