Spaces:
Runtime error
Runtime error
Download theme/static/js/rag-sync.js from junaidte14/django-admin-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 6.06 kB
-
https://huggingface.co/spaces/junaidte14/django-admin-dashboard/resolve/main/theme/static/js/rag-sync.js
- Command line
-
hf download hf://spaces/junaidte14/django-admin-dashboard/theme/static/js/rag-sync.js
-
curl -L -o rag-sync.js https://huggingface.co/spaces/junaidte14/django-admin-dashboard/resolve/main/theme/static/js/rag-sync.js
6.06 kB
| 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; | |
| } |