fix: defensive evaluation import to prevent startup crash (#15)
All checks were successful
Deploy Development / deploy (push) Successful in 49s
Build Test / lint-backend (push) Successful in 1s
Build Test / build-frontend (push) Successful in 13s

Problem: Backend crashed on startup due to evaluation import failure
Solution: Wrap evaluation_helper import in try/except

Changes:
- Import evaluation_helper with error handling
- Add EVALUATION_AVAILABLE flag
- All evaluation calls now check flag before executing
- System remains functional even if evaluation system unavailable

This prevents backend crashes if:
- Migrations haven't run yet
- Dependencies are missing
- Import errors occur

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-23 10:59:23 +01:00
parent e11953736d
commit edd15dd556

View File

@ -16,7 +16,15 @@ from auth import require_auth, check_feature_access, increment_feature_usage
from models import ActivityEntry from models import ActivityEntry
from routers.profiles import get_pid from routers.profiles import get_pid
from feature_logger import log_feature_usage from feature_logger import log_feature_usage
from evaluation_helper import evaluate_and_save_activity
# Evaluation import with error handling (Phase 1.2)
try:
from evaluation_helper import evaluate_and_save_activity
EVALUATION_AVAILABLE = True
except Exception as e:
logger.warning(f"[AUTO-EVAL] Evaluation system not available: {e}")
EVALUATION_AVAILABLE = False
evaluate_and_save_activity = None
router = APIRouter(prefix="/api/activity", tags=["activity"]) router = APIRouter(prefix="/api/activity", tags=["activity"])
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -66,6 +74,7 @@ def create_activity(e: ActivityEntry, x_profile_id: Optional[str]=Header(default
d['rpe'],d['source'],d['notes'])) d['rpe'],d['source'],d['notes']))
# Phase 1.2: Auto-evaluation after INSERT # Phase 1.2: Auto-evaluation after INSERT
if EVALUATION_AVAILABLE:
# Load the activity data to evaluate # Load the activity data to evaluate
cur.execute(""" cur.execute("""
SELECT id, profile_id, date, training_type_id, duration_min, SELECT id, profile_id, date, training_type_id, duration_min,
@ -102,6 +111,7 @@ def update_activity(eid: str, e: ActivityEntry, x_profile_id: Optional[str]=Head
list(d.values())+[eid,pid]) list(d.values())+[eid,pid])
# Phase 1.2: Auto-evaluation after UPDATE # Phase 1.2: Auto-evaluation after UPDATE
if EVALUATION_AVAILABLE:
# Load the updated activity data to evaluate # Load the updated activity data to evaluate
cur.execute(""" cur.execute("""
SELECT id, profile_id, date, training_type_id, duration_min, SELECT id, profile_id, date, training_type_id, duration_min,
@ -257,6 +267,7 @@ def bulk_categorize_activities(
updated_count = cur.rowcount updated_count = cur.rowcount
# Phase 1.2: Auto-evaluation after bulk categorization # Phase 1.2: Auto-evaluation after bulk categorization
if EVALUATION_AVAILABLE:
# Load all activities that were just updated and evaluate them # Load all activities that were just updated and evaluate them
cur.execute(""" cur.execute("""
SELECT id, profile_id, date, training_type_id, duration_min, SELECT id, profile_id, date, training_type_id, duration_min,
@ -369,7 +380,7 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
skipped += 1 # Count as skipped (not newly inserted) skipped += 1 # Count as skipped (not newly inserted)
# Phase 1.2: Auto-evaluation after CSV import UPDATE # Phase 1.2: Auto-evaluation after CSV import UPDATE
if training_type_id: if EVALUATION_AVAILABLE and training_type_id:
try: try:
# Build activity dict for evaluation # Build activity dict for evaluation
activity_dict = { activity_dict = {
@ -408,7 +419,7 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
inserted+=1 inserted+=1
# Phase 1.2: Auto-evaluation after CSV import INSERT # Phase 1.2: Auto-evaluation after CSV import INSERT
if training_type_id: if EVALUATION_AVAILABLE and training_type_id:
try: try:
# Build activity dict for evaluation # Build activity dict for evaluation
activity_dict = { activity_dict = {