diff --git a/webui/index.html b/webui/index.html index 709c9b9e..6cee0db2 100644 --- a/webui/index.html +++ b/webui/index.html @@ -916,6 +916,7 @@ server

+
diff --git a/webui/static/stats-automations.js b/webui/static/stats-automations.js index 62be137b..7e52a709 100644 --- a/webui/static/stats-automations.js +++ b/webui/static/stats-automations.js @@ -381,6 +381,13 @@ function importFileSubmit() { let mirroredPlaylistsLoaded = false; const mirroredPipelinePollers = {}; +const AUTO_SYNC_BUCKETS = [1, 2, 4, 8, 12, 16, 24, 48, 72, 168]; +let _autoSyncScheduleState = { + playlists: [], + automations: [], + playlistSchedules: {}, + automationManaged: [], +}; /** * Fire-and-forget helper: send parsed playlist data to be mirrored on the backend. @@ -570,6 +577,312 @@ function getMirroredSourceRef(p) { return (p && p.source_playlist_id) ? String(p.source_playlist_id) : ''; } +function autoSyncTriggerForHours(hours) { + const h = parseInt(hours, 10) || 24; + if (h >= 24 && h % 24 === 0) { + return { interval: h / 24, unit: 'days' }; + } + return { interval: h, unit: 'hours' }; +} + +function autoSyncHoursFromTrigger(config) { + const interval = parseInt(config?.interval, 10) || 0; + const unit = config?.unit || 'hours'; + if (!interval) return null; + if (unit === 'minutes') return Math.max(1, Math.round(interval / 60)); + if (unit === 'days') return interval * 24; + if (unit === 'weeks') return interval * 168; + return interval; +} + +function autoSyncBucketLabel(hours) { + if (hours === 168) return 'Weekly'; + if (hours >= 24) return `${hours / 24}d`; + return `${hours}h`; +} + +function autoSyncSourceLabel(source) { + const labels = { + spotify: 'Spotify', + spotify_public: 'Spotify Link', + tidal: 'Tidal', + youtube: 'YouTube', + deezer: 'Deezer', + qobuz: 'Qobuz', + beatport: 'Beatport', + file: 'File Imports', + }; + return labels[source] || source || 'Other'; +} + +function autoSyncIsPipelineAutomation(auto) { + return auto && auto.action_type === 'playlist_pipeline'; +} + +function autoSyncPlaylistIdFromAutomation(auto) { + if (!autoSyncIsPipelineAutomation(auto)) return null; + const cfg = auto.action_config || {}; + if (cfg.all === true || cfg.all === 'true') return null; + const raw = cfg.playlist_id; + if (raw === undefined || raw === null || raw === '') return null; + const id = parseInt(raw, 10); + return Number.isFinite(id) ? id : null; +} + +function autoSyncIsScheduleOwned(auto) { + const group = auto?.group_name || ''; + const name = auto?.name || ''; + return group === 'Playlist Auto-Sync' || name.startsWith('Auto-Sync:'); +} + +function buildAutoSyncScheduleState(playlists, automations) { + const playlistSchedules = {}; + const automationManaged = []; + automations.filter(autoSyncIsPipelineAutomation).forEach(auto => { + const playlistId = autoSyncPlaylistIdFromAutomation(auto); + const hours = auto.trigger_type === 'schedule' ? autoSyncHoursFromTrigger(auto.trigger_config || {}) : null; + if (playlistId && hours) { + playlistSchedules[playlistId] = { + automation_id: auto.id, + automation_name: auto.name, + hours, + enabled: auto.enabled !== false && auto.enabled !== 0, + owned: autoSyncIsScheduleOwned(auto), + next_run: auto.next_run, + trigger_config: auto.trigger_config || {}, + }; + } else { + automationManaged.push(auto); + } + }); + return { playlists, automations, playlistSchedules, automationManaged }; +} + +async function openAutoSyncScheduleModal() { + let overlay = document.getElementById('auto-sync-schedule-modal'); + if (!overlay) { + overlay = document.createElement('div'); + overlay.id = 'auto-sync-schedule-modal'; + overlay.className = 'auto-sync-overlay'; + document.body.appendChild(overlay); + } + overlay.innerHTML = ` +
+
+
+

Auto-Sync Schedule

+

Drop mirrored playlists onto an interval to schedule refresh, discovery, sync, and wishlist processing.

+
+ +
+
Loading schedule...
+
+ `; + overlay.style.display = 'flex'; + overlay.addEventListener('click', e => { if (e.target === overlay) closeAutoSyncScheduleModal(); }, { once: true }); + await refreshAutoSyncScheduleModal(); +} + +function closeAutoSyncScheduleModal() { + const overlay = document.getElementById('auto-sync-schedule-modal'); + if (overlay) overlay.remove(); +} + +async function refreshAutoSyncScheduleModal() { + const overlay = document.getElementById('auto-sync-schedule-modal'); + if (!overlay) return; + try { + const [playlistRes, automationRes] = await Promise.all([ + fetch('/api/mirrored-playlists'), + fetch('/api/automations'), + ]); + const playlists = await playlistRes.json(); + const automations = await automationRes.json(); + if (!playlistRes.ok || playlists.error) throw new Error(playlists.error || 'Failed to load mirrored playlists'); + if (!automationRes.ok || automations.error) throw new Error(automations.error || 'Failed to load automations'); + _autoSyncScheduleState = buildAutoSyncScheduleState(playlists, automations); + renderAutoSyncScheduleModal(); + } catch (err) { + overlay.innerHTML = ` +
+
+

Auto-Sync Schedule

Could not load schedule data.

+ +
+
${_esc(err.message)}
+
+ `; + } +} + +function renderAutoSyncScheduleModal() { + const overlay = document.getElementById('auto-sync-schedule-modal'); + if (!overlay) return; + + const { playlists, playlistSchedules, automationManaged } = _autoSyncScheduleState; + const grouped = playlists.reduce((acc, p) => { + const key = p.source || 'other'; + if (!acc[key]) acc[key] = []; + acc[key].push(p); + return acc; + }, {}); + const sourceKeys = Object.keys(grouped).sort((a, b) => autoSyncSourceLabel(a).localeCompare(autoSyncSourceLabel(b))); + + const sidebarHtml = sourceKeys.length ? sourceKeys.map(source => ` +
+
${_esc(autoSyncSourceLabel(source))}
+ ${grouped[source].map(p => { + const schedule = playlistSchedules[p.id]; + const assigned = schedule ? autoSyncBucketLabel(schedule.hours) : 'Unscheduled'; + return ` +
+
${_esc(p.name)}
+
${p.track_count || 0} tracks · ${_esc(assigned)}
+
+ `; + }).join('')} +
+ `).join('') : '
No mirrored playlists yet.
'; + + const bucketHtml = AUTO_SYNC_BUCKETS.map(hours => { + const assigned = playlists.filter(p => playlistSchedules[p.id]?.hours === hours); + return ` +
+
+ ${autoSyncBucketLabel(hours)} + ${assigned.length} +
+
+ ${assigned.length ? assigned.map(p => autoSyncScheduledCardHtml(p, playlistSchedules[p.id])).join('') : '
Drop playlists here
'} +
+
+ `; + }).join(''); + + const managedHtml = automationManaged.length ? ` +
+
Automation-managed pipelines
+ ${automationManaged.map(a => `${_esc(a.name || 'Playlist Pipeline')}`).join('')} +
+ ` : ''; + + overlay.innerHTML = ` +
+
+
+

Auto-Sync Schedule

+

Drag mirrored playlists into an interval. Each placement creates or updates a matching playlist-pipeline automation.

+
+ +
+ ${managedHtml} +
+ +
${bucketHtml}
+
+
+ `; +} + +function autoSyncScheduledCardHtml(playlist, schedule) { + const enabled = schedule?.enabled !== false; + const owned = schedule?.owned === true; + return ` +
+
+
${_esc(playlist.name)}
+
${_esc(autoSyncSourceLabel(playlist.source))} · ${playlist.track_count || 0} tracks${schedule?.next_run ? ` · ${autoSyncNextRunLabel(schedule.next_run)}` : ''}${owned ? '' : ' · Automations page'}
+
+ ${owned + ? `` + : 'Lock'} +
+ `; +} + +function autoSyncNextRunLabel(nextRun) { + if (!nextRun) return ''; + const ts = new Date(nextRun).getTime(); + if (!Number.isFinite(ts)) return ''; + const diff = ts - Date.now(); + if (diff <= 0) return 'due now'; + const mins = Math.ceil(diff / 60000); + if (mins < 60) return `next in ${mins}m`; + const hours = Math.ceil(mins / 60); + if (hours < 24) return `next in ${hours}h`; + return `next in ${Math.ceil(hours / 24)}d`; +} + +function autoSyncDragStart(event) { + const playlistId = event.currentTarget?.dataset?.playlistId; + if (!playlistId) return; + event.dataTransfer.setData('text/plain', playlistId); + event.dataTransfer.effectAllowed = 'move'; +} + +function autoSyncDragOver(event) { + event.preventDefault(); + event.dataTransfer.dropEffect = 'move'; +} + +async function autoSyncDrop(event, hours) { + event.preventDefault(); + const playlistId = parseInt(event.dataTransfer.getData('text/plain'), 10); + if (!playlistId) return; + await saveAutoSyncPlaylistSchedule(playlistId, hours); +} + +async function saveAutoSyncPlaylistSchedule(playlistId, hours) { + const playlist = _autoSyncScheduleState.playlists.find(p => parseInt(p.id, 10) === parseInt(playlistId, 10)); + if (!playlist) return; + const existing = _autoSyncScheduleState.playlistSchedules[playlistId]; + if (existing && !existing.owned) { + showToast('This playlist pipeline is managed from the Automations page for now.', 'info'); + return; + } + const payload = { + name: `Auto-Sync: ${playlist.name}`, + trigger_type: 'schedule', + trigger_config: autoSyncTriggerForHours(hours), + action_type: 'playlist_pipeline', + action_config: { playlist_id: String(playlistId), all: false }, + then_actions: [], + group_name: 'Playlist Auto-Sync', + }; + try { + const res = await fetch(existing ? `/api/automations/${existing.automation_id}` : '/api/automations', { + method: existing ? 'PUT' : 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload), + }); + const data = await res.json(); + if (!res.ok || data.error) throw new Error(data.error || 'Failed to save Auto-Sync schedule'); + showToast(`${playlist.name} scheduled every ${autoSyncBucketLabel(hours)}`, 'success'); + await refreshAutoSyncScheduleModal(); + } catch (err) { + showToast(`Error: ${err.message}`, 'error'); + } +} + +async function unscheduleAutoSyncPlaylist(playlistId) { + const schedule = _autoSyncScheduleState.playlistSchedules[playlistId]; + const playlist = _autoSyncScheduleState.playlists.find(p => parseInt(p.id, 10) === parseInt(playlistId, 10)); + if (!schedule) return; + if (!await showConfirmDialog({ title: 'Remove Auto-Sync', message: `Remove Auto-Sync schedule for "${playlist?.name || 'this playlist'}"?` })) return; + try { + const res = await fetch(`/api/automations/${schedule.automation_id}`, { method: 'DELETE' }); + const data = await res.json(); + if (!res.ok || data.error) throw new Error(data.error || 'Failed to remove Auto-Sync schedule'); + showToast('Auto-Sync schedule removed', 'success'); + await refreshAutoSyncScheduleModal(); + } catch (err) { + showToast(`Error: ${err.message}`, 'error'); + } +} + async function parseMirroredPipelineResponse(res, fallbackMessage) { const text = await res.text(); let data = {}; diff --git a/webui/static/style.css b/webui/static/style.css index 0fa9218b..7e77f94f 100644 --- a/webui/static/style.css +++ b/webui/static/style.css @@ -11178,6 +11178,278 @@ body.helper-mode-active #dashboard-activity-feed:hover { } .sync-history-btn:hover { color: rgb(var(--accent-rgb)); background: rgba(var(--accent-rgb), 0.1); border-color: rgba(var(--accent-rgb), 0.3); } +.auto-sync-manager-btn { + color: #7dd3fc; + border-color: rgba(56, 189, 248, 0.28); + background: rgba(56, 189, 248, 0.1); +} + +.auto-sync-overlay { + position: fixed; + inset: 0; + z-index: 10000; + display: none; + align-items: center; + justify-content: center; + background: rgba(0, 0, 0, 0.72); + backdrop-filter: blur(12px); +} + +.auto-sync-modal { + width: min(1420px, calc(100vw - 48px)); + height: min(820px, calc(100vh - 48px)); + background: rgba(18, 20, 28, 0.98); + border: 1px solid rgba(255, 255, 255, 0.12); + border-radius: 8px; + box-shadow: 0 28px 80px rgba(0, 0, 0, 0.5); + display: flex; + flex-direction: column; + overflow: hidden; +} + +.auto-sync-header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 20px; + padding: 22px 24px 18px; + border-bottom: 1px solid rgba(255, 255, 255, 0.08); +} + +.auto-sync-header h3 { + margin: 0 0 6px; + color: rgba(255, 255, 255, 0.92); + font-size: 21px; + font-weight: 700; +} + +.auto-sync-header p { + margin: 0; + color: rgba(255, 255, 255, 0.48); + font-size: 13px; +} + +.auto-sync-close { + width: 32px; + height: 32px; + border: 0; + border-radius: 6px; + background: rgba(255, 255, 255, 0.06); + color: rgba(255, 255, 255, 0.65); + cursor: pointer; + font-size: 22px; + line-height: 1; +} + +.auto-sync-close:hover { + background: rgba(255, 255, 255, 0.12); + color: #fff; +} + +.auto-sync-body { + min-height: 0; + flex: 1; + display: grid; + grid-template-columns: 300px 1fr; +} + +.auto-sync-sidebar { + min-height: 0; + border-right: 1px solid rgba(255, 255, 255, 0.08); + background: rgba(255, 255, 255, 0.025); + display: flex; + flex-direction: column; +} + +.auto-sync-sidebar-title { + padding: 16px 18px 12px; + color: rgba(255, 255, 255, 0.72); + font-size: 12px; + font-weight: 700; + text-transform: uppercase; +} + +.auto-sync-source-list { + min-height: 0; + overflow-y: auto; + padding: 0 12px 16px; +} + +.auto-sync-source-group { + margin-bottom: 16px; +} + +.auto-sync-source-title { + padding: 8px 6px; + color: rgba(255, 255, 255, 0.42); + font-size: 12px; + font-weight: 700; +} + +.auto-sync-playlist, +.auto-sync-scheduled-card { + border: 1px solid rgba(255, 255, 255, 0.08); + border-radius: 7px; + background: rgba(255, 255, 255, 0.045); + cursor: grab; +} + +.auto-sync-playlist { + padding: 10px; + margin-bottom: 8px; +} + +.auto-sync-playlist:hover, +.auto-sync-scheduled-card:hover { + border-color: rgba(56, 189, 248, 0.32); + background: rgba(56, 189, 248, 0.08); +} + +.auto-sync-playlist.scheduled { + border-color: rgba(34, 197, 94, 0.22); +} + +.auto-sync-playlist-name, +.auto-sync-scheduled-name { + color: rgba(255, 255, 255, 0.86); + font-size: 13px; + font-weight: 700; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.auto-sync-playlist-meta, +.auto-sync-scheduled-meta { + margin-top: 4px; + color: rgba(255, 255, 255, 0.42); + font-size: 11px; +} + +.auto-sync-board { + min-width: 0; + overflow-x: auto; + padding: 18px; + display: grid; + grid-template-columns: repeat(10, minmax(170px, 1fr)); + gap: 12px; +} + +.auto-sync-column { + min-height: 0; + border: 1px solid rgba(255, 255, 255, 0.08); + border-radius: 8px; + background: rgba(255, 255, 255, 0.025); + display: flex; + flex-direction: column; +} + +.auto-sync-column-head { + display: flex; + align-items: center; + justify-content: space-between; + padding: 12px; + border-bottom: 1px solid rgba(255, 255, 255, 0.07); + color: rgba(255, 255, 255, 0.86); + font-weight: 800; +} + +.auto-sync-column-head small { + color: #7dd3fc; + font-size: 11px; + font-weight: 700; +} + +.auto-sync-column-list { + flex: 1; + min-height: 220px; + padding: 10px; +} + +.auto-sync-drop-hint, +.auto-sync-empty, +.auto-sync-loading, +.auto-sync-error { + color: rgba(255, 255, 255, 0.38); + font-size: 13px; + text-align: center; +} + +.auto-sync-drop-hint { + border: 1px dashed rgba(255, 255, 255, 0.12); + border-radius: 7px; + padding: 18px 10px; +} + +.auto-sync-loading, +.auto-sync-error { + padding: 48px; +} + +.auto-sync-scheduled-card { + padding: 10px; + margin-bottom: 10px; + display: flex; + justify-content: space-between; + gap: 8px; +} + +.auto-sync-scheduled-card.disabled { + opacity: 0.52; +} + +.auto-sync-scheduled-card button { + width: 24px; + height: 24px; + border: 0; + border-radius: 5px; + background: rgba(255, 255, 255, 0.06); + color: rgba(255, 255, 255, 0.5); + cursor: pointer; + flex-shrink: 0; +} + +.auto-sync-scheduled-card button:hover { + color: #ef4444; + background: rgba(239, 68, 68, 0.12); +} + +.auto-sync-lock { + align-self: flex-start; + padding: 4px 6px; + border-radius: 5px; + background: rgba(255, 255, 255, 0.06); + color: rgba(255, 255, 255, 0.38); + font-size: 10px; + font-weight: 700; + text-transform: uppercase; + flex-shrink: 0; +} + +.auto-sync-managed { + display: flex; + align-items: center; + gap: 8px; + padding: 10px 24px; + border-bottom: 1px solid rgba(255, 255, 255, 0.07); + color: rgba(255, 255, 255, 0.48); + font-size: 12px; + overflow-x: auto; +} + +.auto-sync-managed-title { + color: rgba(255, 255, 255, 0.72); + font-weight: 700; + flex-shrink: 0; +} + +.auto-sync-managed span { + padding: 4px 8px; + border-radius: 999px; + background: rgba(255, 255, 255, 0.06); + white-space: nowrap; +} + /* Enhanced Progress Bar Animation */ .progress-bar-fill { height: 100%;