06/05 Optimize app: logs will be saved to db

This commit is contained in:
2026-06-05 12:48:55 -04:00
parent 8f8b0348c6
commit f8114e7997
8 changed files with 355 additions and 94 deletions
+45 -7
View File
@@ -168,12 +168,10 @@ tr.expanded-row > td {
{% if file_exists %}
<span class="log-meta-chip"><i class="bi bi-hdd"></i>{{ file_size_fmt }}</span>
<span class="log-meta-chip"><i class="bi bi-clock"></i>{{ file_mtime }}</span>
<span class="log-meta-chip" style="max-width:360px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="{{ log_file }}">
<i class="bi bi-file-text"></i>{{ log_file }}
</span>
{% else %}
<span class="log-meta-chip" style="color:#ef4444;"><i class="bi bi-exclamation-triangle"></i>Log file not found</span>
{% endif %}
<span class="log-meta-chip"><i class="bi bi-database"></i>{{ db_count }} entries in DB</span>
</div>
<div class="ms-auto">
<span id="spinner" class="spinner-border spinner-border-sm text-secondary"></span>
@@ -226,8 +224,21 @@ tr.expanded-row > td {
<i class="bi bi-download me-1"></i><span class="d-none d-sm-inline">Download</span>
</a>
<button class="btn btn-sm btn-outline-danger" id="clear-btn" title="Clear log file">
<i class="bi bi-trash me-1"></i><span class="d-none d-sm-inline">Clear</span>
<div class="dropdown">
<button class="btn btn-sm btn-outline-warning dropdown-toggle" type="button"
data-bs-toggle="dropdown" title="Purge old log entries from database">
<i class="bi bi-clock-history me-1"></i><span class="d-none d-sm-inline">Purge</span>
</button>
<ul class="dropdown-menu dropdown-menu-end" style="font-size:13px;">
<li><h6 class="dropdown-header">Delete entries older than…</h6></li>
<li><a class="dropdown-item purge-item" href="#" data-days="7">7 days</a></li>
<li><a class="dropdown-item purge-item" href="#" data-days="30">30 days</a></li>
<li><a class="dropdown-item purge-item" href="#" data-days="90">90 days</a></li>
</ul>
</div>
<button class="btn btn-sm btn-outline-danger" id="clear-btn" title="Clear all logs (DB + file)">
<i class="bi bi-trash me-1"></i><span class="d-none d-sm-inline">Clear All</span>
</button>
</div>
@@ -470,18 +481,45 @@ document.getElementById('ar-toggle').addEventListener('click', () => {
// ── Clear ───────────────────────────────────────────────────────────────────
document.getElementById('clear-btn').addEventListener('click', function() {
if (!confirm('Clear all log entries from the file?\nThis cannot be undone.')) return;
if (!confirm('Clear ALL log entries from the database and log file?\nThis cannot be undone.')) return;
fetch('{{ url_for("logs.clear") }}', {
method: 'POST',
headers: { 'X-CSRFToken': CSRF, 'Content-Type': 'application/json' },
})
.then(r => r.json())
.then(d => {
if (d.status === 'ok') load();
if (d.status === 'ok') { load(); location.reload(); }
else alert('Clear failed: ' + d.error);
});
});
// ── Purge ───────────────────────────────────────────────────────────────────
document.querySelectorAll('.purge-item').forEach(function(el) {
el.addEventListener('click', function(e) {
e.preventDefault();
const days = this.dataset.days;
if (!confirm(`Delete all log entries older than ${days} days?\nThis cannot be undone.`)) return;
const fd = new FormData();
fd.append('days', days);
fetch('{{ url_for("logs.purge") }}', {
method: 'POST',
headers: { 'X-CSRFToken': CSRF },
body: fd,
})
.then(r => r.json())
.then(d => {
if (d.status === 'ok') {
load();
// Update DB count chip
const chip = document.querySelector('.log-meta-chip .bi-database');
if (chip) chip.parentElement.innerHTML = `<i class="bi bi-database"></i> refreshed`;
} else {
alert('Purge failed: ' + d.error);
}
});
});
});
// ── Keyboard shortcuts ──────────────────────────────────────────────────────
document.addEventListener('keydown', function(e) {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') return;