Auto-update: Tue Aug 6 23:23:51 PDT 2024

This commit is contained in:
sanj 2024-08-06 23:23:51 -07:00
parent 0d99ad1f33
commit 3c02ac65b9

View file

@ -217,40 +217,39 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
raise ValueError("Failed to insert daily weather data: no result returned") raise ValueError("Failed to insert daily weather data: no result returned")
daily_weather_id = daily_weather_result[0]['id'] daily_weather_id = daily_weather_result[0]['id']
debug(f"Inserted daily weather data with id: {daily_weather_id}") debug(f"Inserted daily weather data with id: {daily_weather_id}")
# Hourly weather insertion
if 'hours' in day_data: if 'hours' in day_data:
debug(f"Processing {len(day_data['hours'])} hourly records") debug(f"Processing {len(day_data['hours'])} hourly records")
for hour_data in day_data['hours']: for hour_data in day_data['hours']:
try:
hour_preciptype_array = hour_data.get('preciptype', []) or [] hour_preciptype_array = hour_data.get('preciptype', []) or []
hour_stations_array = hour_data.get('stations', []) or [] hour_stations_array = hour_data.get('stations', []) or []
hourly_weather_params = [ hourly_weather_params = [
daily_weather_id, daily_weather_id,
await gis.dt(hour_data.get('datetimeEpoch')), await gis.dt(hour_data.get('datetimeEpoch')),
hour_data.get('datetimeEpoch'), hour_data.get('datetimeEpoch'),
hour_data['temp'], hour_data.get('temp'),
hour_data['feelslike'], hour_data.get('feelslike'),
hour_data['humidity'], hour_data.get('humidity'),
hour_data['dew'], hour_data.get('dew'),
hour_data['precip'], hour_data.get('precip'),
hour_data['precipprob'], hour_data.get('precipprob'),
hour_preciptype_array, hour_preciptype_array,
hour_data['snow'], hour_data.get('snow'),
hour_data['snowdepth'], hour_data.get('snowdepth'),
hour_data['windgust'], hour_data.get('windgust'),
hour_data['windspeed'], hour_data.get('windspeed'),
hour_data['winddir'], hour_data.get('winddir'),
hour_data['pressure'], hour_data.get('pressure'),
hour_data['cloudcover'], hour_data.get('cloudcover'),
hour_data['visibility'], hour_data.get('visibility'),
hour_data['solarradiation'], hour_data.get('solarradiation'),
hour_data['solarenergy'], hour_data.get('solarenergy'),
hour_data['uvindex'], hour_data.get('uvindex'),
hour_data.get('severerisk', 0), hour_data.get('severerisk', 0),
hour_data['conditions'], hour_data.get('conditions'),
hour_data['icon'], hour_data.get('icon'),
hour_stations_array, hour_stations_array,
hour_data.get('source', '') hour_data.get('source', '')
] ]
@ -268,31 +267,28 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
) RETURNING id ) RETURNING id
''' '''
hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather") hourly_result = await API.execute_write_query(hourly_weather_query, *hourly_weather_params, table_name="hourlyweather")
debug(f"Inserted hourly weather data for {hour_data.get('datetimeEpoch')}") if not hourly_result:
except Exception as e: warn(f"Failed to insert hourly weather data for {hour_data.get('datetimeEpoch')}")
err(f"Error processing hourly data: {e}") else:
err(f"Problematic hour_data: {hour_data}") debug(f"Inserted hourly weather data with id: {hourly_result[0]['id']}")
raise
debug("Successfully stored weather data")
return "SUCCESS" return "SUCCESS"
except Exception as e: except Exception as e:
err(f"Error in weather storage: {e}") err(f"Error in weather storage: {e}")
err(f"Traceback: {traceback.format_exc()}") err(f"Traceback: {traceback.format_exc()}")
return "FAILURE" return "FAILURE"
async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude: float): async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude: float):
debug(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in get_weather_from_db.") debug(f"Using {date_time.strftime('%Y-%m-%d %H:%M:%S')} as our datetime in get_weather_from_db.")
query_date = date_time.date() query_date = date_time.date()
try: try:
# Query to get daily weather data # Query to get daily weather data
daily_query = ''' daily_query = '''
SELECT DW.* FROM dailyweather DW SELECT * FROM dailyweather
WHERE DW.datetime::date = $1 WHERE DATE(datetime) = $1
AND ST_DWithin(DW.location::geography, ST_MakePoint($2,$3)::geography, 8046.72) AND ST_DWithin(location::geography, ST_MakePoint($2,$3)::geography, 8046.72)
ORDER BY ST_Distance(DW.location, ST_MakePoint($4, $5)::geography) ASC ORDER BY ST_Distance(location, ST_MakePoint($4, $5)::geography) ASC
LIMIT 1 LIMIT 1
''' '''
@ -302,13 +298,13 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
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]
# Query to get hourly weather data # Query to get hourly weather data
hourly_query = ''' hourly_query = '''
SELECT HW.* FROM hourlyweather HW SELECT * FROM hourlyweather
WHERE HW.daily_weather_id = $1 WHERE daily_weather_id = $1
ORDER BY HW.datetime ASC ORDER BY 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')
@ -324,4 +320,3 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
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