Skip to content

Commit

Permalink
Fixed codes
Browse files Browse the repository at this point in the history
  • Loading branch information
FxJYg committed Feb 2, 2025
1 parent 7928ad8 commit f0a9c71
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
20 changes: 11 additions & 9 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ def create_command(payload: CommandRequest, db: Session = Depends(get_db)):
@return returns a json object with field of "data" under which there is the payload now pulled from the database
"""
# TODO:(Member) Implement this endpoint
new_command = Command(command_type=payload.command_type, params=payload.params)

db.add(new_command)
db.commit()
db.refresh(new_command)


return {"data": new_command}

try:
new_command = Command(command_type=payload.command_type, params=payload.params)

db.add(new_command)
db.commit()
db.refresh(new_command)

return {"data": new_command}
except Exception as e:
db.rollback()
raise HTTPException(status_code=500, detail="Fail to create command {str(e)}")



Expand Down
16 changes: 10 additions & 6 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time
from time import time, strftime
from collections.abc import Callable
from typing import Any
from fastapi import Request, Response
Expand All @@ -20,14 +20,18 @@ async def dispatch(
@return Response from endpoint
"""
# TODO:(Member) Finish implementing this method
start_time = time.time()
request_time = time.strftime('%Y-%m-%d %H:%M:%S')
start_time = time()
request_time = strftime('%Y-%m-%d %H:%M:%S')
logger.info(f"Request started: [{request_time}] | {request.method} | {request.url.path} | Params: {request.query_params}")

response = await call_next(request)

raw_byte = b""
async for chunk in response.body_iterator:
raw_byte += chunk

# Log response details and duration
duration = time.time() - start_time
duration = time() - start_time

logger.info(f"Response sent: {response.status_code} | Duration: {duration:.4f}s")
return response
logger.info(f"Response sent: {raw_byte} {response.status_code} | Duration: {duration:.4f}s")
return Response(content=raw_byte, status_code=response.status_code, headers=dict(response.headers), media_type=response.media_type)
8 changes: 4 additions & 4 deletions backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def validate_params_format(self):
The format of the comma seperated values is "data1,data2" so no spaces between data and the commas.
"""
# TODO: (Member) Implement this method
if self.params == None and self.format == None:
if self.params is None and self.format is None:
return self
elif self.params == None or self.format == None:
raise ValueError()
elif self.params is None or self.format is None:
raise ValueError("One of params or format is not initialized.")

if len(self.params.split(",")) == len(self.format.split(",")):
return self
else:
raise ValueError()
raise ValueError("Number of comma separated value of params and format does not match.")



Expand Down

0 comments on commit f0a9c71

Please sign in to comment.