Spaces:
Running
Running
Seth
commited on
Commit
·
32b78fe
1
Parent(s):
80a736e
update
Browse files- backend/app/main.py +39 -8
backend/app/main.py
CHANGED
|
@@ -187,28 +187,59 @@ async def get_asset_status(asset_id: int, db: Session = Depends(get_db)):
|
|
| 187 |
if conn:
|
| 188 |
try:
|
| 189 |
cursor = conn.cursor()
|
|
|
|
| 190 |
cursor.execute("""
|
| 191 |
-
SELECT
|
| 192 |
-
FROM
|
| 193 |
-
WHERE
|
| 194 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
row = cursor.fetchone()
|
| 196 |
cursor.close()
|
| 197 |
conn.close()
|
|
|
|
| 198 |
if row:
|
| 199 |
-
|
| 200 |
"asset_id": row[0],
|
| 201 |
"name": row[1],
|
| 202 |
"status": row[2] or "pending",
|
| 203 |
"analyzed_at": row[3].isoformat() if row[3] else None,
|
| 204 |
-
"extracted_content": row[4] if len(row) > 4 else None # Include extracted JSON
|
| 205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
if conn:
|
| 208 |
conn.close()
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
except Exception as e:
|
|
|
|
| 212 |
raise HTTPException(status_code=500, detail=str(e))
|
| 213 |
|
| 214 |
async def analyze_asset_background(asset_id: int, file_path: str, file_type: str):
|
|
|
|
| 187 |
if conn:
|
| 188 |
try:
|
| 189 |
cursor = conn.cursor()
|
| 190 |
+
# First check if extracted_content column exists
|
| 191 |
cursor.execute("""
|
| 192 |
+
SELECT column_name
|
| 193 |
+
FROM information_schema.columns
|
| 194 |
+
WHERE table_name='assets' AND column_name='extracted_content'
|
| 195 |
+
""")
|
| 196 |
+
has_extracted_content = cursor.fetchone() is not None
|
| 197 |
+
|
| 198 |
+
# Build query based on column existence
|
| 199 |
+
if has_extracted_content:
|
| 200 |
+
cursor.execute("""
|
| 201 |
+
SELECT id, name, analysis_status, analyzed_at, extracted_content
|
| 202 |
+
FROM assets
|
| 203 |
+
WHERE id = %s
|
| 204 |
+
""", (asset_id,))
|
| 205 |
+
else:
|
| 206 |
+
cursor.execute("""
|
| 207 |
+
SELECT id, name, analysis_status, analyzed_at
|
| 208 |
+
FROM assets
|
| 209 |
+
WHERE id = %s
|
| 210 |
+
""", (asset_id,))
|
| 211 |
+
|
| 212 |
row = cursor.fetchone()
|
| 213 |
cursor.close()
|
| 214 |
conn.close()
|
| 215 |
+
|
| 216 |
if row:
|
| 217 |
+
result = {
|
| 218 |
"asset_id": row[0],
|
| 219 |
"name": row[1],
|
| 220 |
"status": row[2] or "pending",
|
| 221 |
"analyzed_at": row[3].isoformat() if row[3] else None,
|
|
|
|
| 222 |
}
|
| 223 |
+
# Add extracted_content only if column exists and value is present
|
| 224 |
+
if has_extracted_content and len(row) > 4:
|
| 225 |
+
result["extracted_content"] = row[4]
|
| 226 |
+
else:
|
| 227 |
+
result["extracted_content"] = None
|
| 228 |
+
return result
|
| 229 |
+
else:
|
| 230 |
+
raise HTTPException(status_code=404, detail="Asset not found")
|
| 231 |
+
except HTTPException:
|
| 232 |
+
raise
|
| 233 |
except Exception as e:
|
| 234 |
if conn:
|
| 235 |
conn.close()
|
| 236 |
+
print(f"Error in get_asset_status: {e}")
|
| 237 |
+
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
| 238 |
+
raise HTTPException(status_code=500, detail="Database connection failed")
|
| 239 |
+
except HTTPException:
|
| 240 |
+
raise
|
| 241 |
except Exception as e:
|
| 242 |
+
print(f"Error in get_asset_status (outer): {e}")
|
| 243 |
raise HTTPException(status_code=500, detail=str(e))
|
| 244 |
|
| 245 |
async def analyze_asset_background(asset_id: int, file_path: str, file_type: str):
|