Bladeren bron

Fix python virtual env paths

lanfr144 3 weken geleden
bovenliggende
commit
00f1d63625
3 gewijzigde bestanden met toevoegingen van 56 en 16 verwijderingen
  1. 16 14
      ingest_csv.py
  2. 38 0
      reset_pwd.py
  3. 2 2
      start_batch_ingest.sh

+ 16 - 14
ingest_csv.py

@@ -1,7 +1,7 @@
 import pandas as pd
 import myloginpath
 import urllib.parse
-from sqlalchemy import create_engine
+from sqlalchemy import create_engine, text
 import os
 import sys
 
@@ -38,8 +38,16 @@ def ingest_file(filename, engine):
             if 'code' in chunk.columns:
                 df = chunk.drop_duplicates(subset=['code'])
             else:
-                df = chunk
-                
+            # Only keep the minimum columns required by our clinical analytical schema!
+            target_cols = [
+                'code', 'product_name', 'generic_name', 'brands', 'allergens', 'ingredients_text',
+                'proteins_100g', 'fat_100g', 'carbohydrates_100g', 'sugars_100g', 'sodium_100g', 'energy-kcal_100g',
+                'vitamin-c_100g', 'iron_100g', 'calcium_100g'
+            ]
+            # Use intersection in case some CSV chunks lack certain columns
+            exist_cols = [c for c in target_cols if c in df.columns]
+            df = df[exist_cols]
+            
             df.to_sql('products', con=engine, if_exists='append', index=False)
             total_processed += len(df)
             print(f"   Successfully appended {total_processed} rows (Dynamic schema)...", end="\r")
@@ -58,24 +66,18 @@ def create_indexes(engine):
     try:
         with engine.begin() as connection:
             print("  Building Primary Key on `code`...")
-            # We must make `code` the primary key if pandas just made it a TEXT field
-            # But MySQL cannot have a TEXT field as PRIMARY KEY without a length constraint.
-            # Convert code to VARCHAR(50) first.
-            connection.execute(urllib.parse.unquote("ALTER TABLE products MODIFY code VARCHAR(50);"))
-            connection.execute(urllib.parse.unquote("ALTER TABLE products ADD PRIMARY KEY (code);"))
+            connection.execute(text("ALTER TABLE products MODIFY code VARCHAR(50);"))
+            connection.execute(text("ALTER TABLE products ADD PRIMARY KEY (code);"))
 
             print("  Building Fulltext Indexes...")
-            connection.execute(urllib.parse.unquote("CREATE FULLTEXT INDEX ft_idx_search ON products(product_name, ingredients_text, brands);"))
+            connection.execute(text("CREATE FULLTEXT INDEX ft_idx_search ON products(product_name, ingredients_text, brands);"))
             
             print("  Building B-TREE Indexes on core macros...")
-            # We attempt to index key macros if they exist
             macro_cols = ['energy-kcal_100g', 'fat_100g', 'carbohydrates_100g', 'proteins_100g', 'sugars_100g', 'sodium_100g', 'iron_100g', 'calcium_100g', 'vitamin-c_100g']
             for col in macro_cols:
-                # Convert TEXT to DOUBLE for numerical indexing and querying
-                # We catch errors if the column doesn't exist to be safe
                 try:
-                    connection.execute(urllib.parse.unquote(f"ALTER TABLE products MODIFY `{col}` DOUBLE;"))
-                    connection.execute(urllib.parse.unquote(f"CREATE INDEX idx_{col.replace('-', '_')} ON products(`{col}`);"))
+                    connection.execute(text(f"ALTER TABLE products MODIFY `{col}` DOUBLE;"))
+                    connection.execute(text(f"CREATE INDEX idx_{col.replace('-', '_')} ON products(`{col}`);"))
                 except:
                     pass
         print("✅ Indexing Complete!")

+ 38 - 0
reset_pwd.py

@@ -0,0 +1,38 @@
+import bcrypt
+import pymysql
+import sys
+
+def get_db_connection():
+    return pymysql.connect(
+        host='127.0.0.1',
+        user='db_app_auth',
+        password='BTSai123_app_auth',
+        database='food_db',
+        cursorclass=pymysql.cursors.DictCursor,
+        autocommit=True
+    )
+
+def reset_pwd(username, plain_password):
+    conn = get_db_connection()
+    if not conn:
+        print("Failed DB connection!")
+        sys.exit(1)
+        
+    hashed = bcrypt.hashpw(plain_password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
+    with conn.cursor() as cursor:
+        rows = cursor.execute("UPDATE users SET password_hash = %s WHERE username = %s", (hashed, username))
+        if rows > 0:
+            print(f"✅ Successfully updated password for {username}!")
+        else:
+            print(f"❌ User '{username}' not found in database!")
+    conn.close()
+
+if __name__ == "__main__":
+    if len(sys.argv) < 3:
+        username = input("Enter Username: ")
+        plain_password = input("Enter New Password: ")
+    else:
+        username = sys.argv[1]
+        plain_password = sys.argv[2]
+        
+    reset_pwd(username, plain_password)

+ 2 - 2
start_batch_ingest.sh

@@ -15,13 +15,13 @@ fi
 
 echo "🚀 Starting database wipe and reset..."
 # Automatically run the new DB setup to drop the rigid table
-python3 setup_db.py
+venv/bin/python3 setup_db.py
 
 echo "🚀 Triggering background ingestion process via nohup..."
 echo "All outputs will be saved to ingestion_process.log"
 
 # Run securely in background
-nohup python3 -u ingest_csv.py > ingestion_process.log 2>&1 &
+nohup venv/bin/python3 -u ingest_csv.py > ingestion_process.log 2>&1 &
 BG_PID=$!
 
 echo "✅ Process started in the background (PID: $BG_PID)"