from __future__ import annotations import re from sqlmodel import Session, select from ..db.models import App _APPOPS_ROLLBACK_ID = re.compile( r'^unraid_appops_gateway(?:_1216)?_pre_\d{8}t\d{6}z$', re.IGNORECASE, ) def is_appops_deployment_container_name(value: str | None) -> bool: key = str(value or '').strip().lower().replace('_', '-') return key in {'unraid-appops-gateway', 'unraid-appops-gateway-1216'} or bool( re.fullmatch(r'unraid-appops-gateway(?:-1216)?-pre-\d{8}t\d{6}z', key) ) def is_historical_app_record(app: App) -> bool: """Keep deployment history out of active product views without deleting it.""" return bool(_APPOPS_ROLLBACK_ID.fullmatch(str(app.id or '').strip())) def category_label(value: str | None) -> str: words = str(value or 'Unknown').strip().replace('_', ' ').replace('-', ' ').title() replacements = {'Ai': 'AI', 'Api': 'API', 'It': 'IT'} return ' '.join(replacements.get(word, word) for word in words.split()) or 'Unknown' def list_active_apps(session: Session, *, order_by: str = 'name') -> list[App]: apps = [app for app in session.exec(select(App)).all() if not is_historical_app_record(app)] # A port-specific canonical record supersedes the old unqualified 1211 record. if any(app.id == 'unraid_appops_gateway_1216' for app in apps): apps = [app for app in apps if app.id != 'unraid_appops_gateway'] key = (lambda app: app.id.lower()) if order_by == 'id' else (lambda app: app.name.lower()) return sorted(apps, key=key)