From 77957a61a3f29ba727e080b4a034dbc25f91ee6b Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 17 Feb 2025 00:24:49 +1100 Subject: [PATCH] Add logs view for timer functions - Create new database table `timer_function_invocations` to track function executions - Implement `/logs/` route in timer routes to fetch and display invocation logs - Add new logs template for timer functions with detailed invocation information - Update header templates to include logs URL for timer functions --- routes/timer.py | 49 +++++++++++++ .../dashboard/http_functions/client.html | 3 +- .../dashboard/http_functions/editor.html | 3 +- .../dashboard/http_functions/header.html | 3 +- .../dashboard/http_functions/history.html | 3 +- templates/dashboard/http_functions/logs.html | 3 +- templates/dashboard/timer_functions/edit.html | 3 +- templates/dashboard/timer_functions/logs.html | 72 +++++++++++++++++++ 8 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 templates/dashboard/timer_functions/logs.html diff --git a/routes/timer.py b/routes/timer.py index 62f45ff..1d9a032 100644 --- a/routes/timer.py +++ b/routes/timer.py @@ -101,6 +101,21 @@ AFTER INSERT OR UPDATE ON timer_functions FOR EACH ROW EXECUTE PROCEDURE fn_timer_functions_versioning(); + +CREATE TABLE timer_function_invocations ( + id SERIAL PRIMARY KEY, + timer_function_id INT NOT NULL, + status TEXT, + invocation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + logs TEXT, + version_number INT NOT NULL, + + CONSTRAINT fk_timer_function_invocations + FOREIGN KEY (timer_function_id) + REFERENCES timer_functions (id) + ON DELETE CASCADE +); + ''' DEFAULT_SCRIPT = """async (req) => { @@ -353,4 +368,38 @@ def toggle(function_id): "message": f"Error toggling timer function: {str(e)}" }), 400 +@timer.route('/logs/') +@login_required +def logs(function_id): + # Fetch the timer function to verify ownership + timer_function = db.execute(""" + SELECT id, name + FROM timer_functions + WHERE id = %s AND user_id = %s + """, [function_id, current_user.id], one=True) + + if not timer_function: + flash('Timer function not found', 'error') + return redirect(url_for('timer.overview')) + + # Fetch the invocation logs + timer_function_invocations = db.execute(""" + SELECT id, timer_function_id, status, invocation_time, + logs, version_number + FROM timer_function_invocations + WHERE timer_function_id = %s + ORDER BY invocation_time DESC + LIMIT 100 + """, [function_id]) + + args = { + 'user_id': current_user.id, + 'function_id': function_id, + 'timer_function_invocations': timer_function_invocations + } + + if htmx: + return render_block(environment, 'dashboard/timer_functions/logs.html', 'page', **args) + return render_template('dashboard/timer_functions/logs.html', **args) + diff --git a/templates/dashboard/http_functions/client.html b/templates/dashboard/http_functions/client.html index b7510e1..79bd0bb 100644 --- a/templates/dashboard/http_functions/client.html +++ b/templates/dashboard/http_functions/client.html @@ -9,7 +9,8 @@ show_logs=True, show_client=True, show_history=True, edit_url=url_for('http_function_editor', function_id=function_id), -cancel_url=url_for('dashboard_http_functions')) }} +cancel_url=url_for('dashboard_http_functions', +logs_url=url_for('get_http_function_logs', function_id=function_id))) }}
diff --git a/templates/dashboard/http_functions/editor.html b/templates/dashboard/http_functions/editor.html index 31c43d2..2dc23b6 100644 --- a/templates/dashboard/http_functions/editor.html +++ b/templates/dashboard/http_functions/editor.html @@ -9,7 +9,8 @@ show_logs=True, show_client=True, show_history=True, edit_url=edit_url, -cancel_url=cancel_url) }} +cancel_url=cancel_url, +logs_url=url_for('get_http_function_logs', function_id=function_id)) }}
diff --git a/templates/dashboard/http_functions/header.html b/templates/dashboard/http_functions/header.html index ab41c9c..69787c8 100644 --- a/templates/dashboard/http_functions/header.html +++ b/templates/dashboard/http_functions/header.html @@ -26,8 +26,7 @@ {% if show_logs|default(false, true) %}