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
This commit is contained in:
Peter Stockings
2025-02-20 22:51:52 +11:00
parent 19b5508a9c
commit 24a0c0ffef

View File

@@ -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)