Spaces:
Running
Running
prince1604 commited on
Commit ·
357e12c
1
Parent(s): da8ed9b
Enhance progress/result endpoints with query param support and code cleanup
Browse files
api.py
CHANGED
|
@@ -22,16 +22,6 @@ logger = logging.getLogger("API")
|
|
| 22 |
|
| 23 |
app = FastAPI(title="Antigravity SEO Scaler API")
|
| 24 |
|
| 25 |
-
app.add_middleware(
|
| 26 |
-
CORS,
|
| 27 |
-
allow_origins=["*"],
|
| 28 |
-
allow_credentials=True,
|
| 29 |
-
allow_methods=["*"],
|
| 30 |
-
allow_headers=["*"],
|
| 31 |
-
from fastapi.staticfiles import StaticFiles
|
| 32 |
-
|
| 33 |
-
# ...
|
| 34 |
-
|
| 35 |
app.add_middleware(
|
| 36 |
CORS,
|
| 37 |
allow_origins=["*"],
|
|
@@ -221,26 +211,26 @@ def start_scan(payload: StartReq, bg: BackgroundTasks):
|
|
| 221 |
return {"job_id": job_id}
|
| 222 |
|
| 223 |
@app.get("/api/progress/{job_id}")
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
job = JOBS.get(job_id)
|
| 226 |
if not job:
|
| 227 |
-
return JSONResponse(status_code=404, content={"status": "not_found", "error": "Job ID not found"})
|
| 228 |
|
| 229 |
# Calculate Live Stats
|
| 230 |
current_time = time.time()
|
| 231 |
elapsed = int(current_time - job.get("start_time", current_time))
|
| 232 |
|
| 233 |
# Simple ETA Calculation
|
| 234 |
-
# If we have scanned X pages in Y seconds, and need to scan Z total...
|
| 235 |
-
# ETA = (target_pages - scanned_pages) * (elapsed / scanned_pages)
|
| 236 |
eta_seconds = None
|
| 237 |
pages_scanned = job.get("pages_scanned", 0)
|
| 238 |
percent = job.get("percent", 0)
|
| 239 |
|
| 240 |
if job["status"] == "running" and pages_scanned > 0 and percent > 0:
|
| 241 |
avg_per_page = elapsed / pages_scanned
|
| 242 |
-
# Assume limit is roughly the target (or use percent to back-calculate)
|
| 243 |
-
# Using percent is safer if limit wasn't reached
|
| 244 |
estimated_total = (pages_scanned / percent) * 100
|
| 245 |
remaining = estimated_total - pages_scanned
|
| 246 |
eta_seconds = int(remaining * avg_per_page)
|
|
@@ -257,10 +247,14 @@ def scan_progress(job_id: str):
|
|
| 257 |
}
|
| 258 |
|
| 259 |
@app.get("/api/result/{job_id}")
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
job = JOBS.get(job_id)
|
| 262 |
if not job:
|
| 263 |
-
return JSONResponse(status_code=404, content={"status": "not_found", "error": "Job ID not found"})
|
| 264 |
|
| 265 |
if job["status"] != "done":
|
| 266 |
return {"status": job["status"], "message": "Result not ready yet"}
|
|
|
|
| 22 |
|
| 23 |
app = FastAPI(title="Antigravity SEO Scaler API")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
app.add_middleware(
|
| 26 |
CORS,
|
| 27 |
allow_origins=["*"],
|
|
|
|
| 211 |
return {"job_id": job_id}
|
| 212 |
|
| 213 |
@app.get("/api/progress/{job_id}")
|
| 214 |
+
@app.get("/api/progress")
|
| 215 |
+
def scan_progress(job_id: Optional[str] = None):
|
| 216 |
+
if not job_id:
|
| 217 |
+
return JSONResponse(status_code=400, content={"error": "job_id is required"})
|
| 218 |
+
|
| 219 |
job = JOBS.get(job_id)
|
| 220 |
if not job:
|
| 221 |
+
return JSONResponse(status_code=404, content={"status": "not_found", "error": f"Job ID {job_id} not found"})
|
| 222 |
|
| 223 |
# Calculate Live Stats
|
| 224 |
current_time = time.time()
|
| 225 |
elapsed = int(current_time - job.get("start_time", current_time))
|
| 226 |
|
| 227 |
# Simple ETA Calculation
|
|
|
|
|
|
|
| 228 |
eta_seconds = None
|
| 229 |
pages_scanned = job.get("pages_scanned", 0)
|
| 230 |
percent = job.get("percent", 0)
|
| 231 |
|
| 232 |
if job["status"] == "running" and pages_scanned > 0 and percent > 0:
|
| 233 |
avg_per_page = elapsed / pages_scanned
|
|
|
|
|
|
|
| 234 |
estimated_total = (pages_scanned / percent) * 100
|
| 235 |
remaining = estimated_total - pages_scanned
|
| 236 |
eta_seconds = int(remaining * avg_per_page)
|
|
|
|
| 247 |
}
|
| 248 |
|
| 249 |
@app.get("/api/result/{job_id}")
|
| 250 |
+
@app.get("/api/result")
|
| 251 |
+
def scan_result(job_id: Optional[str] = None):
|
| 252 |
+
if not job_id:
|
| 253 |
+
return JSONResponse(status_code=400, content={"error": "job_id is required"})
|
| 254 |
+
|
| 255 |
job = JOBS.get(job_id)
|
| 256 |
if not job:
|
| 257 |
+
return JSONResponse(status_code=404, content={"status": "not_found", "error": f"Job ID {job_id} not found"})
|
| 258 |
|
| 259 |
if job["status"] != "done":
|
| 260 |
return {"status": job["status"], "message": "Result not ready yet"}
|