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")
daily_weather_id = daily_weather_result[0]['id']
debug(f"Inserted daily weather data with id: {daily_weather_id}")
# Hourly weather insertion
if 'hours' in day_data:
debug(f"Processing {len(day_data['hours'])} hourly records")
for hour_data in day_data['hours']:
try:
hour_preciptype_array = hour_data.get('preciptype', []) or []
hour_stations_array = hour_data.get('stations', []) or []
hourly_weather_params = [
daily_weather_id,
await gis.dt(hour_data.get('datetimeEpoch')),
hour_data.get('datetimeEpoch'),
hour_data['temp'],
hour_data['feelslike'],
hour_data['humidity'],
hour_data['dew'],
hour_data['precip'],
hour_data['precipprob'],
hour_data.get('temp'),
hour_data.get('feelslike'),
hour_data.get('humidity'),
hour_data.get('dew'),
hour_data.get('precip'),
hour_data.get('precipprob'),
hour_preciptype_array,
hour_data['snow'],
hour_data['snowdepth'],
hour_data['windgust'],
hour_data['windspeed'],
hour_data['winddir'],
hour_data['pressure'],
hour_data['cloudcover'],
hour_data['visibility'],
hour_data['solarradiation'],
hour_data['solarenergy'],
hour_data['uvindex'],
hour_data.get('snow'),
hour_data.get('snowdepth'),
hour_data.get('windgust'),
hour_data.get('windspeed'),
hour_data.get('winddir'),
hour_data.get('pressure'),
hour_data.get('cloudcover'),
hour_data.get('visibility'),
hour_data.get('solarradiation'),
hour_data.get('solarenergy'),
hour_data.get('uvindex'),
hour_data.get('severerisk', 0),
hour_data['conditions'],
hour_data['icon'],
hour_data.get('conditions'),
hour_data.get('icon'),
hour_stations_array,
hour_data.get('source', '')
]
@ -268,15 +267,12 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
) RETURNING id
'''
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')}")
except Exception as e:
err(f"Error processing hourly data: {e}")
err(f"Problematic hour_data: {hour_data}")
raise
if not hourly_result:
warn(f"Failed to insert hourly weather data for {hour_data.get('datetimeEpoch')}")
else:
debug(f"Inserted hourly weather data with id: {hourly_result[0]['id']}")
debug("Successfully stored weather data")
return "SUCCESS"
except Exception as e:
err(f"Error in weather storage: {e}")
err(f"Traceback: {traceback.format_exc()}")
@ -289,10 +285,10 @@ async def get_weather_from_db(date_time: dt_datetime, latitude: float, longitude
try:
# Query to get daily weather data
daily_query = '''
SELECT DW.* FROM dailyweather DW
WHERE DW.datetime::date = $1
AND ST_DWithin(DW.location::geography, ST_MakePoint($2,$3)::geography, 8046.72)
ORDER BY ST_Distance(DW.location, ST_MakePoint($4, $5)::geography) ASC
SELECT * FROM dailyweather
WHERE DATE(datetime) = $1
AND ST_DWithin(location::geography, ST_MakePoint($2,$3)::geography, 8046.72)
ORDER BY ST_Distance(location, ST_MakePoint($4, $5)::geography) ASC
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.")
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
hourly_query = '''
SELECT HW.* FROM hourlyweather HW
WHERE HW.daily_weather_id = $1
ORDER BY HW.datetime ASC
SELECT * FROM hourlyweather
WHERE daily_weather_id = $1
ORDER BY datetime ASC
'''
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"Traceback: {traceback.format_exc()}")
return None