Auto-update: Tue Aug 6 23:20:17 PDT 2024

This commit is contained in:
sanj 2024-08-06 23:20:17 -07:00
parent 210059e186
commit 0d99ad1f33
2 changed files with 16 additions and 23 deletions

View file

@ -833,34 +833,30 @@ class APIConfig(BaseModel):
async def execute_write_query(self, query: str, *args, table_name: str): async def execute_write_query(self, query: str, *args, table_name: str):
local_ts_id = os.environ.get('TS_ID')
conn = await self.get_connection(self.local_db) conn = await self.get_connection(self.local_db)
if conn is None: if conn is None:
err(f"Unable to connect to local database. Write operation failed.") err(f"Unable to connect to local database. Write operation failed.")
return [] return []
try: try:
# Remove any formatting from the query # Execute the original query
query = ' '.join(query.split()) result = await conn.fetch(query, *args)
# Add version and server_id to the query
columns = query.split('(')[1].split(')')[0]
values = query.split('VALUES')[1].split('RETURNING')[0]
modified_query = f""" if result:
INSERT INTO {table_name} ({columns}, version, server_id) # Update version and server_id
VALUES {values[:-1]}, (SELECT COALESCE(MAX(version), 0) + 1 FROM {table_name}), '{local_ts_id}') update_query = f"""
ON CONFLICT (id) DO UPDATE SET UPDATE {table_name}
version = {table_name}.version + 1, SET version = COALESCE(version, 0) + 1,
server_id = EXCLUDED.server_id server_id = $1
RETURNING id, version WHERE id = $2
""" RETURNING id, version
"""
result = await conn.fetch(modified_query, *args) update_result = await conn.fetch(update_query, os.environ.get('TS_ID'), result[0]['id'])
return update_result
return result return result
except Exception as e: except Exception as e:
err(f"Error executing write query: {str(e)}") err(f"Error executing write query: {str(e)}")
err(f"Query: {modified_query}") err(f"Query: {query}")
err(f"Args: {args}") err(f"Args: {args}")
err(f"Traceback: {traceback.format_exc()}") err(f"Traceback: {traceback.format_exc()}")
return [] return []
@ -868,7 +864,6 @@ class APIConfig(BaseModel):
await conn.close() await conn.close()
async def get_table_columns(self, conn, table_name: str) -> List[str]: async def get_table_columns(self, conn, table_name: str) -> List[str]:
query = """ query = """

View file

@ -139,11 +139,9 @@ 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): 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: try:
day_data = weather_data.get('days', [{}])[0] day_data = weather_data.get('days', [{}])[0]
debug(f"RAW DAY_DATA: {day_data}")
preciptype_array = day_data.get('preciptype', []) or [] preciptype_array = day_data.get('preciptype', []) or []
stations_array = day_data.get('stations', []) or [] stations_array = day_data.get('stations', []) or []
@ -212,7 +210,7 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
$29, $30, $31, $32, $33, $34, $35, $36, $37, $38 $29, $30, $31, $32, $33, $34, $35, $36, $37, $38
) RETURNING id ) RETURNING id
''' '''
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: