Create services/service.py
Browse files- app/services/service.py +26 -0
app/services/service.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sqlalchemy.orm import Session
|
| 2 |
+
from app.models.model import DrowsinessEvent
|
| 3 |
+
from app.schemas.schema import DrowsinessEventCreate
|
| 4 |
+
from sqlalchemy import func
|
| 5 |
+
def log_event(db:Session,event:DrowsinessEventCreate)->DrowsinessEvent:
|
| 6 |
+
db_event=DrowsinessEvent(source=event.source,status=event.status)
|
| 7 |
+
db.add(db_event)
|
| 8 |
+
db.commit()
|
| 9 |
+
db.refresh(db_event)
|
| 10 |
+
return db_event
|
| 11 |
+
|
| 12 |
+
def get_all_events(db:Session):
|
| 13 |
+
return db.query(DrowsinessEvent).order_by(DrowsinessEvent.timestamp.desc()).all()
|
| 14 |
+
|
| 15 |
+
def get_event_stats(db:Session):
|
| 16 |
+
total=db.query(DrowsinessEvent).count()
|
| 17 |
+
by_status = (
|
| 18 |
+
db.query(DrowsinessEvent.status, func.count())
|
| 19 |
+
.group_by(DrowsinessEvent.status)
|
| 20 |
+
.all()
|
| 21 |
+
)
|
| 22 |
+
return {
|
| 23 |
+
"total":total,
|
| 24 |
+
"by_status":dict(by_status)
|
| 25 |
+
|
| 26 |
+
}
|