Spaces:
Running
Running
Commit Β·
ca51ce4
1
Parent(s): 70f3ca7
fix: Err. 500 connexion supabase
Browse files- .gitignore +3 -1
- app/api/v1/investigate.py +15 -6
- app/services/data_loader.py +18 -14
.gitignore
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
venv/
|
| 2 |
-
__pycache__/
|
|
|
|
|
|
|
| 3 |
# Ignorer tout le dossier des scripts sensibles
|
| 4 |
app/scripts/
|
| 5 |
force_admin.py
|
|
|
|
| 1 |
venv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
.vscode/
|
| 4 |
+
pyrightconfig.json
|
| 5 |
# Ignorer tout le dossier des scripts sensibles
|
| 6 |
app/scripts/
|
| 7 |
force_admin.py
|
app/api/v1/investigate.py
CHANGED
|
@@ -13,6 +13,7 @@ from typing import Optional, List
|
|
| 13 |
from datetime import datetime
|
| 14 |
from sqlalchemy.orm import Session
|
| 15 |
|
|
|
|
| 16 |
from app.core.supabase_client import get_user_client
|
| 17 |
from app.services.llm_service import llm_service
|
| 18 |
from app.services.data_loader import get_companies_df
|
|
@@ -93,12 +94,20 @@ async def investigate_company(
|
|
| 93 |
"""
|
| 94 |
logger.info(f"π Investigation request for company_id: {company_id}")
|
| 95 |
|
| 96 |
-
# ββ 1. Retrieve enriched data from Supabase βββββββββββββββββββββ
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
company_name = enriched.get("company_name", "")
|
| 103 |
wilaya = enriched.get("wilaya", "")
|
| 104 |
enrichment_data = enriched.get("data", {})
|
|
|
|
| 13 |
from datetime import datetime
|
| 14 |
from sqlalchemy.orm import Session
|
| 15 |
|
| 16 |
+
from app.database import engine
|
| 17 |
from app.core.supabase_client import get_user_client
|
| 18 |
from app.services.llm_service import llm_service
|
| 19 |
from app.services.data_loader import get_companies_df
|
|
|
|
| 94 |
"""
|
| 95 |
logger.info(f"π Investigation request for company_id: {company_id}")
|
| 96 |
|
| 97 |
+
# ββ 1. Retrieve enriched data from Supabase via SQL βββββββββββββββββββββ
|
| 98 |
+
enriched = {}
|
| 99 |
+
try:
|
| 100 |
+
with engine.connect() as conn:
|
| 101 |
+
from sqlalchemy import text
|
| 102 |
+
result = conn.execute(
|
| 103 |
+
text("SELECT * FROM enriched_companies WHERE company_id = :cid"),
|
| 104 |
+
{"cid": company_id}
|
| 105 |
+
)
|
| 106 |
+
row = result.mappings().first()
|
| 107 |
+
if row:
|
| 108 |
+
enriched = dict(row)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logger.error(f"Error fetching enriched data: {e}")
|
| 111 |
company_name = enriched.get("company_name", "")
|
| 112 |
wilaya = enriched.get("wilaya", "")
|
| 113 |
enrichment_data = enriched.get("data", {})
|
app/services/data_loader.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
from app.
|
| 7 |
from app.core.utils import clean_nans
|
| 8 |
|
| 9 |
# Load environment variables
|
|
@@ -73,18 +73,20 @@ class DataLoader:
|
|
| 73 |
if self._cached_df is not None and not force_refresh:
|
| 74 |
return self._cached_df
|
| 75 |
|
| 76 |
-
print("Fetching companies from Supabase view 'companies_unified'...")
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
if
|
| 83 |
print("Warning: No data returned from Supabase view.")
|
| 84 |
return pd.DataFrame()
|
| 85 |
|
| 86 |
# Clean NaNs and convert to DF
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
df = pd.DataFrame(data)
|
| 89 |
|
| 90 |
# Add 'id' column for backward compatibility (1-indexed)
|
|
@@ -114,13 +116,15 @@ class DataLoader:
|
|
| 114 |
Fetch a single company from Supabase by its normalized name.
|
| 115 |
"""
|
| 116 |
try:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
.
|
| 122 |
-
.
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error fetching specific company: {e}")
|
| 126 |
return None
|
|
|
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from app.database import engine
|
| 7 |
from app.core.utils import clean_nans
|
| 8 |
|
| 9 |
# Load environment variables
|
|
|
|
| 73 |
if self._cached_df is not None and not force_refresh:
|
| 74 |
return self._cached_df
|
| 75 |
|
| 76 |
+
print("Fetching companies from Supabase view 'companies_unified' via SQL...")
|
| 77 |
try:
|
| 78 |
+
# Fetch directly using SQLAlchemy engine
|
| 79 |
+
query = "SELECT * FROM companies_unified"
|
| 80 |
+
df = pd.read_sql(query, con=engine)
|
| 81 |
|
| 82 |
+
if df.empty:
|
| 83 |
print("Warning: No data returned from Supabase view.")
|
| 84 |
return pd.DataFrame()
|
| 85 |
|
| 86 |
# Clean NaNs and convert to DF
|
| 87 |
+
# Clean NaNs
|
| 88 |
+
# Note: Pandas read_sql already handles many things, but clean_nans ensures JSON safety for API
|
| 89 |
+
data = clean_nans(df.to_dict(orient='records'))
|
| 90 |
df = pd.DataFrame(data)
|
| 91 |
|
| 92 |
# Add 'id' column for backward compatibility (1-indexed)
|
|
|
|
| 116 |
Fetch a single company from Supabase by its normalized name.
|
| 117 |
"""
|
| 118 |
try:
|
| 119 |
+
query = "SELECT * FROM companies_unified WHERE name_normalized = %s"
|
| 120 |
+
# Use pandas read_sql with params or engine.connect()
|
| 121 |
+
with engine.connect() as conn:
|
| 122 |
+
from sqlalchemy import text
|
| 123 |
+
result = conn.execute(text("SELECT * FROM companies_unified WHERE name_normalized = :name"), {"name": company_name_normalized})
|
| 124 |
+
row = result.mappings().first()
|
| 125 |
+
if row:
|
| 126 |
+
return clean_nans(dict(row))
|
| 127 |
+
return None
|
| 128 |
except Exception as e:
|
| 129 |
print(f"Error fetching specific company: {e}")
|
| 130 |
return None
|