Store duration of http & timer functions in ms from begining of request to end

This commit is contained in:
Peter Stockings
2025-12-02 19:55:32 +11:00
parent d04b7f2120
commit 71296b1301
2 changed files with 12 additions and 2 deletions

7
app.py
View File

@@ -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/<int:user_id>/<path:function>', 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'])

View File

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