Skip to content

Commit

Permalink
Bugfixes and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua committed Dec 9, 2023
1 parent f999f3c commit 13f11b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
security_opt:
- no-new-privileges:true
restart: unless-stopped
ports:
- 8086:8086
environment:
DOCKER_INFLUXDB_INIT_MODE: "setup"
DOCKER_INFLUXDB_INIT_USERNAME: "admin"
Expand Down
37 changes: 21 additions & 16 deletions provider_portal/app/api/customer_api/customer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,27 @@ def get_meter_measurements(self, start_time, end_time, data_interval, meter_UID)
Raises:
ValueError: If the specified time range is in the future or exceeds the maximum data points allowed.
"""
# Check if end_time is in the future
current_time = datetime.now(timezone(timedelta(hours=1)))
if datetime.fromisoformat(end_time.replace(" ", "+")) > current_time:
raise ValueError("error_no_data")

# Check the number of data points based on the specified time range and data interval
time_diff = datetime.fromisoformat(end_time.replace(" ", "+")) - datetime.fromisoformat(
start_time.replace(" ", "+"))
num_data_points = int(time_diff.total_seconds() / int(data_interval))

if num_data_points > 3600:
raise ValueError("error_over_maximum")

converted_start_time = start_time.replace(" ", "+")
converted_end_time = end_time.replace(" ", "+")
converted_data_interval = data_interval + "s"
try:
# Check if end_time is in the future
current_time = datetime.now(timezone(timedelta(hours=1)))
if datetime.fromisoformat(end_time.replace(" ", "+")) > current_time:
raise ValueError("error_no_data")

# Check the number of data points based on the specified time range and data interval
time_diff = datetime.fromisoformat(end_time.replace(" ", "+")) - datetime.fromisoformat(
start_time.replace(" ", "+"))
num_data_points = int(time_diff.total_seconds() / int(data_interval))

if num_data_points > 3600:
raise ValueError("error_over_maximum")

converted_start_time = start_time.replace(" ", "+")
converted_end_time = end_time.replace(" ", "+")
converted_data_interval = data_interval + "s"
except Exception as err:
logger.error(f"An exception occurred while decoding time format")
raise ValueError("error_decoding")


influxdb = InfluxDB()
reading = influxdb.read(start_time=converted_start_time, end_time=converted_end_time,
Expand Down
3 changes: 2 additions & 1 deletion provider_portal/app/api/customer_api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ def meter_measurements():
api = CustomerAPI(customer_UID=customer_UID, api_key=api_key)
auth_status = api.authenticate_customer_portal()
else:
logger.error(f"An error while authenticating has occurred")
raise ValueError("error_decoding")
except Exception as err:
logger.error(f"An error has occurred within the get measurements: {err}")
logger.error(f"An error has occurred within the get measurements, while validation: {err}")
res = {"message": "error_decoding"}
return Response(dict=res).create_response()

Expand Down

0 comments on commit 13f11b7

Please sign in to comment.