Store duration of http & timer functions in ms from begining of request to end
This commit is contained in:
7
app.py
7
app.py
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from flask import Flask, Response, jsonify, redirect, render_template, render_template_string, request, url_for
|
from flask import Flask, Response, jsonify, redirect, render_template, render_template_string, request, url_for
|
||||||
import jinja_partials
|
import jinja_partials
|
||||||
from jinja2_fragments import render_block
|
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'])
|
@app.route('/f/<int:user_id>/<path:function>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'])
|
||||||
async def execute_http_function(user_id, function):
|
async def execute_http_function(user_id, function):
|
||||||
try:
|
try:
|
||||||
|
start_time = time.time()
|
||||||
# Split the function_path into the function name and the sub-path
|
# Split the function_path into the function name and the sub-path
|
||||||
parts = function.split('/', 1)
|
parts = function.split('/', 1)
|
||||||
function_name = parts[0]
|
function_name = parts[0]
|
||||||
@@ -267,6 +269,9 @@ async def execute_http_function(user_id, function):
|
|||||||
# Update function's own environment (without shared envs)
|
# Update function's own environment (without shared envs)
|
||||||
db.update_http_function_environment_info_and_invoked_count(user_id, function_name, function_specific_env)
|
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(
|
db.add_http_function_invocation(
|
||||||
http_function['id'],
|
http_function['id'],
|
||||||
response_data['status'],
|
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['result'] if (log_response or response_data['status'] != 'SUCCESS') else {},
|
||||||
response_data['logs'],
|
response_data['logs'],
|
||||||
version_number,
|
version_number,
|
||||||
response_data.get('execution_time'))
|
execution_time)
|
||||||
|
|
||||||
if response_data['status'] != 'SUCCESS':
|
if response_data['status'] != 'SUCCESS':
|
||||||
return render_template("function_error.html", function_name=function_name ,error=response_data['result'], logs=response_data['logs'])
|
return render_template("function_error.html", function_name=function_name ,error=response_data['result'], logs=response_data['logs'])
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
# Load environment variables from .env file first, before any other imports
|
# 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
|
Execute a timer function asynchronously and record the invocation
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
start_time = time.time()
|
||||||
code = timer_function['code']
|
code = timer_function['code']
|
||||||
environment = timer_function['environment']
|
environment = timer_function['environment']
|
||||||
name = timer_function['name']
|
name = timer_function['name']
|
||||||
@@ -111,6 +113,9 @@ async def execute_timer_function_async(timer_function):
|
|||||||
WHERE id = %s
|
WHERE id = %s
|
||||||
""", [json.dumps(function_specific_env), next_run, timer_function['id']], commit=True)
|
""", [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
|
# Record the invocation
|
||||||
db.execute("""
|
db.execute("""
|
||||||
INSERT INTO timer_function_invocations
|
INSERT INTO timer_function_invocations
|
||||||
@@ -121,7 +126,7 @@ async def execute_timer_function_async(timer_function):
|
|||||||
response_data['status'],
|
response_data['status'],
|
||||||
json.dumps(response_data['logs']),
|
json.dumps(response_data['logs']),
|
||||||
version_number,
|
version_number,
|
||||||
response_data.get('execution_time')
|
execution_time
|
||||||
], commit=True)
|
], commit=True)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user