Spaces:
Sleeping
Sleeping
barathvasan-dev commited on
Commit ·
ebbca8b
1
Parent(s): 603954b
Fix: Add database engine None checks to prevent 500 errors
Browse files- Added engine None checks to get_hourly_traffic and get_vehicle_density_by_location
- Added safe_db_query helper function for future use
- Prevents AttributeError when database engine is None
- database.py +33 -0
database.py
CHANGED
|
@@ -93,6 +93,24 @@ except Exception as e:
|
|
| 93 |
print(f"⚠️ Database connection warning: {e}")
|
| 94 |
engine = None
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# =========================================================
|
| 97 |
# CONFIG
|
| 98 |
# =========================================================
|
|
@@ -905,6 +923,10 @@ def health_check():
|
|
| 905 |
def get_vehicles_by_state():
|
| 906 |
"""Get vehicle count by state with timeout protection"""
|
| 907 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 908 |
try:
|
| 909 |
|
| 910 |
sql = """
|
|
@@ -929,6 +951,10 @@ def get_vehicles_by_state():
|
|
| 929 |
def get_hourly_traffic():
|
| 930 |
"""Get traffic by hour with timeout protection"""
|
| 931 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 932 |
try:
|
| 933 |
|
| 934 |
sql = """
|
|
@@ -953,6 +979,10 @@ def get_hourly_traffic():
|
|
| 953 |
def get_top_plates():
|
| 954 |
"""Get top detected plates with timeout protection"""
|
| 955 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 956 |
try:
|
| 957 |
|
| 958 |
sql = """
|
|
@@ -1105,6 +1135,9 @@ def get_peak_traffic_hours():
|
|
| 1105 |
|
| 1106 |
def get_vehicle_density_by_location():
|
| 1107 |
"""Get traffic density (vehicle count) by location"""
|
|
|
|
|
|
|
|
|
|
| 1108 |
try:
|
| 1109 |
sql = """
|
| 1110 |
SELECT location, COUNT(*) as vehicle_count,
|
|
|
|
| 93 |
print(f"⚠️ Database connection warning: {e}")
|
| 94 |
engine = None
|
| 95 |
|
| 96 |
+
# =========================================================
|
| 97 |
+
# HELPER: Safe Database Execution
|
| 98 |
+
# =========================================================
|
| 99 |
+
|
| 100 |
+
def safe_db_query(query_func):
|
| 101 |
+
"""Decorator to safely execute database queries with None check"""
|
| 102 |
+
def wrapper(*args, **kwargs):
|
| 103 |
+
if engine is None:
|
| 104 |
+
print(f"⚠️ Database engine not available for {query_func.__name__}")
|
| 105 |
+
# Return appropriate empty result
|
| 106 |
+
return [] if 'get_' in query_func.__name__ else None
|
| 107 |
+
try:
|
| 108 |
+
return query_func(*args, **kwargs)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"❌ Database query error in {query_func.__name__}: {e}")
|
| 111 |
+
return [] if 'get_' in query_func.__name__ else None
|
| 112 |
+
return wrapper
|
| 113 |
+
|
| 114 |
# =========================================================
|
| 115 |
# CONFIG
|
| 116 |
# =========================================================
|
|
|
|
| 923 |
def get_vehicles_by_state():
|
| 924 |
"""Get vehicle count by state with timeout protection"""
|
| 925 |
|
| 926 |
+
if engine is None:
|
| 927 |
+
print("⚠️ Database not available for get_vehicles_by_state")
|
| 928 |
+
return []
|
| 929 |
+
|
| 930 |
try:
|
| 931 |
|
| 932 |
sql = """
|
|
|
|
| 951 |
def get_hourly_traffic():
|
| 952 |
"""Get traffic by hour with timeout protection"""
|
| 953 |
|
| 954 |
+
if engine is None:
|
| 955 |
+
print("⚠️ Database not available for get_hourly_traffic")
|
| 956 |
+
return []
|
| 957 |
+
|
| 958 |
try:
|
| 959 |
|
| 960 |
sql = """
|
|
|
|
| 979 |
def get_top_plates():
|
| 980 |
"""Get top detected plates with timeout protection"""
|
| 981 |
|
| 982 |
+
if engine is None:
|
| 983 |
+
print("⚠️ Database not available for get_top_plates")
|
| 984 |
+
return []
|
| 985 |
+
|
| 986 |
try:
|
| 987 |
|
| 988 |
sql = """
|
|
|
|
| 1135 |
|
| 1136 |
def get_vehicle_density_by_location():
|
| 1137 |
"""Get traffic density (vehicle count) by location"""
|
| 1138 |
+
if engine is None:
|
| 1139 |
+
print("⚠️ Database not available for get_vehicle_density_by_location")
|
| 1140 |
+
return []
|
| 1141 |
try:
|
| 1142 |
sql = """
|
| 1143 |
SELECT location, COUNT(*) as vehicle_count,
|