Jelajahi Sumber

TG-86: implement AI dietary enforcement rules and zabbix telemetry

lanfr144 1 Minggu lalu
induk
melakukan
4c5bf89970
2 mengubah file dengan 55 tambahan dan 5 penghapusan
  1. 21 5
      app.py
  2. 34 0
      zabbix_telemetry.py

+ 21 - 5
app.py

@@ -45,19 +45,34 @@ search_tool_schema = {
     },
 }
 
-def search_nutrition_db(query: str) -> str:
+def search_nutrition_db(query: str, user_eav=None) -> str:
     conn = get_db_connection('app_reader')
     if not conn: return "Database connection failed."
     try:
         with conn.cursor() as cursor:
-            # Query products view via natural language match on core table
-            sql = """
+            # Dynamically build strictly-enforced clinical SQL filters
+            clinical_filters = ""
+            if user_eav:
+                for p in user_eav:
+                    name = p['name'].lower()
+                    val = p['value'].lower()
+                    if name in ['condition', 'illness']:
+                        if val == 'diabetes': clinical_filters += " AND m.sugars_100g < 5.0"
+                        elif 'kidney' in val: clinical_filters += " AND m.proteins_100g < 15.0"
+                        elif 'hypertension' in val: clinical_filters += " AND m.sodium_100g < 0.2"
+                    elif name in ['diet', 'religious', 'preference']:
+                        if val == 'kosher': clinical_filters += " AND c.ingredients_text NOT LIKE '%pork%' AND c.ingredients_text NOT LIKE '%shellfish%'"
+                        elif val == 'halal': clinical_filters += " AND c.ingredients_text NOT LIKE '%pork%' AND c.ingredients_text NOT LIKE '%wine%' AND c.ingredients_text NOT LIKE '%alcohol%'"
+                        elif val in ['christian', 'good friday', 'ash wednesday']: clinical_filters += " AND c.ingredients_text NOT LIKE '%meat%' AND c.ingredients_text NOT LIKE '%beef%' AND c.ingredients_text NOT LIKE '%chicken%' AND c.ingredients_text NOT LIKE '%pork%'"
+
+            sql = f"""
                 SELECT c.code, c.product_name, m.proteins_100g, m.fat_100g, m.carbohydrates_100g, m.sugars_100g 
                 FROM food_db.products_core c
                 LEFT JOIN food_db.products_macros m ON c.code = m.code
                 WHERE MATCH(c.product_name, c.ingredients_text) AGAINST(%s IN BOOLEAN MODE)
                 AND c.product_name IS NOT NULL AND c.product_name != '' AND c.product_name != 'None'
-                LIMIT 5
+                {clinical_filters}
+                LIMIT 15
             """
             bool_query = " ".join([f"+{w}" for w in query.split()])
             cursor.execute(sql, (bool_query,))
@@ -754,7 +769,8 @@ with tab_planner:
             profile_text = ", ".join([f"{p['name']}: {p['value']}" for p in user_eav]) if user_eav else "None"
             
             # Pre-fetch database context directly without using AI tools!
-            db_context = search_nutrition_db(diet_pref)
+            # Enforce the strict clinical constraints directly via SQL
+            db_context = search_nutrition_db(diet_pref, user_eav)
             
             sys_prompt = f"""You are a professional clinical Dietitian planner. Target: {target_cal}kcal over {meal_count} meals. 
             Dietary constraint: {diet_pref}. Additional notes: {extra_notes}.

+ 34 - 0
zabbix_telemetry.py

@@ -0,0 +1,34 @@
+import os
+import time
+from app import get_db_connection
+from snmp_notifier import notifier
+
+def report_telemetry():
+    conn = get_db_connection('app_reader')
+    if not conn:
+        print("Failed to connect to database for telemetry.")
+        return
+    
+    try:
+        with conn.cursor() as cursor:
+            # Get products count
+            cursor.execute("SELECT COUNT(*) as cnt FROM food_db.products_core")
+            products_count = cursor.fetchone()['cnt']
+            
+            # Get users count
+            cursor.execute("SELECT COUNT(*) as cnt FROM food_db.users")
+            users_count = cursor.fetchone()['cnt']
+            
+            msg = f"TELEMETRY: products_core_count={products_count}, users_count={users_count}"
+            print(f"Sending to Zabbix: {msg}")
+            
+            # Push via SNMP Trap (which is hooked into Zabbix server on 192.168.130.170)
+            notifier.send_alert(msg)
+            
+    except Exception as e:
+        print(f"Telemetry error: {e}")
+    finally:
+        conn.close()
+
+if __name__ == "__main__":
+    report_telemetry()