Add logs view for timer functions

- Create new database table `timer_function_invocations` to track function executions
- Implement `/logs/<function_id>` 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
This commit is contained in:
Peter Stockings
2025-02-17 00:24:49 +11:00
parent 29da93a9f8
commit 77957a61a3
8 changed files with 132 additions and 7 deletions

View File

@@ -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/<int:function_id>')
@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)