From 24a0c0ffef19f423544db3960368b3dc0af1996b Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Thu, 20 Feb 2025 22:51:52 +1100 Subject: [PATCH] Enhance timer function tracking with invocation count and log storage - Add `invocation_count` column to `timer_functions` table - Change `logs` column in `timer_function_invocations` to JSONB type - Create trigger function to automatically increment invocation count on new log entries --- routes/timer.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/routes/timer.py b/routes/timer.py index 78d7d65..26e686a 100644 --- a/routes/timer.py +++ b/routes/timer.py @@ -25,6 +25,7 @@ CREATE TABLE timer_functions ( next_run TIMESTAMPTZ, last_run TIMESTAMPTZ, enabled BOOLEAN NOT NULL DEFAULT TRUE, + invocation_count INT NOT NULL DEFAULT 0, -- Define the foreign key constraint CONSTRAINT fk_timer_functions_user @@ -107,7 +108,7 @@ CREATE TABLE timer_function_invocations ( timer_function_id INT NOT NULL, status TEXT, invocation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), - logs TEXT, + logs JSONB, version_number INT NOT NULL, CONSTRAINT fk_timer_function_invocations @@ -116,6 +117,23 @@ CREATE TABLE timer_function_invocations ( ON DELETE CASCADE ); +CREATE OR REPLACE FUNCTION fn_increment_invocation_count() +RETURNS TRIGGER +LANGUAGE plpgsql +AS $$ +BEGIN + UPDATE timer_functions + SET invocation_count = invocation_count + 1 + WHERE id = NEW.timer_function_id; + + RETURN NEW; +END; +$$; + +CREATE TRIGGER tr_increment_invocation_count +AFTER INSERT ON timer_function_invocations +FOR EACH ROW +EXECUTE PROCEDURE fn_increment_invocation_count(); ''' DEFAULT_SCRIPT = """async (req) => { @@ -439,4 +457,3 @@ def history(function_id): return render_block(environment, 'dashboard/timer_functions/history.html', 'page', **args) return render_template('dashboard/timer_functions/history.html', **args) -