Spaces:
Runtime error
Runtime error
File size: 6,976 Bytes
4088a7c | 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 163 164 | // ===== Data Upload & Schema =====
let selectedFile = null;
// Drag & Drop - initialize safely on DOMContentLoaded
document.addEventListener('DOMContentLoaded', () => {
const zone = document.getElementById('uploadZone');
const fileInput = document.getElementById('fileInput');
if (zone && fileInput) {
zone.addEventListener('click', (e) => {
// Don't trigger file picker if clicking inside the source selector
if (e.target.closest('.source-selector')) return;
fileInput.click();
});
zone.addEventListener('dragover', e => { e.preventDefault(); zone.classList.add('dragover'); });
zone.addEventListener('dragleave', () => zone.classList.remove('dragover'));
zone.addEventListener('drop', e => {
e.preventDefault();
zone.classList.remove('dragover');
if (e.dataTransfer.files.length > 0) selectFile(e.dataTransfer.files[0]);
});
fileInput.addEventListener('change', e => {
if (e.target.files.length > 0) selectFile(e.target.files[0]);
});
}
// Load schema (Data Dictionary)
loadSchema();
});
function selectFile(file) {
selectedFile = file;
const selector = document.getElementById('sourceSelector');
if (selector) selector.style.display = 'block';
const nameEl = document.getElementById('selectedFileName');
if (nameEl) nameEl.textContent = `${file.name} (${(file.size / 1024).toFixed(1)} KB)`;
}
function cancelUpload() {
selectedFile = null;
const selector = document.getElementById('sourceSelector');
if (selector) selector.style.display = 'none';
const vr = document.getElementById('validationResults');
if (vr) vr.classList.remove('visible');
const fi = document.getElementById('fileInput');
if (fi) fi.value = '';
}
function uploadFile() {
if (!selectedFile) return;
const sourceType = document.getElementById('sourceType').value;
if (!sourceType) { alert('Please select a data source type'); return; }
const btn = document.querySelector('.btn-upload');
if (btn) { btn.disabled = true; btn.textContent = 'Uploading...'; }
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('source_type', sourceType);
fetch('/data/api/data/upload-raw', { method: 'POST', body: formData })
.then(r => r.json())
.then(data => {
if (btn) { btn.disabled = false; btn.textContent = 'Upload to Raw'; }
if (data.error) { alert(data.error); return; }
showValidationResults(data);
// Refresh the pipeline status to show the new file
if (typeof refreshPipelineStatus === 'function') refreshPipelineStatus();
})
.catch(err => {
if (btn) { btn.disabled = false; btn.textContent = 'Upload to Raw'; }
console.error('Upload error:', err);
alert('Upload failed: ' + err.message);
});
}
function showValidationResults(data) {
const container = document.getElementById('validationResults');
const checks = document.getElementById('validationChecks');
if (!container || !checks) return;
container.classList.add('visible');
let html = '';
// Show uploaded file info
if (data.saved_as) {
html += `
<div class="validation-check">
<div class="check-icon pass">✓</div>
<span class="check-name">File Saved</span>
<span class="check-detail">Saved as <strong>${data.saved_as}</strong> in raw folder</span>
</div>`;
}
// Show validation checks
if (data.checks) {
html += data.checks.map(c => `
<div class="validation-check">
<div class="check-icon ${c.status}">${c.status === 'pass' ? '✓' : c.status === 'fail' ? '✗' : c.status === 'warning' ? '!' : 'i'}</div>
<span class="check-name">${c.name}</span>
<span class="check-detail">${c.detail}</span>
</div>
`).join('');
}
if (data.stats) {
html += `
<div style="margin-top: 16px; padding: 12px; background: var(--bg-secondary); border-radius: 8px; font-size: 12px; color: var(--text-secondary);">
<strong style="color: var(--text-primary);">Summary:</strong> ${data.stats.rows.toLocaleString()} rows, ${data.stats.columns} columns, ${data.stats.missing_pct}% missing
</div>`;
}
html += `
<div style="margin-top: 12px; font-size: 12px; color: var(--text-muted);">
💡 Use the <strong>Data Profiler</strong> and <strong>Auto Column Mapper</strong> agents above to process this file through the pipeline.
</div>`;
checks.innerHTML = html;
}
// ===== Data Schema / Data Dictionary =====
function loadSchema() {
const container = document.getElementById('schemaSections');
if (!container) return;
fetch('/data/api/data/schema')
.then(r => r.json())
.then(schema => {
const labels = {
media: 'Media Data', sales: 'Sales Data', consumer_promo: 'Consumer Promo Data',
trade_promo: 'Trade Promo Data', external_factors: 'External Factors Data'
};
const icons = {
media: '📺', sales: '💰', consumer_promo: '🎯',
trade_promo: '🏪', external_factors: '🌍'
};
container.innerHTML = Object.entries(schema).map(([key, s]) => `
<div class="schema-section" onclick="this.classList.toggle('open')">
<div class="schema-header">
<h4><span class="schema-icon">${icons[key] || ''}</span> ${labels[key] || key}</h4>
<span class="field-count">${s.required_count} required / ${s.total_count} total fields</span>
</div>
<div class="schema-body">
<div class="schema-field schema-field-header">
<span class="field-name">Column Name</span>
<span class="field-type">Data Type</span>
<span class="field-req">Status</span>
</div>
${s.fields.map(f => `
<div class="schema-field">
<span class="field-name">${f.name}</span>
<span class="field-type">${f.type}</span>
<span class="field-req ${f.required ? 'required' : 'optional'}">${f.required ? 'Required' : 'Optional'}</span>
</div>`).join('')}
</div>
</div>
`).join('');
})
.catch(err => {
container.innerHTML = '<p class="results-empty">Could not load schema data.</p>';
console.error('Schema load error:', err);
});
}
|