Skip to content

Commit

Permalink
DBC22-2125: fixed current weather stations using wrong time data (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd authored May 15, 2024
1 parent 18ed7ce commit fce3f78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/backend/apps/feed/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timezone
import logging
from datetime import datetime, timezone
from typing import Dict
from urllib.parse import urljoin

Expand All @@ -13,8 +13,8 @@
OPEN511,
REGIONAL_WEATHER,
REGIONAL_WEATHER_AREAS,
WEBCAM,
REST_STOP,
WEBCAM,
)
from apps.feed.serializers import (
CarsEventSerializer,
Expand All @@ -23,9 +23,9 @@
EventFeedSerializer,
FerryAPISerializer,
RegionalWeatherSerializer,
RestStopSerializer,
WebcamAPISerializer,
WebcamFeedSerializer,
RestStopSerializer,
)
from django.conf import settings
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -325,7 +325,7 @@ def get_regional_weather_list_feed(self, resource_type, resource_name, serialize
# Env Canada sends this field as ISO time without
# offset, needed for python to parse correctly
forecast_issued = datetime.fromisoformat(f"{forecast_issued}+00:00")
except: # date parsing error
except Exception: # date parsing error
logger.error(f"Issued UTC sent by {code} as {forecast_issued}")

riseset_data = data.get("RiseSet") or {}
Expand Down Expand Up @@ -422,7 +422,10 @@ def get_current_weather_list_feed(self, resource_type, resource_name, serializer
response = requests.get(api_endpoint, headers=headers)
data = response.json()
datasets = data.get("Datasets") if data else None
issuedUtc = data.get("IssuedUtc")

# DBC22-2125 - use CollectionUtc of first dataset instead of IssuedUtc, to be improved
issuedUtc = datasets[0].get("CollectionUtc") if datasets and len(datasets) else None

elevation = data.get('WeatherStation').get("Elevation")
Longitude = data.get('WeatherStation').get("Longitude")
Latitude = data.get('WeatherStation').get("Latitude")
Expand Down Expand Up @@ -462,6 +465,7 @@ def get_current_weather_list_feed(self, resource_type, resource_name, serializer

except requests.RequestException as e:
logger.error(f"Error making API call for Area Code {station_number}: {e}")

try:
serializer.is_valid(raise_exception=True)
return json_objects
Expand All @@ -470,6 +474,7 @@ def get_current_weather_list_feed(self, resource_type, resource_name, serializer
field_errors = serializer.errors
for field, errors in field_errors.items():
logger.error(f"Field: {field}, Errors: {errors}")

except requests.RequestException:
return Response("Error fetching data from weather API", status=500)

Expand All @@ -479,7 +484,7 @@ def get_current_weather_list(self):
{"format": "json", "limit": 500}
)

# Rest Stop
# Rest Stop
def get_rest_stop_list_feed(self, resource_type, resource_name, serializer_cls, params=None):
"""Get data feed for list of objects."""
rest_stop_api_url = settings.DRIVEBC_REST_STOP_API_BASE_URL
Expand All @@ -502,8 +507,7 @@ def get_rest_stop_list_feed(self, resource_type, resource_name, serializer_cls,
'bbox': bbox,
}

serializer = serializer_cls(data=rest_stop_data,
many=isinstance(rest_stop_data, list))
serializer = serializer_cls(data=rest_stop_data, many=isinstance(rest_stop_data, list))
json_objects.append(rest_stop_data)

except requests.RequestException as e:
Expand All @@ -519,7 +523,7 @@ def get_rest_stop_list_feed(self, resource_type, resource_name, serializer_cls,
print(f"Field: {field}, Errors: {errors}")

def get_rest_stop_list(self):
return self.get_rest_stop_list_feed(
REST_STOP, 'reststop', RestStopSerializer,
{"format": "json", "limit": 500}
)
return self.get_rest_stop_list_feed(
REST_STOP, 'reststop', RestStopSerializer,
{"format": "json", "limit": 500}
)
2 changes: 1 addition & 1 deletion src/backend/apps/weather/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def populate_current_weather_from_data(new_current_weather_data):
existing_record = CurrentWeather.objects.filter(weather_station_name=weather_station_name).first()
issued_utc = new_current_weather_data.get('issuedUtc')
if issued_utc is not None:
issued_utc = datetime.datetime.strptime(issued_utc, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc)
issued_utc = datetime.datetime.strptime(issued_utc, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=datetime.timezone.utc)

data = {
'weather_station_name': weather_station_name,
Expand Down

0 comments on commit fce3f78

Please sign in to comment.