Nhughes09 commited on
Commit ·
325a6f9
1
Parent(s): 387721d
Fix frontend flashing loop and handle 429s
Browse files
frontend/src/components/ProductSection.jsx
CHANGED
|
@@ -18,39 +18,48 @@ const ProductSection = ({ vertical, id }) => {
|
|
| 18 |
setDebugLogs(prev => [`[${new Date().toLocaleTimeString()}] ${msg}`, ...prev].slice(0, 50));
|
| 19 |
};
|
| 20 |
|
| 21 |
-
|
| 22 |
setLoading(true);
|
| 23 |
const apiUrl = import.meta.env.VITE_API_URL || '';
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
pollStatus();
|
| 41 |
}
|
| 42 |
-
|
| 43 |
setData(previewData);
|
| 44 |
setFiles(filesData.files || []);
|
| 45 |
setPrediction(predictionData);
|
| 46 |
setLoading(false);
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}, [vertical]);
|
| 55 |
|
| 56 |
const pollStatus = async () => {
|
|
@@ -64,26 +73,29 @@ const ProductSection = ({ vertical, id }) => {
|
|
| 64 |
const dateHeader = res.headers.get('date');
|
| 65 |
const serverHeader = res.headers.get('server');
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if (res.status === 404) {
|
| 68 |
addDebugLog(`Status: 404 Not Found`);
|
| 69 |
addDebugLog(`Server: ${serverHeader} | Time: ${dateHeader}`);
|
| 70 |
|
| 71 |
// CHECK IF IT'S THE OLD BACKEND
|
| 72 |
-
// If /api/status is 404, check /api/version
|
| 73 |
try {
|
| 74 |
-
// Add cache buster to prevent Cloudflare from serving stale 404s
|
| 75 |
const verRes = await fetch(`${apiUrl}/api/version?t=${Date.now()}`);
|
| 76 |
if (verRes.ok) {
|
| 77 |
const verData = await verRes.json();
|
| 78 |
addDebugLog(`✓ NEW BACKEND DETECTED: ${verData.version}`);
|
| 79 |
addDebugLog("Status endpoint should be available momentarily...");
|
| 80 |
} else {
|
| 81 |
-
// If version check fails, check catalog (old endpoint)
|
| 82 |
const catRes = await fetch(`${apiUrl}/api/catalog?t=${Date.now()}`);
|
| 83 |
if (catRes.ok) {
|
| 84 |
addDebugLog("⚠️ DIAGNOSIS: OLD BACKEND DETECTED (v1.0)");
|
| 85 |
addDebugLog("The server is online but running old code.");
|
| 86 |
-
addDebugLog("Waiting for v2.1 update to apply...");
|
| 87 |
} else {
|
| 88 |
addDebugLog("Diagnosis: Server might be completely down.");
|
| 89 |
}
|
|
@@ -106,8 +118,9 @@ const ProductSection = ({ vertical, id }) => {
|
|
| 106 |
setTimeout(pollStatus, 1000);
|
| 107 |
return;
|
| 108 |
} else {
|
| 109 |
-
addDebugLog("System Ready.
|
| 110 |
-
|
|
|
|
| 111 |
return;
|
| 112 |
}
|
| 113 |
} else {
|
|
|
|
| 18 |
setDebugLogs(prev => [`[${new Date().toLocaleTimeString()}] ${msg}`, ...prev].slice(0, 50));
|
| 19 |
};
|
| 20 |
|
| 21 |
+
const fetchData = async () => {
|
| 22 |
setLoading(true);
|
| 23 |
const apiUrl = import.meta.env.VITE_API_URL || '';
|
| 24 |
|
| 25 |
+
try {
|
| 26 |
+
// Fetch Data Preview, Files, AND ML Prediction
|
| 27 |
+
const [previewData, filesData, predictionData] = await Promise.all([
|
| 28 |
+
fetch(`${apiUrl}/api/preview/${vertical}`).then(res => res.json()),
|
| 29 |
+
fetch(`${apiUrl}/api/files/${vertical}`).then(res => res.json()),
|
| 30 |
+
fetch(`${apiUrl}/api/predict/${vertical}`).then(res => res.json())
|
| 31 |
+
]);
|
| 32 |
+
|
| 33 |
+
console.log("Preview Data:", previewData);
|
| 34 |
+
console.log("Files Data:", filesData);
|
| 35 |
+
console.log("Prediction Data:", predictionData);
|
| 36 |
+
|
| 37 |
+
if (previewData.error || predictionData.error || predictionData.detail) {
|
| 38 |
+
const errorMsg = previewData.error || predictionData.error || predictionData.detail;
|
| 39 |
+
console.error("API Error:", errorMsg);
|
| 40 |
+
addDebugLog(`API Error: ${errorMsg}`);
|
| 41 |
+
|
| 42 |
+
// If error is due to loading, start polling
|
| 43 |
+
if (predictionData.error === "ML Engine Loading" || errorMsg === "Not Found") {
|
| 44 |
pollStatus();
|
| 45 |
}
|
| 46 |
+
} else {
|
| 47 |
setData(previewData);
|
| 48 |
setFiles(filesData.files || []);
|
| 49 |
setPrediction(predictionData);
|
| 50 |
setLoading(false);
|
| 51 |
+
}
|
| 52 |
+
} catch (err) {
|
| 53 |
+
console.error("Fetch Error:", err);
|
| 54 |
+
addDebugLog(`Fetch Error: ${err.message}`);
|
| 55 |
+
// Only stop loading if we are NOT going to poll
|
| 56 |
+
// But here we want to poll if fetch failed
|
| 57 |
+
pollStatus();
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
useEffect(() => {
|
| 62 |
+
fetchData();
|
| 63 |
}, [vertical]);
|
| 64 |
|
| 65 |
const pollStatus = async () => {
|
|
|
|
| 73 |
const dateHeader = res.headers.get('date');
|
| 74 |
const serverHeader = res.headers.get('server');
|
| 75 |
|
| 76 |
+
if (res.status === 429) {
|
| 77 |
+
addDebugLog("⚠️ Status: 429 Too Many Requests (Rate Limited)");
|
| 78 |
+
addDebugLog("Waiting 15s before retry...");
|
| 79 |
+
setTimeout(pollStatus, 15000); // Backoff for 15s
|
| 80 |
+
return;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
if (res.status === 404) {
|
| 84 |
addDebugLog(`Status: 404 Not Found`);
|
| 85 |
addDebugLog(`Server: ${serverHeader} | Time: ${dateHeader}`);
|
| 86 |
|
| 87 |
// CHECK IF IT'S THE OLD BACKEND
|
|
|
|
| 88 |
try {
|
|
|
|
| 89 |
const verRes = await fetch(`${apiUrl}/api/version?t=${Date.now()}`);
|
| 90 |
if (verRes.ok) {
|
| 91 |
const verData = await verRes.json();
|
| 92 |
addDebugLog(`✓ NEW BACKEND DETECTED: ${verData.version}`);
|
| 93 |
addDebugLog("Status endpoint should be available momentarily...");
|
| 94 |
} else {
|
|
|
|
| 95 |
const catRes = await fetch(`${apiUrl}/api/catalog?t=${Date.now()}`);
|
| 96 |
if (catRes.ok) {
|
| 97 |
addDebugLog("⚠️ DIAGNOSIS: OLD BACKEND DETECTED (v1.0)");
|
| 98 |
addDebugLog("The server is online but running old code.");
|
|
|
|
| 99 |
} else {
|
| 100 |
addDebugLog("Diagnosis: Server might be completely down.");
|
| 101 |
}
|
|
|
|
| 118 |
setTimeout(pollStatus, 1000);
|
| 119 |
return;
|
| 120 |
} else {
|
| 121 |
+
addDebugLog("System Ready. Fetching Data...");
|
| 122 |
+
// STOP RELOADING, JUST FETCH DATA
|
| 123 |
+
fetchData();
|
| 124 |
return;
|
| 125 |
}
|
| 126 |
} else {
|