Auto-update: Tue Aug 6 22:36:26 PDT 2024
This commit is contained in:
parent
98e9eef70a
commit
3ac5f56323
2 changed files with 29 additions and 30 deletions
|
@ -849,22 +849,26 @@ class APIConfig(BaseModel):
|
||||||
if table_name in self.SPECIAL_TABLES:
|
if table_name in self.SPECIAL_TABLES:
|
||||||
result = await self._execute_special_table_write(conn, query, *args, table_name=table_name)
|
result = await self._execute_special_table_write(conn, query, *args, table_name=table_name)
|
||||||
else:
|
else:
|
||||||
# Modify the query to be an UPSERT operation
|
# Get table columns
|
||||||
if query.strip().upper().startswith("INSERT"):
|
columns = await self.get_table_columns(conn, table_name)
|
||||||
table_columns = await self.get_table_columns(conn, table_name)
|
|
||||||
primary_key = await self.get_primary_key(conn, table_name)
|
# Prepare the INSERT ... ON CONFLICT ... query
|
||||||
|
placeholders = [f'${i+1}' for i in range(len(args))]
|
||||||
if primary_key and len(table_columns) == len(args):
|
insert_cols = ', '.join(columns[1:]) # Exclude 'id' column
|
||||||
set_clause = ", ".join([f"{col} = EXCLUDED.{col}" for col in table_columns if col != primary_key])
|
insert_vals = ', '.join(placeholders)
|
||||||
query = f"""
|
update_cols = ', '.join([f"{col} = EXCLUDED.{col}" for col in columns[1:] if col not in ['id', 'version', 'server_id']])
|
||||||
INSERT INTO {table_name} ({', '.join(table_columns)})
|
|
||||||
VALUES ({', '.join(f'${i+1}' for i in range(len(args)))})
|
query = f"""
|
||||||
ON CONFLICT ({primary_key}) DO UPDATE SET
|
INSERT INTO {table_name} ({insert_cols})
|
||||||
{set_clause}
|
VALUES ({insert_vals})
|
||||||
"""
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
else:
|
{update_cols},
|
||||||
err(f"Column count mismatch for table {table_name}. Columns: {len(table_columns)}, Values: {len(args)}")
|
version = {table_name}.version + 1,
|
||||||
return []
|
server_id = EXCLUDED.server_id
|
||||||
|
WHERE {table_name}.version < EXCLUDED.version
|
||||||
|
OR ({table_name}.version = EXCLUDED.version AND {table_name}.server_id < EXCLUDED.server_id)
|
||||||
|
RETURNING id, version
|
||||||
|
"""
|
||||||
|
|
||||||
result = await conn.fetch(query, *args)
|
result = await conn.fetch(query, *args)
|
||||||
|
|
||||||
|
@ -888,6 +892,7 @@ class APIConfig(BaseModel):
|
||||||
"""
|
"""
|
||||||
columns = await conn.fetch(query, table_name)
|
columns = await conn.fetch(query, table_name)
|
||||||
return [col['column_name'] for col in columns]
|
return [col['column_name'] for col in columns]
|
||||||
|
|
||||||
|
|
||||||
async def get_primary_key(self, conn, table_name: str) -> str:
|
async def get_primary_key(self, conn, table_name: str) -> str:
|
||||||
query = """
|
query = """
|
||||||
|
|
|
@ -168,8 +168,8 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||||
day_data['sunset'] = await gis.dt(day_data.get('sunsetEpoch'))
|
day_data['sunset'] = await gis.dt(day_data.get('sunsetEpoch'))
|
||||||
debug(f"Corrected datetimes: datetime={day_data['datetime']}, sunrise={day_data['sunrise']}, sunset={day_data['sunset']}")
|
debug(f"Corrected datetimes: datetime={day_data['datetime']}, sunrise={day_data['sunrise']}, sunset={day_data['sunset']}")
|
||||||
|
|
||||||
|
|
||||||
daily_weather_params = [
|
daily_weather_params = [
|
||||||
|
location_point,
|
||||||
day_data.get('sunrise'), day_data.get('sunriseEpoch'),
|
day_data.get('sunrise'), day_data.get('sunriseEpoch'),
|
||||||
day_data.get('sunset'), day_data.get('sunsetEpoch'),
|
day_data.get('sunset'), day_data.get('sunsetEpoch'),
|
||||||
day_data.get('description'), day_data.get('tempmax'),
|
day_data.get('description'), day_data.get('tempmax'),
|
||||||
|
@ -188,10 +188,12 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||||
day_data.get('solarradiation'), day_data.get('solarenergy'),
|
day_data.get('solarradiation'), day_data.get('solarenergy'),
|
||||||
day_data.get('severerisk', 0), day_data.get('moonphase'),
|
day_data.get('severerisk', 0), day_data.get('moonphase'),
|
||||||
day_data.get('conditions'), stations_array, day_data.get('source'),
|
day_data.get('conditions'), stations_array, day_data.get('source'),
|
||||||
location_point
|
os.environ.get('TS_ID') # server_id
|
||||||
]
|
]
|
||||||
|
|
||||||
daily_weather_query = 'INSERT INTO dailyweather DEFAULT VALUES'
|
debug(f"Prepared daily_weather_params: {daily_weather_params}")
|
||||||
|
|
||||||
|
daily_weather_query = 'INSERT INTO dailyweather DEFAULT VALUES'
|
||||||
daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")
|
daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")
|
||||||
|
|
||||||
if not daily_weather_result:
|
if not daily_weather_result:
|
||||||
|
@ -234,18 +236,10 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
|
||||||
hour_data['icon'],
|
hour_data['icon'],
|
||||||
hour_stations_array,
|
hour_stations_array,
|
||||||
hour_data.get('source', ''),
|
hour_data.get('source', ''),
|
||||||
|
os.environ.get('TS_ID') # server_id
|
||||||
]
|
]
|
||||||
|
|
||||||
hourly_weather_query = '''
|
hourly_weather_query = 'INSERT INTO hourlyweather DEFAULT VALUES'
|
||||||
INSERT INTO hourlyweather (
|
|
||||||
daily_weather_id, datetime, datetimeepoch, temp, feelslike,
|
|
||||||
humidity, dew, precip, precipprob, preciptype, snow, snowdepth,
|
|
||||||
windgust, windspeed, winddir, pressure, cloudcover, visibility,
|
|
||||||
solarradiation, solarenergy, uvindex, severerisk, conditions,
|
|
||||||
icon, stations, source
|
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,
|
|
||||||
$16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26)
|
|
||||||
'''
|
|
||||||
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_datetime}")
|
debug(f"Inserted hourly weather data for {hour_datetime}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in a new issue