Spaces:
Runtime error
Runtime error
File size: 6,059 Bytes
0ad3564 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | let eventSource = null;
async function syncSource(el) {
const sourceId = Number(el.dataset.id);
const displayName = el.dataset.displayName;
const initUrl = el.getAttribute('data-sync-url') || el.dataset.syncUrl;
const modal = document.getElementById('progressModal');
const title = document.getElementById('progressTitle');
const status = document.getElementById('progressStatus');
const spinner = document.getElementById('statusSpinner');
const log = document.getElementById('progressLog');
const closeBtn = document.getElementById('progressCloseBtn');
// UI Reset
title.innerText = `Syncing: ${displayName}`;
status.innerText = 'Initializing connection...';
status.classList.add('animate-pulse');
spinner.classList.remove('hidden');
log.innerHTML = '<div>[INFO] Contacting sync service...</div>';
closeBtn.disabled = true;
modal.classList.remove('hidden');
modal.classList.add('flex');
try {
const response = await fetch(initUrl, {
method: 'POST',
headers: {
'X-CSRFToken': document.querySelector('[name="csrfmiddlewaretoken"]')?.value,
'Content-Type': 'application/json',
}
});
const initData = await response.json();
if (!response.ok || !initData.success) {
throw new Error(initData.message || 'Failed to initialize sync.');
}
const { operation_id } = initData;
appendLog('INFO', 'Background worker spawned successfully.');
const sseUrl = `/rag/sources/${sourceId}/progress/${operation_id}/`;
eventSource = new EventSource(sseUrl);
eventSource.onmessage = function(event) {
const rawData = event.data?.trim();
if (!rawData) return;
let currentStatus = rawData;
let logMessage = rawData;
try {
// 1. Try parsing structured JSON if backend sends it
const progressData = JSON.parse(rawData);
currentStatus = progressData.message || progressData.status || 'Syncing...';
logMessage = progressData.message || '';
} catch (e) {
// 2. Fallback: Raw text stream line becomes the direct update message
currentStatus = rawData;
}
// 3. Update the text banner directly with the message received
status.innerText = currentStatus;
// Log output to tracking box panels (ignoring raw json leftovers)
if (logMessage && !logMessage.startsWith('{')) {
appendLog('SYNC', logMessage);
}
// 4. Handle completed/success status indicators cleanly
const lowerStatus = currentStatus.toLowerCase();
// CRITICAL FIX: Only finish if it's 100%, or contains explicit final success phrases
const isTrulyFinished =
lowerStatus.includes('100% complete') ||
lowerStatus.includes('sync completed successfully') ||
lowerStatus.includes('sync finished') ||
lowerStatus === 'completed' ||
lowerStatus === 'success';
if (isTrulyFinished) {
status.classList.remove('animate-pulse');
spinner.classList.add('hidden'); // Stop spinning
handleSyncSuccess();
// Clean up and close the event stream since we are officially done
if (eventSource) {
eventSource.close();
}
}
};
eventSource.onerror = function(err) {
console.error("SSE Error:", err);
appendLog('WARN', 'Stream connection adjusted or finalized.');
status.classList.remove('animate-pulse');
spinner.classList.add('hidden');
handleSyncFinishedUI();
};
} catch (error) {
status.innerText = 'Initialization Failed';
status.classList.remove('animate-pulse');
spinner.classList.add('hidden');
appendLog('ERROR', error.message);
closeBtn.disabled = false;
}
}
function appendLog(level, text) {
const log = document.getElementById('progressLog');
const time = new Date().toLocaleTimeString();
log.innerHTML += `<div class="mb-1"><span class="text-gray-400">[${time}]</span> <span class="font-bold">[${level}]</span> ${text}</div>`;
log.scrollTop = log.scrollHeight; // Auto-scrolls tracking log to bottom
}
function handleSyncSuccess() {
appendLog('SUCCESS', 'Source synchronization complete!');
document.getElementById('progressStatus').innerText = 'Completed!';
handleSyncFinishedUI();
}
function handleSyncFinishedUI() {
if (eventSource) {
eventSource.close();
eventSource = null;
}
const closeBtn = document.getElementById('progressCloseBtn');
closeBtn.disabled = false;
closeBtn.classList.remove('bg-gray-700');
closeBtn.classList.add('bg-green-600', 'hover:bg-green-700');
closeBtn.innerText = "Refresh Dashboard";
}
function hideProgressModal() {
const modal = document.getElementById('progressModal');
modal.classList.add('hidden');
modal.classList.remove('flex');
// On success close, refresh the page to show new sync indicators/times
if (!document.getElementById('progressCloseBtn').disabled) {
window.location.reload();
}
}
// Quick helper to read CSRF tokens if needed outside forms
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
} |