sql_app: data: collect user connection times

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-07 16:11:39 +03:00
parent d9f3a9da4e
commit 2eed2db77e
3 changed files with 23 additions and 3 deletions

View File

@ -163,3 +163,9 @@ def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity
db.add(iot_entity) db.add(iot_entity)
db.commit() db.commit()
db.refresh(iot_entity) db.refresh(iot_entity)
def record_user_connection(db: Session, user: models.User, time: datetime):
entry = models.UserConnectionHistory(user_id=user.id, timestamp=time)
db.add(entry)
db.commit()
db.refresh(entry)

View File

@ -77,11 +77,13 @@ def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
data={"sub": db_user.username}, expires_delta=timedelta(minutes=15) data={"sub": db_user.username}, expires_delta=timedelta(minutes=15)
) )
crud.set_user_last_token(db, db_user.username, access_token) crud.set_user_last_token(db, db_user.username, access_token)
crud.record_user_connection(db, db_user, datetime.now())
return db_user return db_user
@app.get("/users/me/", response_model=schemas.User, tags=['Users']) @app.get("/users/me/", response_model=schemas.User, tags=['Users'])
def get_user_details(current_user: schemas.User = Depends(get_current_active_user)): def get_user_details(db: Session = Depends(get_db),
current_user: schemas.User = Depends(get_current_active_user)):
crud.record_user_connection(db, current_user, datetime.now())
return current_user return current_user
@app.post("/users/open", tags=['Users']) @app.post("/users/open", tags=['Users'])
@ -102,6 +104,8 @@ def issue_open_door_command(command: schemas.OpenDoorRequestTime,
command="OPEN", command="OPEN",
timestamp=datetime.now()) timestamp=datetime.now())
crud.record_door_access_log(db, log_entry) crud.record_door_access_log(db, log_entry)
crud.record_user_connection(db, current_user, datetime.now())
return device return device
raise err raise err
@ -122,6 +126,8 @@ def issue_close_door_command(command: schemas.CloseDoorRequest,
command="CLOSE", command="CLOSE",
timestamp=datetime.now()) timestamp=datetime.now())
crud.record_door_access_log(db, log_entry) crud.record_door_access_log(db, log_entry)
crud.record_user_connection(db, current_user, datetime.now())
return device return device
@app.post("/users/tkn", response_model=schemas.Token, tags=['Users']) @app.post("/users/tkn", response_model=schemas.Token, tags=['Users'])
@ -139,6 +145,7 @@ def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db:
data={"sub": form_data.username}, expires_delta=timedelta(minutes=15) data={"sub": form_data.username}, expires_delta=timedelta(minutes=15)
) )
crud.set_user_last_token(db, form_data.username, access_token) crud.set_user_last_token(db, form_data.username, access_token)
crud.record_user_connection(db, user, datetime.now())
return {"access_token": access_token, "token_type": "bearer"} return {"access_token": access_token, "token_type": "bearer"}
@app.get("/users/acesslist/", response_model=List[schemas.RoomOverview], tags=['Users']) @app.get("/users/acesslist/", response_model=List[schemas.RoomOverview], tags=['Users'])
@ -162,6 +169,7 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc
smoke_sensor_reading=sensors.smoke_sensor_reading smoke_sensor_reading=sensors.smoke_sensor_reading
) )
access_list.append(entry) access_list.append(entry)
crud.record_user_connection(db, user, datetime.now())
return access_list return access_list
@app.get("/admin/users/", response_model=List[schemas.User], tags=['Admin']) @app.get("/admin/users/", response_model=List[schemas.User], tags=['Admin'])

View File

@ -14,7 +14,7 @@ class User(Base):
passwd_salt = Column(String, nullable=False) passwd_salt = Column(String, nullable=False)
is_active = Column(Boolean, default=True, nullable=False) is_active = Column(Boolean, default=True, nullable=False)
last_token = Column(String, nullable=True) last_token = Column(String, nullable=True)
connections = relationship("UserConnectionHistory")
authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users")
@ -57,4 +57,10 @@ class RoomSensorData(Base):
smoke_sensor_reading = Column(Integer) smoke_sensor_reading = Column(Integer)
timestamp = Column(DateTime) timestamp = Column(DateTime)
class UserConnectionHistory(Base):
__tablename__ = "user_connection_history"
reading_id = Column(Integer, primary_key=True)
user_id = Column(Integer,ForeignKey("user_accounts.id"), index=True)
timestamp = Column(DateTime)
# TODO: Add table to store active sessions. May periodically clear. # TODO: Add table to store active sessions. May periodically clear.