From b4c67d834614b0d1dabbed85be9e532a7d608269 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 6 Apr 2024 19:49:46 +1100 Subject: [PATCH] Add versions for http_functions, whenever script is updated a new version is logged. --- app.py | 11 +++++++---- db.py | 19 ++++++++++--------- services.py | 1 + templates/dashboard/http_functions/edit.html | 2 +- templates/dashboard/http_functions/logs.html | 3 +++ .../dashboard/http_functions/overview.html | 11 +++++++---- templates/function_editor.html | 14 ++++++++------ 7 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index 98aa106..dcb3e38 100644 --- a/app.py +++ b/app.py @@ -158,10 +158,11 @@ def get_http_function_edit_form(function_id): is_public = http_function['is_public'] log_request = http_function['log_request'] log_response = http_function['log_response'] + version_number = http_function['version_number'] if htmx: - return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) - return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) + return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number) + return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number) @ app.route("/dashboard/http_functions//edit", methods=["POST"]) @login_required @@ -175,7 +176,7 @@ def edit_http_function(function_id): log_request = request.json.get('log_request') log_response = request.json.get('log_response') - db.edit_http_function(user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response) + updated_version = db.edit_http_function(user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response) return { "status": "success", "message": f'{name} updated' } except Exception as e: @@ -275,6 +276,7 @@ def execute_http_function(user_id, function): is_public = http_function['is_public'] log_request = http_function['log_request'] log_response = http_function['log_response'] + version_number = http_function['version_number'] # Check if the function is public, if not check if the user is authenticated and owns the function if not is_public: @@ -323,7 +325,8 @@ def execute_http_function(user_id, function): response_data['status'], request_data if log_request else {}, response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {}, - response_data['logs']) + response_data['logs'], + version_number) if response_data['status'] != 'SUCCESS': return render_template("function_error.html", function_name=function_name ,error=response_data['result'], logs=response_data['logs']) diff --git a/db.py b/db.py index a42324f..20c0fe2 100644 --- a/db.py +++ b/db.py @@ -49,17 +49,17 @@ class DataBase(): def get_http_functions_for_user(self, user_id): http_functions = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id]) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s ORDER by id DESC', [user_id]) return http_functions def get_http_function(self, user_id, name): http_function = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) return http_function def get_http_function_by_id(self, user_id, http_function_id): http_function = self.execute( - 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True) + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True) return http_function def create_new_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response): @@ -70,11 +70,12 @@ class DataBase(): ) def edit_http_function(self, user_id, function_id, name, script_content, environment_info, is_public, log_request, log_response): - self.execute( - 'UPDATE http_functions SET NAME=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s WHERE user_id=%s AND id=%s', + updated_version = self.execute( + 'UPDATE http_functions SET NAME=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s WHERE user_id=%s AND id=%s RETURNING version_number', [name, script_content, environment_info, is_public, log_request, log_response, user_id, function_id], - commit=True + commit=True, one=True ) + return updated_version def update_http_function_environment_info_and_invoked_count(self, user_id, name, environment_info): self.execute( @@ -84,13 +85,13 @@ class DataBase(): self.execute( 'DELETE FROM http_functions WHERE user_id=%s AND id=%s', [user_id, function_id], commit=True) - def add_http_function_invocation(self, http_function_id, status, request_data, response_data, logs): + def add_http_function_invocation(self, http_function_id, status, request_data, response_data, logs, version_number): self.execute( - 'INSERT INTO http_function_invocations (http_function_id, status, request_data, response_data, logs) VALUES (%s, %s, %s, %s, %s)', [http_function_id, status, json.dumps(request_data), json.dumps(response_data), json.dumps(logs)], commit=True) + 'INSERT INTO http_function_invocations (http_function_id, status, request_data, response_data, logs, version_number) VALUES (%s, %s, %s, %s, %s, %s)', [http_function_id, status, json.dumps(request_data), json.dumps(response_data), json.dumps(logs), version_number], commit=True) def get_http_function_invocations(self, http_function_id): http_function_invocations = self.execute( - """SELECT id, http_function_id, STATUS, invocation_time, request_data, response_data, LOGS + """SELECT id, http_function_id, STATUS, invocation_time, request_data, response_data, LOGS, version_number FROM http_function_invocations WHERE http_function_id=%s ORDER BY invocation_time DESC""", [http_function_id]) diff --git a/services.py b/services.py index 5fa94ce..1b0aad4 100644 --- a/services.py +++ b/services.py @@ -9,6 +9,7 @@ def create_http_function_view_model(http_function): "is_public": http_function['is_public'], "log_request": http_function['log_request'], "log_response": http_function['log_response'], + "version_number": http_function['version_number'] } return function_view_model diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html index c3cb664..6b1b680 100644 --- a/templates/dashboard/http_functions/edit.html +++ b/templates/dashboard/http_functions/edit.html @@ -8,6 +8,6 @@ refresh_url=url_for('get_http_function_edit_form', function_id=function_id), sho {{ render_partial('function_editor.html', function_id=function_id, name=name, script=script, environment_info=environment_info, -is_public=is_public, log_request=log_request, log_response=log_response, is_edit=True) }} +is_public=is_public, log_request=log_request, log_response=log_response, version_number=version_number, is_edit=True) }} {% endblock %} \ No newline at end of file diff --git a/templates/dashboard/http_functions/logs.html b/templates/dashboard/http_functions/logs.html index ab718f0..a89f8e2 100644 --- a/templates/dashboard/http_functions/logs.html +++ b/templates/dashboard/http_functions/logs.html @@ -25,6 +25,9 @@ refresh_url=url_for('get_http_function_logs', function_id=function_id), show_edi {{ invocation.invocation_time.strftime('%Y-%m-%d %H:%M:%S') }} + + v{{ invocation.version_number }} + diff --git a/templates/dashboard/http_functions/overview.html b/templates/dashboard/http_functions/overview.html index 3130e89..7dfb2ec 100644 --- a/templates/dashboard/http_functions/overview.html +++ b/templates/dashboard/http_functions/overview.html @@ -32,11 +32,14 @@ {% for function in http_functions %} - + {{ function.name }} - - {{ function.invoked_count }} + + #{{ function.invoked_count }} + + + v{{ function.version_number }} {% if function.is_public %} diff --git a/templates/function_editor.html b/templates/function_editor.html index d5f8879..602a403 100644 --- a/templates/function_editor.html +++ b/templates/function_editor.html @@ -17,6 +17,10 @@ + + v{{ version_number }} + +