diff --git a/routes/home.py b/routes/home.py index 290d134..0cc89e4 100644 --- a/routes/home.py +++ b/routes/home.py @@ -12,48 +12,58 @@ def index(): # Fetch user statistics stats = db.execute(""" WITH timer_stats AS ( - SELECT + SELECT COUNT(*) as total_timer_functions, COUNT(*) FILTER (WHERE enabled = true) as active_timer_functions, - (SELECT COUNT(*) FROM timer_function_invocations tfi - JOIN timer_functions tf ON tf.id = tfi.timer_function_id + (SELECT COUNT(*) FROM timer_function_invocations tfi + JOIN timer_functions tf ON tf.id = tfi.timer_function_id WHERE tf.user_id = %s) as timer_invocations, - (SELECT COUNT(*) FROM timer_function_invocations tfi - JOIN timer_functions tf ON tf.id = tfi.timer_function_id + (SELECT COUNT(*) FROM timer_function_invocations tfi + JOIN timer_functions tf ON tf.id = tfi.timer_function_id WHERE tf.user_id = %s AND tfi.status = 'SUCCESS') as timer_successful_invocations, + (SELECT AVG(tfi.execution_time) FROM timer_function_invocations tfi + JOIN timer_functions tf ON tf.id = tfi.timer_function_id + WHERE tf.user_id = %s) as avg_timer_execution_time, MAX(last_run) as last_timer_invocation FROM timer_functions WHERE user_id = %s ), http_stats AS ( - SELECT + SELECT COUNT(*) as total_http_functions, COUNT(*) FILTER (WHERE is_public = true) as public_http_functions, - SUM(invoked_count) as http_invocations, - (SELECT COUNT(*) FROM http_function_invocations hfi - JOIN http_functions hf ON hf.id = hfi.http_function_id + (SELECT COUNT(*) FROM http_function_invocations hfi + JOIN http_functions hf ON hf.id = hfi.http_function_id + WHERE hf.user_id = %s) as http_invocations, + (SELECT COUNT(*) FROM http_function_invocations hfi + JOIN http_functions hf ON hf.id = hfi.http_function_id WHERE hf.user_id = %s AND hfi.status = 'SUCCESS') as http_successful_invocations, - (SELECT MAX(invocation_time) - FROM http_function_invocations hfi - JOIN http_functions hf ON hf.id = hfi.http_function_id + (SELECT AVG(hfi.execution_time) FROM http_function_invocations hfi + JOIN http_functions hf ON hf.id = hfi.http_function_id + WHERE hf.user_id = %s) as avg_http_execution_time, + (SELECT MAX(invocation_time) + FROM http_function_invocations hfi + JOIN http_functions hf ON hf.id = hfi.http_function_id WHERE hf.user_id = %s) as last_http_invocation FROM http_functions WHERE user_id = %s ) - SELECT + SELECT *, - CASE - WHEN timer_invocations > 0 THEN + (timer_invocations - timer_successful_invocations) as timer_failed_invocations, + (http_invocations - http_successful_invocations) as http_failed_invocations, + CASE + WHEN timer_invocations > 0 THEN (timer_successful_invocations * 100.0 / timer_invocations)::numeric(5,1) - ELSE 0.0 + ELSE 0.0 END as timer_success_rate, - CASE - WHEN http_invocations > 0 THEN + CASE + WHEN http_invocations > 0 THEN (http_successful_invocations * 100.0 / http_invocations)::numeric(5,1) - ELSE 0.0 + ELSE 0.0 END as http_success_rate FROM timer_stats, http_stats - """, [current_user.id, current_user.id, current_user.id, current_user.id, current_user.id, current_user.id], one=True) + """, [current_user.id, current_user.id, current_user.id, current_user.id, current_user.id, current_user.id, current_user.id, current_user.id, current_user.id], one=True) # Get 24-hour distribution hour_distribution = db.execute(""" diff --git a/templates/dashboard/home.html b/templates/dashboard/home.html index 70780e5..7a07f2a 100644 --- a/templates/dashboard/home.html +++ b/templates/dashboard/home.html @@ -9,7 +9,7 @@
{{ stats.timer_failed_invocations }}
++ {{ "%.2f"|format(stats.avg_timer_execution_time|default(0)) }}s +
+{{ stats.http_failed_invocations }}
++ {{ "%.2f"|format(stats.avg_http_execution_time|default(0)) }}s +
+