-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add logging and 🐛 Fix reset password
- Loading branch information
1 parent
09eec93
commit aa0a5d7
Showing
10 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import datetime | ||
from sqlalchemy.orm import Session | ||
from app.models.logs import Logs | ||
import logging | ||
|
||
logging.basicConfig(level=logging.INFO,format="%(asctime)s - %(levelname)s - %(message)s") | ||
|
||
def create_log_info(db: Session, user_id: str, message: str, station_id: str = None, charging_booth_id: str = None): # type: ignore | ||
current_datetime = datetime.datetime.now() | ||
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S,%f")[:-3] | ||
message = f"{formatted_datetime} - INFO - {message}" | ||
try: | ||
db_log = Logs(user_id=user_id, message=message, station_id=station_id, charging_booth_id=charging_booth_id) | ||
db.add(db_log) | ||
db.commit() | ||
db.refresh(db_log) | ||
return db_log | ||
except Exception as e: | ||
print(e) | ||
return None | ||
|
||
def get_logs(db: Session, skip: int = 0, limit: int = 10): | ||
try: | ||
return db.query(Logs).offset(skip).limit(limit).all() # type: ignore | ||
except Exception as e: | ||
print(e) | ||
return None | ||
|
||
def get_logs_by_user_id(db: Session, user_id: str, skip: int = 0, limit: int = 10): | ||
try: | ||
return db.query(Logs).filter(Logs.user_id == user_id).offset(skip).limit(limit).all() # type: ignore | ||
except Exception as e: | ||
print(e) | ||
return None | ||
|
||
def get_logs_by_station_id(db: Session, station_id: str, skip: int = 0, limit: int = 10): | ||
try: | ||
return db.query(Logs).filter(Logs.station_id == station_id).offset(skip).limit(limit).all() # type: ignore | ||
except Exception as e: | ||
print(e) | ||
return None | ||
|
||
def get_logs_by_charging_booth_id(db: Session, charging_booth_id: str, skip: int = 0, limit: int = 10): | ||
try: | ||
return db.query(Logs).filter(Logs.charging_booth_id == charging_booth_id).offset(skip).limit(limit).all() # type: ignore | ||
except Exception as e: | ||
print(e) | ||
return None | ||
|
||
def delete_log_by_time(db: Session, start_time: str, end_time: str): | ||
try: | ||
db.query(Logs).filter(Logs.created_at >= start_time).filter(Logs.created_at <= end_time).delete() # type: ignore | ||
db.commit() | ||
return True | ||
except Exception as e: | ||
print(e) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import datetime | ||
import uuid | ||
from sqlalchemy import Column, DateTime, ForeignKey, String | ||
from app.database import Base | ||
|
||
class Logs(Base): | ||
__tablename__ = "logs" | ||
|
||
id = Column(String, primary_key=True, index=True) | ||
user_id = Column(String, ForeignKey("users.id"), nullable=False) | ||
station_id = Column(String, ForeignKey("stations.id"), nullable=True) | ||
booth_id = Column(String, ForeignKey("charging_booth.booth_id"),nullable=True) | ||
message = Column(String) | ||
created_at = Column(DateTime, default=datetime.datetime.now()) | ||
|
||
def __init__(self, user_id, message, station_id=None, charging_booth_id=None): | ||
self.id = str(uuid.uuid4()) | ||
self.user_id = user_id | ||
self.station_id = station_id | ||
self.charging_booth_id = charging_booth_id | ||
self.message = message | ||
self.created_at = datetime.datetime.now() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.orm import Session | ||
from app.core.security import get_current_user | ||
from app.crud.logs import ( | ||
delete_log_by_time, | ||
get_logs, | ||
get_logs_by_charging_booth_id, | ||
get_logs_by_station_id, | ||
get_logs_by_user_id, | ||
) | ||
from app.database import get_db | ||
from app.schemas.log import DeleteLogs | ||
from app.schemas.user import User | ||
|
||
router = APIRouter( | ||
prefix="/log", | ||
tags=["Log"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.get("/") | ||
async def read_logs( | ||
db: Session = Depends(get_db), current_user: User = Depends(get_current_user) | ||
): | ||
if current_user.role not in ["superadmin", "stationadmin"]: | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
if logs := get_logs(db): | ||
return logs | ||
raise HTTPException(status_code=400, detail="Logs not found") | ||
|
||
|
||
@router.get("/{user_id}") | ||
async def read_logs_by_user_id( | ||
user_id: str, | ||
db: Session = Depends(get_db), | ||
current_user: User = Depends(get_current_user), | ||
): | ||
if current_user.role not in ["superadmin", "stationadmin"]: | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
if logs := get_logs_by_user_id(db, user_id): | ||
return logs | ||
raise HTTPException(status_code=400, detail="Logs not found") | ||
|
||
|
||
@router.get("/{station_id}") | ||
async def read_logs_by_station_id( | ||
station_id: str, | ||
db: Session = Depends(get_db), | ||
current_user: User = Depends(get_current_user), | ||
): | ||
if current_user.role not in ["superadmin", "stationadmin"]: | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
if logs := get_logs_by_station_id(db, station_id): | ||
return logs | ||
raise HTTPException(status_code=400, detail="Logs not found") | ||
|
||
|
||
@router.get("/{charging_booth_id}") | ||
async def read_logs_by_charging_booth_id( | ||
charging_booth_id: str, | ||
db: Session = Depends(get_db), | ||
current_user: User = Depends(get_current_user), | ||
): | ||
if current_user.role not in ["superadmin", "stationadmin"]: | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
if logs := get_logs_by_charging_booth_id(db, charging_booth_id): | ||
return logs | ||
raise HTTPException(status_code=400, detail="Logs not found") | ||
|
||
|
||
@router.delete("/") | ||
async def delete_log( | ||
delete_time: DeleteLogs, | ||
db: Session = Depends(get_db), | ||
current_user: User = Depends(get_current_user), | ||
): | ||
if current_user.role not in ["superadmin"]: | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
if delete_log_by_time(db, delete_time.start_date, delete_time.end_date): | ||
return {"message": "Logs deleted successfully"} | ||
raise HTTPException(status_code=400, detail="Logs not deleted") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.