"""Service for recalculating the global status of a Technique based on the state and result of its associated tests.""" from sqlalchemy.orm import Session from app.models.enums import TechniqueStatus from app.models.technique import Technique def recalculate_technique_status(db: Session, technique: Technique) -> None: """Recompute ``technique.status_global`` from its tests and commit. Rules ----- - No tests → ``not_evaluated`` - Any test not yet ``validated`` → ``in_progress`` - All validated and all ``detected`` → ``validated`` - All validated and any ``partially_detected`` → ``partial`` - Otherwise → ``not_covered`` """ tests = technique.tests if not tests: technique.status_global = TechniqueStatus.not_evaluated elif any(t.state != "validated" for t in tests): technique.status_global = TechniqueStatus.in_progress else: results = [t.result for t in tests] if all(r == "detected" for r in results): technique.status_global = TechniqueStatus.validated elif any(r == "partially_detected" for r in results): technique.status_global = TechniqueStatus.partial else: technique.status_global = TechniqueStatus.not_covered db.commit()