update : tasks sch
Local Docker Build (Dev) / build-dev (push) Successful in 6s

This commit is contained in:
2025-10-28 18:26:28 -04:00
parent f11839e005
commit 6711e926a6
3 changed files with 449 additions and 4 deletions
+1 -1
View File
@@ -654,6 +654,6 @@
<!-- Toast Notifications -->
<div class="toast-container" id="toast-container"></div>
<script src="/static/js/app.js?v=manual-scan-ui"></script>
<script src="/static/js/app.js?v=scheduled-scans-system"></script>
</body>
</html>
+394 -1
View File
@@ -53,6 +53,9 @@ function switchTab(tabName) {
case 'reports':
loadReport();
break;
case 'scheduled-scans':
loadScheduledScans();
break;
case 'tools':
loadDetailedStats();
break;
@@ -1625,4 +1628,394 @@ async function logout() {
console.error('Logout failed:', error);
showToast('❌ Logout error', 'error');
}
}
}
// Scheduled Scans Functions
async function loadScheduledScans() {
try {
// Load schedules
const response = await fetch('/api/admin/scheduled-scans');
const data = await response.json();
if (data.success) {
populateSchedulesTable(data.scans);
} else {
showToast('❌ Failed to load scheduled scans', 'error');
}
// Load execution history
const execResponse = await fetch('/api/admin/scheduled-scans/executions');
const execData = await execResponse.json();
if (execData.success) {
populateExecutionsTable(execData.executions);
}
} catch (error) {
console.error('Failed to load scheduled scans:', error);
showToast('❌ Error loading scheduled scans', 'error');
}
}
function populateSchedulesTable(scans) {
const tbody = document.getElementById('schedules-table-body');
tbody.innerHTML = '';
if (scans.length === 0) {
tbody.innerHTML = '<tr><td colspan="8" class="text-center">No scheduled scans configured</td></tr>';
return;
}
scans.forEach(scan => {
const row = document.createElement('tr');
const nextRun = scan.next_run_at ? new Date(scan.next_run_at).toLocaleString() : 'Not scheduled';
const lastRun = scan.last_run_at ? new Date(scan.last_run_at).toLocaleString() : 'Never';
const statusBadge = scan.enabled ?
'<span class="badge badge-success">Enabled</span>' :
'<span class="badge badge-secondary">Disabled</span>';
row.innerHTML = `
<td>
<strong>${scan.name}</strong>
${scan.description ? `<br><small class="text-muted">${scan.description}</small>` : ''}
</td>
<td>
<span class="badge badge-info">${scan.media_type}</span>
</td>
<td>
<span class="badge badge-primary">${scan.scan_mode}</span>
</td>
<td>
<code>${scan.cron_expression}</code>
</td>
<td>${lastRun}</td>
<td>${nextRun}</td>
<td>${statusBadge}</td>
<td>
<div class="btn-group">
<button class="btn btn-sm btn-outline" onclick="editSchedule(${scan.id})" title="Edit">
<i class="fas fa-edit"></i>
</button>
<button class="btn btn-sm btn-outline" onclick="toggleSchedule(${scan.id})" title="Toggle">
<i class="fas fa-power-off"></i>
</button>
<button class="btn btn-sm btn-outline" onclick="runSchedule(${scan.id})" title="Run Now">
<i class="fas fa-play"></i>
</button>
<button class="btn btn-sm btn-outline text-red" onclick="deleteSchedule(${scan.id})" title="Delete">
<i class="fas fa-trash"></i>
</button>
</div>
</td>
`;
tbody.appendChild(row);
});
}
function populateExecutionsTable(executions) {
const tbody = document.getElementById('executions-table-body');
tbody.innerHTML = '';
if (executions.length === 0) {
tbody.innerHTML = '<tr><td colspan="8" class="text-center">No execution history</td></tr>';
return;
}
executions.forEach(exec => {
const row = document.createElement('tr');
const started = new Date(exec.started_at).toLocaleString();
const duration = exec.execution_time_seconds ?
`${exec.execution_time_seconds}s` : 'N/A';
let statusBadge;
switch (exec.status) {
case 'completed':
statusBadge = '<span class="badge badge-success">Completed</span>';
break;
case 'failed':
statusBadge = '<span class="badge badge-danger">Failed</span>';
break;
case 'running':
statusBadge = '<span class="badge badge-primary">Running</span>';
break;
default:
statusBadge = `<span class="badge badge-secondary">${exec.status}</span>`;
}
row.innerHTML = `
<td>${exec.schedule_name}</td>
<td>${started}</td>
<td>${duration}</td>
<td>${statusBadge}</td>
<td>${exec.items_processed || 0}</td>
<td>${exec.items_skipped || 0}</td>
<td>${exec.items_failed || 0}</td>
<td>
${exec.error_message ?
`<button class="btn btn-sm btn-outline" onclick="showExecutionDetails(${exec.id})" title="View Details">
<i class="fas fa-info-circle"></i>
</button>` : ''}
</td>
`;
tbody.appendChild(row);
});
}
function openScheduleModal(scheduleId = null) {
const modal = document.getElementById('schedule-modal');
const title = document.getElementById('schedule-modal-title');
const submitText = document.getElementById('schedule-submit-text');
const form = document.getElementById('schedule-form');
// Reset form
form.reset();
document.getElementById('schedule-id').value = '';
document.getElementById('schedule-enabled').checked = true;
if (scheduleId) {
title.textContent = 'Edit Schedule';
submitText.textContent = 'Update Schedule';
loadScheduleForEdit(scheduleId);
} else {
title.textContent = 'Add New Schedule';
submitText.textContent = 'Create Schedule';
}
modal.style.display = 'block';
}
function closeScheduleModal() {
document.getElementById('schedule-modal').style.display = 'none';
}
async function loadScheduleForEdit(scheduleId) {
try {
const response = await fetch('/api/admin/scheduled-scans');
const data = await response.json();
if (data.success) {
const schedule = data.scans.find(s => s.id === scheduleId);
if (schedule) {
document.getElementById('schedule-id').value = schedule.id;
document.getElementById('schedule-name').value = schedule.name;
document.getElementById('schedule-description').value = schedule.description || '';
document.getElementById('schedule-media-type').value = schedule.media_type;
document.getElementById('schedule-scan-mode').value = schedule.scan_mode;
document.getElementById('schedule-cron').value = schedule.cron_expression;
document.getElementById('schedule-paths').value = schedule.specific_paths || '';
document.getElementById('schedule-enabled').checked = schedule.enabled;
}
}
} catch (error) {
console.error('Failed to load schedule for edit:', error);
showToast('❌ Failed to load schedule details', 'error');
}
}
async function toggleSchedule(scheduleId) {
try {
const response = await fetch(`/api/admin/scheduled-scans/${scheduleId}/toggle`, {
method: 'POST'
});
const data = await response.json();
if (data.success) {
showToast(`${data.message}`, 'success');
loadScheduledScans(); // Reload table
} else {
showToast(`${data.error}`, 'error');
}
} catch (error) {
console.error('Failed to toggle schedule:', error);
showToast('❌ Error toggling schedule', 'error');
}
}
async function runSchedule(scheduleId) {
if (!confirm('Are you sure you want to run this scan now?')) {
return;
}
try {
const response = await fetch(`/api/admin/scheduled-scans/${scheduleId}/run`, {
method: 'POST'
});
const data = await response.json();
if (data.success) {
showToast(`${data.message}`, 'success');
// Reload executions table after a moment
setTimeout(() => {
loadScheduledScans();
}, 2000);
} else {
showToast(`${data.error}`, 'error');
}
} catch (error) {
console.error('Failed to run schedule:', error);
showToast('❌ Error running schedule', 'error');
}
}
async function deleteSchedule(scheduleId) {
if (!confirm('Are you sure you want to delete this scheduled scan? This action cannot be undone.')) {
return;
}
try {
const response = await fetch(`/api/admin/scheduled-scans/${scheduleId}`, {
method: 'DELETE'
});
const data = await response.json();
if (data.success) {
showToast(`${data.message}`, 'success');
loadScheduledScans(); // Reload table
} else {
showToast(`${data.error}`, 'error');
}
} catch (error) {
console.error('Failed to delete schedule:', error);
showToast('❌ Error deleting schedule', 'error');
}
}
function editSchedule(scheduleId) {
openScheduleModal(scheduleId);
}
// Cron Builder Functions
function openCronBuilder() {
document.getElementById('cron-builder-modal').style.display = 'block';
updateCronPreview();
}
function closeCronBuilder() {
document.getElementById('cron-builder-modal').style.display = 'none';
}
function setCronPreset(expression) {
const parts = expression.split(' ');
document.getElementById('cron-minute').value = parts[0];
document.getElementById('cron-hour').value = parts[1];
document.getElementById('cron-day').value = parts[2];
document.getElementById('cron-month').value = parts[3];
document.getElementById('cron-dow').value = parts[4];
updateCronPreview();
}
function updateCronPreview() {
const minute = document.getElementById('cron-minute').value || '*';
const hour = document.getElementById('cron-hour').value || '*';
const day = document.getElementById('cron-day').value || '*';
const month = document.getElementById('cron-month').value || '*';
const dow = document.getElementById('cron-dow').value || '*';
const expression = `${minute} ${hour} ${day} ${month} ${dow}`;
document.getElementById('cron-preview-text').textContent = expression;
// Simple description generation
let description = 'Runs ';
if (minute === '0' && hour !== '*' && day === '*' && month === '*' && dow === '*') {
description += `daily at ${hour}:00`;
} else if (minute === '0' && hour !== '*' && day === '*' && month === '*' && dow !== '*') {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
description += `weekly on ${days[dow]} at ${hour}:00`;
} else if (minute === '0' && hour !== '*' && day !== '*' && month === '*' && dow === '*') {
description += `monthly on day ${day} at ${hour}:00`;
} else {
description += 'with custom schedule';
}
document.getElementById('cron-description').textContent = description;
}
function applyCronExpression() {
const expression = document.getElementById('cron-preview-text').textContent;
document.getElementById('schedule-cron').value = expression;
closeCronBuilder();
}
// Event listeners for scheduled scans
document.addEventListener('DOMContentLoaded', function() {
// Add Schedule button
const addScheduleBtn = document.getElementById('add-schedule-btn');
if (addScheduleBtn) {
addScheduleBtn.addEventListener('click', () => openScheduleModal());
}
// Cron builder button
const cronBuilderBtn = document.getElementById('cron-builder-btn');
if (cronBuilderBtn) {
cronBuilderBtn.addEventListener('click', openCronBuilder);
}
// Cron field change listeners
const cronFields = ['cron-minute', 'cron-hour', 'cron-day', 'cron-month', 'cron-dow'];
cronFields.forEach(fieldId => {
const field = document.getElementById(fieldId);
if (field) {
field.addEventListener('input', updateCronPreview);
}
});
// Schedule form submission
const scheduleForm = document.getElementById('schedule-form');
if (scheduleForm) {
scheduleForm.addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const scheduleId = document.getElementById('schedule-id').value;
const scheduleData = {
name: formData.get('schedule-name') || document.getElementById('schedule-name').value,
description: formData.get('schedule-description') || document.getElementById('schedule-description').value,
cron_expression: formData.get('schedule-cron') || document.getElementById('schedule-cron').value,
media_type: formData.get('schedule-media-type') || document.getElementById('schedule-media-type').value,
scan_mode: formData.get('schedule-scan-mode') || document.getElementById('schedule-scan-mode').value,
specific_paths: formData.get('schedule-paths') || document.getElementById('schedule-paths').value,
enabled: document.getElementById('schedule-enabled').checked
};
try {
let url, method;
if (scheduleId) {
url = `/api/admin/scheduled-scans/${scheduleId}`;
method = 'PUT';
} else {
url = '/api/admin/scheduled-scans';
method = 'POST';
}
const response = await fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(scheduleData)
});
const data = await response.json();
if (data.success) {
showToast(`${data.message}`, 'success');
closeScheduleModal();
loadScheduledScans(); // Reload table
} else {
showToast(`${data.error}`, 'error');
}
} catch (error) {
console.error('Failed to save schedule:', error);
showToast('❌ Error saving schedule', 'error');
}
});
}
});