From 1319a61b0c9f6da710722a6d9bb2266a38883267 Mon Sep 17 00:00:00 2001 From: brahmani Date: Sun, 31 May 2026 19:09:46 +0530 Subject: [PATCH] Implement Clear Command History button and backend purge (Issue #50) --- app.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ ui/app.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- ui/index.html | 1 + 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 097aeaf..afd8beb 100644 --- a/app.py +++ b/app.py @@ -603,6 +603,54 @@ def get_command_history(): }) +@app.route('/api/command_history/clear', methods=['POST']) +def clear_command_history(): + try: + # Overwrite COMMAND_HISTORY_FILE + with open(COMMAND_HISTORY_FILE, 'w', encoding='utf-8') as f: + json.dump([], f) + + # Empty history files + with open(HISTORY_FILE, 'w', encoding='utf-8') as f: + pass + with open(FAILED_HISTORY_FILE, 'w', encoding='utf-8') as f: + pass + + # Clear execution logs + if os.path.exists(EXECUTION_LOG_DIR): + for filename in os.listdir(EXECUTION_LOG_DIR): + file_path = os.path.join(EXECUTION_LOG_DIR, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except Exception: + pass + + # Clear session logs + if os.path.exists(SESSION_LOG_DIR): + for filename in os.listdir(SESSION_LOG_DIR): + file_path = os.path.join(SESSION_LOG_DIR, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except Exception: + pass + + return jsonify({ + 'success': True, + 'message': 'Command and execution history cleared successfully' + }) + except Exception as e: + return jsonify({ + 'success': False, + 'message': f'Failed to clear history: {str(e)}' + }), 500 + + @app.route('/api/history/analytics') def history_analytics(): entries = _load_history_entries(limit=1000) diff --git a/ui/app.js b/ui/app.js index 5dd09d3..0847698 100644 --- a/ui/app.js +++ b/ui/app.js @@ -19,6 +19,7 @@ const API = { history: '/api/history', history_export: '/api/history/export', kill: '/api/scripts/kill', + command_history_clear: '/api/command_history/clear', }; // ─── State ──────────────────────────────────────────────── @@ -656,6 +657,47 @@ async function exportExecutionHistory(format = 'log') { URL.revokeObjectURL(url); } +async function clearExecutionHistory() { + if (!confirm('Are you sure you want to clear all command and execution history? This will delete all script and command run logs, and reset CLI command history. This action cannot be undone.')) { + return; + } + try { + const res = await fetch(API.command_history_clear, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }); + const data = await res.json(); + if (data.success) { + // Reset state + state.cmdHistory = []; + state.cmdHistoryIndex = -1; + state.historyEntries = []; + state.historySummary = { + total: 0, + failed: 0, + successful: 0, + scripts: 0, + commands: 0 + }; + + // Re-render + renderHistorySummary(state.historySummary); + renderHistoryEntries(state.historyEntries); + + // Notify user + notify('History cleared successfully.', 'success'); + } else { + notify(data.message || 'Failed to clear history.', 'error'); + } + } catch (err) { + console.error('Error clearing history:', err); + notify('Failed to clear history. Server might be offline.', 'error'); + } +} + + async function saveScript(category, filename, content) { const btn = document.getElementById('modal-save'); @@ -1891,9 +1933,9 @@ function bindEvents() { const historyOverlay = document.getElementById('history-modal-overlay'); const historyClose = document.getElementById('history-modal-close'); const historySearch = document.getElementById('history-search'); - const historyFilters = document.querySelectorAll('.history-filter'); const historyExportTxt = document.getElementById('history-export-txt'); const historyExportLog = document.getElementById('history-export-log'); + const btnClearHistory = document.getElementById('btn-clear-history'); if (historyClose) historyClose.addEventListener('click', closeHistoryViewer); if (historyOverlay) { @@ -1917,6 +1959,8 @@ function bindEvents() { }); if (historyExportTxt) historyExportTxt.addEventListener('click', () => exportExecutionHistory('txt')); if (historyExportLog) historyExportLog.addEventListener('click', () => exportExecutionHistory('log')); + if (btnClearHistory) btnClearHistory.addEventListener('click', clearExecutionHistory); + // Main Modal controls document.getElementById('modal-close').addEventListener('click', closeModal); diff --git a/ui/index.html b/ui/index.html index 5ade3af..e7d6ca2 100644 --- a/ui/index.html +++ b/ui/index.html @@ -623,6 +623,7 @@

Execution History

+