mirror of
https://github.com/Sosokker/HomieCare.git
synced 2025-12-20 02:14:05 +01:00
Add action query and endpoint
This commit is contained in:
parent
e2b4fdc552
commit
c3554dad3c
@ -36,11 +36,9 @@ class PredictionData(Base):
|
|||||||
indoor_temp = Column(Float)
|
indoor_temp = Column(Float)
|
||||||
|
|
||||||
|
|
||||||
# class ActionData(Base):
|
class ActionData(Base):
|
||||||
# __tablename__ = "action"
|
__tablename__ = "action"
|
||||||
|
|
||||||
# id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
# timestamp = Column(DateTime)
|
timestamp = Column(DateTime)
|
||||||
# action = Column(Text)
|
action = Column(Text)
|
||||||
# probability = Column(Float)
|
|
||||||
# camera_id = Column(Integer)
|
|
||||||
16
StreamServer/src/query/action.py
Normal file
16
StreamServer/src/query/action.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from sqlalchemy import func
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from models import ActionData
|
||||||
|
|
||||||
|
|
||||||
|
def get_weekly_action_data(session: Session):
|
||||||
|
"""
|
||||||
|
Get the list of actions for the this week.
|
||||||
|
"""
|
||||||
|
date_7_days_ago = (datetime.now() - timedelta(days=7)).date()
|
||||||
|
|
||||||
|
return session.query(ActionData).filter(
|
||||||
|
func.date(ActionData.timestamp) >= date_7_days_ago
|
||||||
|
).all()
|
||||||
29
StreamServer/src/routers/action.py
Normal file
29
StreamServer/src/routers/action.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from fastapi import APIRouter, HTTPException, Depends
|
||||||
|
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from scheme import ActionData
|
||||||
|
from query.action import get_weekly_action_data
|
||||||
|
from database import engine, SessionLocal, Base
|
||||||
|
|
||||||
|
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
#Dependency
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try :
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/week", response_model=list[ActionData])
|
||||||
|
async def get_week_action(db: Session = Depends(get_db)):
|
||||||
|
"""Return the list of ActionData for the this week."""
|
||||||
|
action_data = get_weekly_action_data(db)
|
||||||
|
if not action_data:
|
||||||
|
raise HTTPException(status_code=404, detail="Action data for the last week not found")
|
||||||
|
return action_data
|
||||||
@ -88,11 +88,6 @@ class HealthData(BaseModel):
|
|||||||
outdoor_humidity: float
|
outdoor_humidity: float
|
||||||
|
|
||||||
|
|
||||||
# class Action(BaseModel):
|
class ActionData(BaseModel):
|
||||||
# timestamp: Optional[datetime]
|
timestamp: Optional[datetime]
|
||||||
# action: Optional[str]
|
action: Optional[str]
|
||||||
# probability: Optional[float]
|
|
||||||
# camera_id: Optional[int]
|
|
||||||
|
|
||||||
# class Config:
|
|
||||||
# from_attributes = True
|
|
||||||
Loading…
Reference in New Issue
Block a user