From 71296b130118d9b3c7bab6cdabb8956cbee86639 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Tue, 2 Dec 2025 19:55:32 +1100 Subject: [PATCH] Store duration of http & timer functions in ms from begining of request to end --- app.py | 7 ++++++- worker.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index e6a8942..8e24dc1 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import json import os +import time from flask import Flask, Response, jsonify, redirect, render_template, render_template_string, request, url_for import jinja_partials from jinja2_fragments import render_block @@ -129,6 +130,7 @@ async def execute_code(): @app.route('/f//', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD']) async def execute_http_function(user_id, function): try: + start_time = time.time() # Split the function_path into the function name and the sub-path parts = function.split('/', 1) function_name = parts[0] @@ -267,6 +269,9 @@ async def execute_http_function(user_id, function): # Update function's own environment (without shared envs) db.update_http_function_environment_info_and_invoked_count(user_id, function_name, function_specific_env) + # Calculate execution time in milliseconds + execution_time = (time.time() - start_time) * 1000 + db.add_http_function_invocation( http_function['id'], response_data['status'], @@ -274,7 +279,7 @@ async def execute_http_function(user_id, function): response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {}, response_data['logs'], version_number, - response_data.get('execution_time')) + execution_time) 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/worker.py b/worker.py index dd7b5ff..dcac787 100644 --- a/worker.py +++ b/worker.py @@ -1,4 +1,5 @@ import os +import time from dotenv import load_dotenv # Load environment variables from .env file first, before any other imports @@ -31,6 +32,7 @@ async def execute_timer_function_async(timer_function): Execute a timer function asynchronously and record the invocation """ try: + start_time = time.time() code = timer_function['code'] environment = timer_function['environment'] name = timer_function['name'] @@ -111,6 +113,9 @@ async def execute_timer_function_async(timer_function): WHERE id = %s """, [json.dumps(function_specific_env), next_run, timer_function['id']], commit=True) + # Calculate execution time in milliseconds + execution_time = (time.time() - start_time) * 1000 + # Record the invocation db.execute(""" INSERT INTO timer_function_invocations @@ -121,7 +126,7 @@ async def execute_timer_function_async(timer_function): response_data['status'], json.dumps(response_data['logs']), version_number, - response_data.get('execution_time') + execution_time ], commit=True) except Exception as e: