From c4179b3b0890410852afa77cd86fa1a9495d7d2c Mon Sep 17 00:00:00 2001 From: ziesorx Date: Sun, 10 Aug 2025 17:55:02 +0700 Subject: [PATCH] Done feature 3 fully with postgresql integration --- siwatsystem/database.py | 31 +++++++++++++++++++++++++------ siwatsystem/pympta.py | 4 ++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/siwatsystem/database.py b/siwatsystem/database.py index 58b4e0e..6340986 100644 --- a/siwatsystem/database.py +++ b/siwatsystem/database.py @@ -130,7 +130,7 @@ class DatabaseManager: # Create table if it doesn't exist create_table_query = """ CREATE TABLE IF NOT EXISTS gas_station_1.car_frontal_info ( - camera_id VARCHAR(255), + display_id VARCHAR(255), captured_timestamp VARCHAR(255), session_id VARCHAR(255) PRIMARY KEY, license_character VARCHAR(255) DEFAULT NULL, @@ -138,15 +138,34 @@ class DatabaseManager: car_brand VARCHAR(255) DEFAULT NULL, car_model VARCHAR(255) DEFAULT NULL, car_body_type VARCHAR(255) DEFAULT NULL, - created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ) """ cur.execute(create_table_query) + + # Add columns if they don't exist (for existing tables) + alter_queries = [ + "ALTER TABLE gas_station_1.car_frontal_info ADD COLUMN IF NOT EXISTS car_brand VARCHAR(255) DEFAULT NULL", + "ALTER TABLE gas_station_1.car_frontal_info ADD COLUMN IF NOT EXISTS car_model VARCHAR(255) DEFAULT NULL", + "ALTER TABLE gas_station_1.car_frontal_info ADD COLUMN IF NOT EXISTS car_body_type VARCHAR(255) DEFAULT NULL", + "ALTER TABLE gas_station_1.car_frontal_info ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP DEFAULT NOW()" + ] + + for alter_query in alter_queries: + try: + cur.execute(alter_query) + logger.debug(f"Executed: {alter_query}") + except Exception as e: + # Ignore errors if column already exists (for older PostgreSQL versions) + if "already exists" in str(e).lower(): + logger.debug(f"Column already exists, skipping: {alter_query}") + else: + logger.warning(f"Error in ALTER TABLE: {e}") + self.connection.commit() cur.close() - logger.info("Successfully created/verified car_frontal_info table in gas_station_1 schema") + logger.info("Successfully created/verified car_frontal_info table with all required columns") return True except Exception as e: @@ -155,7 +174,7 @@ class DatabaseManager: self.connection.rollback() return False - def insert_initial_detection(self, camera_id: str, captured_timestamp: str, session_id: str = None) -> str: + def insert_initial_detection(self, display_id: str, captured_timestamp: str, session_id: str = None) -> str: """Insert initial detection record and return the session_id.""" if not self.is_connected(): if not self.connect(): @@ -174,12 +193,12 @@ class DatabaseManager: cur = self.connection.cursor() insert_query = """ INSERT INTO gas_station_1.car_frontal_info - (camera_id, captured_timestamp, session_id, license_character, license_type, car_brand, car_model, car_body_type) + (display_id, captured_timestamp, session_id, license_character, license_type, car_brand, car_model, car_body_type) VALUES (%s, %s, %s, NULL, 'No model available', NULL, NULL, NULL) ON CONFLICT (session_id) DO NOTHING """ - cur.execute(insert_query, (camera_id, captured_timestamp, session_id)) + cur.execute(insert_query, (display_id, captured_timestamp, session_id)) self.connection.commit() cur.close() logger.info(f"Inserted initial detection record with session_id: {session_id}") diff --git a/siwatsystem/pympta.py b/siwatsystem/pympta.py index bbbd43a..d21232d 100644 --- a/siwatsystem/pympta.py +++ b/siwatsystem/pympta.py @@ -666,11 +666,11 @@ def run_pipeline(frame, node: dict, return_bbox: bool=False, context=None): generated_session_id = str(uuid_lib.uuid4()) # Insert initial detection record - camera_id = detection_result.get("camera_id", "unknown") + display_id = detection_result.get("display_id", "unknown") timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S") inserted_session_id = node["db_manager"].insert_initial_detection( - camera_id=camera_id, + display_id=display_id, captured_timestamp=timestamp, session_id=generated_session_id )