Auto-update: Tue Aug 6 21:30:52 PDT 2024
This commit is contained in:
parent
8b0ba25288
commit
e073efb0fa
1 changed files with 20 additions and 13 deletions
|
@ -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")
|
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)
|
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
|
# 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()}
|
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)
|
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):
|
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
|
fetch_new_data = force_refresh
|
||||||
|
daily_weather_data = None
|
||||||
|
|
||||||
if not force_refresh:
|
if not force_refresh:
|
||||||
try:
|
try:
|
||||||
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
||||||
if daily_weather_data:
|
if daily_weather_data:
|
||||||
debug(f"Daily weather data from db: {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_data = unhexlify(daily_weather_data['DailyWeather'].get('location'))
|
||||||
stored_loc = loads(stored_loc_data)
|
stored_loc = loads(stored_loc_data)
|
||||||
stored_lat, stored_lon, stored_ele = stored_loc.y, stored_loc.x, stored_loc.z
|
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)
|
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}")
|
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
|
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:
|
||||||
last_updated > date_time and request_haversine < 8 and
|
|
||||||
hourly_weather and len(hourly_weather) > 0):
|
|
||||||
debug(f"Using existing data")
|
debug(f"Using existing data")
|
||||||
fetch_new_data = False
|
fetch_new_data = False
|
||||||
else:
|
else:
|
||||||
|
@ -91,7 +93,7 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Error checking existing weather data: {e}")
|
err(f"Error checking existing weather data: {e}")
|
||||||
fetch_new_data = True
|
fetch_new_data = True
|
||||||
|
|
||||||
if fetch_new_data:
|
if fetch_new_data:
|
||||||
debug(f"Fetching new weather data")
|
debug(f"Fetching new weather data")
|
||||||
request_date_str = date_time.strftime("%Y-%m-%d")
|
request_date_str = date_time.strftime("%Y-%m-%d")
|
||||||
|
@ -105,7 +107,7 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
|
||||||
store_result = await store_weather_to_db(date_time, weather_data)
|
store_result = await store_weather_to_db(date_time, weather_data)
|
||||||
if store_result != "SUCCESS":
|
if store_result != "SUCCESS":
|
||||||
raise HTTPException(status_code=500, detail=f"Failed to store weather data: {store_result}")
|
raise HTTPException(status_code=500, detail=f"Failed to store weather data: {store_result}")
|
||||||
|
|
||||||
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
daily_weather_data = await get_weather_from_db(date_time, latitude, longitude)
|
||||||
if daily_weather_data is None:
|
if daily_weather_data is None:
|
||||||
raise HTTPException(status_code=500, detail="Weather data was not properly stored.")
|
raise HTTPException(status_code=500, detail="Weather data was not properly stored.")
|
||||||
|
@ -115,15 +117,17 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float,
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Exception during API call or data storage: {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)}")
|
raise HTTPException(status_code=500, detail=f"Error fetching or storing weather data: {str(e)}")
|
||||||
|
|
||||||
if daily_weather_data is None:
|
if daily_weather_data is None:
|
||||||
raise HTTPException(status_code=404, detail="No weather data found")
|
raise HTTPException(status_code=404, detail="No weather data found")
|
||||||
|
|
||||||
return daily_weather_data
|
return daily_weather_data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
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')}")
|
debug(f"Starting store_weather_to_db for datetime: {date_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||||
try:
|
try:
|
||||||
|
@ -272,19 +276,20 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
|
||||||
ORDER BY ST_Distance(DW.location, ST_MakePoint($4, $5)::geography) ASC
|
ORDER BY ST_Distance(DW.location, ST_MakePoint($4, $5)::geography) ASC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
'''
|
'''
|
||||||
|
|
||||||
daily_weather_records = await API.execute_read_query(daily_query, query_date, longitude, latitude, longitude, latitude, table_name='dailyweather')
|
daily_weather_records = await API.execute_read_query(daily_query, query_date, longitude, latitude, longitude, latitude, table_name='dailyweather')
|
||||||
|
|
||||||
if not daily_weather_records:
|
if not daily_weather_records:
|
||||||
debug(f"No daily weather data retrieved from database.")
|
debug(f"No daily weather data retrieved from database.")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
daily_weather_data = daily_weather_records[0] # Get the first (and only) record
|
daily_weather_data = daily_weather_records[0] # Get the first (and only) record
|
||||||
|
|
||||||
# Query to get hourly weather data
|
# Query to get hourly weather data
|
||||||
hourly_query = '''
|
hourly_query = '''
|
||||||
SELECT HW.* FROM hourlyweather HW
|
SELECT HW.* FROM hourlyweather HW
|
||||||
WHERE HW.daily_weather_id = $1
|
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')
|
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,
|
'HourlyWeather': hourly_weather_records,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug(f"Retrieved weather data for {date_time.date()}")
|
||||||
return day
|
return day
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Unexpected error occurred in get_weather_from_db: {e}")
|
err(f"Unexpected error occurred in get_weather_from_db: {e}")
|
||||||
err(f"Traceback: {traceback.format_exc()}")
|
err(f"Traceback: {traceback.format_exc()}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue