Auto-update: Tue Aug 6 21:30:52 PDT 2024

This commit is contained in:
sanj 2024-08-06 21:30:52 -07:00
parent 8b0ba25288
commit e073efb0fa

View file

@ -49,6 +49,9 @@ async def get_refreshed_weather(
debug(f"Passing date_time {date_time.strftime('%Y-%m-%d %H:%M:%S')}, {lat}/{lon} into get_weather")
day = await get_weather(date_time, lat, lon, force_refresh=True)
if day is None:
raise HTTPException(status_code=404, detail="No weather data found for the given date and location")
# Convert the day object to a JSON-serializable format
day_dict = {k: str(v) if isinstance(v, (dt_datetime, date)) else v for k, v in day.items()}
return JSONResponse(content={"weather": day_dict}, status_code=200)
@ -64,15 +67,16 @@ async def get_refreshed_weather(
async def get_weather(date_time: dt_datetime, latitude: float, longitude: float, force_refresh: bool = False):
daily_weather_data = None
fetch_new_data = force_refresh
daily_weather_data = None
if not force_refresh:
try:
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
if daily_weather_data:
debug(f"Daily weather data from db: {daily_weather_data}")
last_updated = await gis.dt(str(daily_weather_data['DailyWeather'].get('last_updated')))
last_updated = str(daily_weather_data['DailyWeather'].get('last_updated'))
last_updated = await gis.dt(last_updated)
stored_loc_data = unhexlify(daily_weather_data['DailyWeather'].get('location'))
stored_loc = loads(stored_loc_data)
stored_lat, stored_lon, stored_ele = stored_loc.y, stored_loc.x, stored_loc.z
@ -81,9 +85,7 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
request_haversine = haversine(latitude, longitude, stored_lat, stored_lon)
debug(f"\nINFO:\nlast updated {last_updated}\nstored lat: {stored_lat} - requested lat: {latitude}\nstored lon: {stored_lon} - requested lon: {longitude}\nHaversine: {request_haversine}")
if (last_updated and date_time <= dt_datetime.now(TZ) and
last_updated > date_time and request_haversine < 8 and
hourly_weather and len(hourly_weather) > 0):
if last_updated and (date_time <= dt_datetime.now(TZ) and last_updated > date_time and request_haversine < 8) and hourly_weather and len(hourly_weather) > 0:
debug(f"Using existing data")
fetch_new_data = False
else:
@ -115,6 +117,7 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
raise
except Exception as e:
err(f"Exception during API call or data storage: {e}")
err(f"Traceback: {traceback.format_exc()}")
raise HTTPException(status_code=500, detail=f"Error fetching or storing weather data: {str(e)}")
if daily_weather_data is None:
@ -124,6 +127,7 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
debug(f"Starting store_weather_to_db for datetime: {date_time.strftime('%Y-%m-%d %H:%M:%S')}")
try:
@ -285,6 +289,7 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
hourly_query = '''
SELECT HW.* FROM hourlyweather HW
WHERE HW.daily_weather_id = $1
ORDER BY HW.datetime ASC
'''
hourly_weather_records = await API.execute_read_query(hourly_query, daily_weather_data['id'], table_name='hourlyweather')
@ -294,8 +299,10 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
'HourlyWeather': hourly_weather_records,
}
debug(f"Retrieved weather data for {date_time.date()}")
return day
except Exception as e:
err(f"Unexpected error occurred in get_weather_from_db: {e}")
err(f"Traceback: {traceback.format_exc()}")
return None