Add user delete process

This commit is contained in:
Urtzi Alfaro
2025-10-31 11:54:19 +01:00
parent 63f5c6d512
commit 269d3b5032
74 changed files with 16783 additions and 213 deletions

View File

@@ -16,7 +16,7 @@ from shared.monitoring.decorators import track_execution_time
from shared.monitoring.metrics import get_metrics_collector
from shared.database.base import create_database_manager
from shared.auth.decorators import get_current_user_dep
from shared.auth.access_control import require_user_role, admin_role_required
from shared.auth.access_control import require_user_role, admin_role_required, service_only_access
from shared.security import create_audit_logger, create_rate_limiter, AuditSeverity, AuditAction
from shared.subscription.plans import (
get_training_job_quota,
@@ -503,3 +503,126 @@ async def health_check():
],
"timestamp": datetime.now().isoformat()
}
# ============================================================================
# Tenant Data Deletion Operations (Internal Service Only)
# ============================================================================
@router.delete(
route_builder.build_base_route("tenant/{tenant_id}", include_tenant_prefix=False),
response_model=dict
)
@service_only_access
async def delete_tenant_data(
tenant_id: str = Path(..., description="Tenant ID to delete data for"),
current_user: dict = Depends(get_current_user_dep)
):
"""
Delete all training data for a tenant (Internal service only)
This endpoint is called by the orchestrator during tenant deletion.
It permanently deletes all training-related data including:
- Trained models (all versions)
- Model artifacts (files and metadata)
- Training logs and job history
- Model performance metrics
- Training job queue entries
- Audit logs
**WARNING**: This operation is irreversible!
**NOTE**: Physical model files (.pkl) should be cleaned up separately
Returns:
Deletion summary with counts of deleted records
"""
from app.services.tenant_deletion_service import TrainingTenantDeletionService
from app.core.config import settings
try:
logger.info("training.tenant_deletion.api_called", tenant_id=tenant_id)
db_manager = create_database_manager(settings.DATABASE_URL, "training")
async with db_manager.get_session() as session:
deletion_service = TrainingTenantDeletionService(session)
result = await deletion_service.safe_delete_tenant_data(tenant_id)
if not result.success:
raise HTTPException(
status_code=500,
detail=f"Tenant data deletion failed: {', '.join(result.errors)}"
)
return {
"message": "Tenant data deletion completed successfully",
"note": "Physical model files should be cleaned up separately from storage",
"summary": result.to_dict()
}
except HTTPException:
raise
except Exception as e:
logger.error("training.tenant_deletion.api_error",
tenant_id=tenant_id,
error=str(e),
exc_info=True)
raise HTTPException(
status_code=500,
detail=f"Failed to delete tenant data: {str(e)}"
)
@router.get(
route_builder.build_base_route("tenant/{tenant_id}/deletion-preview", include_tenant_prefix=False),
response_model=dict
)
@service_only_access
async def preview_tenant_data_deletion(
tenant_id: str = Path(..., description="Tenant ID to preview deletion for"),
current_user: dict = Depends(get_current_user_dep)
):
"""
Preview what data would be deleted for a tenant (dry-run)
This endpoint shows counts of all data that would be deleted
without actually deleting anything. Useful for:
- Confirming deletion scope before execution
- Auditing and compliance
- Troubleshooting
Returns:
Dictionary with entity names and their counts
"""
from app.services.tenant_deletion_service import TrainingTenantDeletionService
from app.core.config import settings
try:
logger.info("training.tenant_deletion.preview_called", tenant_id=tenant_id)
db_manager = create_database_manager(settings.DATABASE_URL, "training")
async with db_manager.get_session() as session:
deletion_service = TrainingTenantDeletionService(session)
preview = await deletion_service.get_tenant_data_preview(tenant_id)
total_records = sum(preview.values())
return {
"tenant_id": tenant_id,
"service": "training",
"preview": preview,
"total_records": total_records,
"note": "Physical model files (.pkl, metadata) are not counted here",
"warning": "These records will be permanently deleted and cannot be recovered"
}
except Exception as e:
logger.error("training.tenant_deletion.preview_error",
tenant_id=tenant_id,
error=str(e),
exc_info=True)
raise HTTPException(
status_code=500,
detail=f"Failed to preview tenant data deletion: {str(e)}"
)

View File

@@ -0,0 +1,292 @@
# services/training/app/services/tenant_deletion_service.py
"""
Tenant Data Deletion Service for Training Service
Handles deletion of all training-related data for a tenant
"""
from typing import Dict
from sqlalchemy import select, func, delete
from sqlalchemy.ext.asyncio import AsyncSession
import structlog
from shared.services.tenant_deletion import (
BaseTenantDataDeletionService,
TenantDataDeletionResult
)
from app.models import (
TrainedModel,
ModelTrainingLog,
ModelPerformanceMetric,
TrainingJobQueue,
ModelArtifact,
AuditLog
)
logger = structlog.get_logger(__name__)
class TrainingTenantDeletionService(BaseTenantDataDeletionService):
"""Service for deleting all training-related data for a tenant"""
def __init__(self, db: AsyncSession):
self.db = db
self.service_name = "training"
async def get_tenant_data_preview(self, tenant_id: str) -> Dict[str, int]:
"""
Get counts of what would be deleted for a tenant (dry-run)
Args:
tenant_id: The tenant ID to preview deletion for
Returns:
Dictionary with entity names and their counts
"""
logger.info("training.tenant_deletion.preview", tenant_id=tenant_id)
preview = {}
try:
# Count trained models
model_count = await self.db.scalar(
select(func.count(TrainedModel.id)).where(
TrainedModel.tenant_id == tenant_id
)
)
preview["trained_models"] = model_count or 0
# Count model artifacts
artifact_count = await self.db.scalar(
select(func.count(ModelArtifact.id)).where(
ModelArtifact.tenant_id == tenant_id
)
)
preview["model_artifacts"] = artifact_count or 0
# Count training logs
log_count = await self.db.scalar(
select(func.count(ModelTrainingLog.id)).where(
ModelTrainingLog.tenant_id == tenant_id
)
)
preview["model_training_logs"] = log_count or 0
# Count performance metrics
metric_count = await self.db.scalar(
select(func.count(ModelPerformanceMetric.id)).where(
ModelPerformanceMetric.tenant_id == tenant_id
)
)
preview["model_performance_metrics"] = metric_count or 0
# Count training job queue entries
queue_count = await self.db.scalar(
select(func.count(TrainingJobQueue.id)).where(
TrainingJobQueue.tenant_id == tenant_id
)
)
preview["training_job_queue"] = queue_count or 0
# Count audit logs
audit_count = await self.db.scalar(
select(func.count(AuditLog.id)).where(
AuditLog.tenant_id == tenant_id
)
)
preview["audit_logs"] = audit_count or 0
logger.info(
"training.tenant_deletion.preview_complete",
tenant_id=tenant_id,
preview=preview
)
except Exception as e:
logger.error(
"training.tenant_deletion.preview_error",
tenant_id=tenant_id,
error=str(e),
exc_info=True
)
raise
return preview
async def delete_tenant_data(self, tenant_id: str) -> TenantDataDeletionResult:
"""
Permanently delete all training data for a tenant
Deletion order:
1. ModelArtifact (references models)
2. ModelPerformanceMetric (references models)
3. ModelTrainingLog (independent job logs)
4. TrainingJobQueue (independent queue entries)
5. TrainedModel (parent model records)
6. AuditLog (independent)
Note: This also deletes physical model files from disk/storage
Args:
tenant_id: The tenant ID to delete data for
Returns:
TenantDataDeletionResult with deletion counts and any errors
"""
logger.info("training.tenant_deletion.started", tenant_id=tenant_id)
result = TenantDataDeletionResult(tenant_id=tenant_id, service_name=self.service_name)
try:
# Step 1: Delete model artifacts (references models)
logger.info("training.tenant_deletion.deleting_artifacts", tenant_id=tenant_id)
# TODO: Delete physical files from storage before deleting DB records
# artifacts = await self.db.execute(
# select(ModelArtifact).where(ModelArtifact.tenant_id == tenant_id)
# )
# for artifact in artifacts.scalars():
# try:
# os.remove(artifact.file_path) # Delete physical file
# except Exception as e:
# logger.warning("Failed to delete artifact file",
# path=artifact.file_path, error=str(e))
artifacts_result = await self.db.execute(
delete(ModelArtifact).where(
ModelArtifact.tenant_id == tenant_id
)
)
result.deleted_counts["model_artifacts"] = artifacts_result.rowcount
logger.info(
"training.tenant_deletion.artifacts_deleted",
tenant_id=tenant_id,
count=artifacts_result.rowcount
)
# Step 2: Delete model performance metrics
logger.info("training.tenant_deletion.deleting_metrics", tenant_id=tenant_id)
metrics_result = await self.db.execute(
delete(ModelPerformanceMetric).where(
ModelPerformanceMetric.tenant_id == tenant_id
)
)
result.deleted_counts["model_performance_metrics"] = metrics_result.rowcount
logger.info(
"training.tenant_deletion.metrics_deleted",
tenant_id=tenant_id,
count=metrics_result.rowcount
)
# Step 3: Delete training logs
logger.info("training.tenant_deletion.deleting_logs", tenant_id=tenant_id)
logs_result = await self.db.execute(
delete(ModelTrainingLog).where(
ModelTrainingLog.tenant_id == tenant_id
)
)
result.deleted_counts["model_training_logs"] = logs_result.rowcount
logger.info(
"training.tenant_deletion.logs_deleted",
tenant_id=tenant_id,
count=logs_result.rowcount
)
# Step 4: Delete training job queue entries
logger.info("training.tenant_deletion.deleting_queue", tenant_id=tenant_id)
queue_result = await self.db.execute(
delete(TrainingJobQueue).where(
TrainingJobQueue.tenant_id == tenant_id
)
)
result.deleted_counts["training_job_queue"] = queue_result.rowcount
logger.info(
"training.tenant_deletion.queue_deleted",
tenant_id=tenant_id,
count=queue_result.rowcount
)
# Step 5: Delete trained models (parent records)
logger.info("training.tenant_deletion.deleting_models", tenant_id=tenant_id)
# TODO: Delete physical model files (.pkl) before deleting DB records
# models = await self.db.execute(
# select(TrainedModel).where(TrainedModel.tenant_id == tenant_id)
# )
# for model in models.scalars():
# try:
# if model.model_path:
# os.remove(model.model_path) # Delete .pkl file
# if model.metadata_path:
# os.remove(model.metadata_path) # Delete metadata file
# except Exception as e:
# logger.warning("Failed to delete model file",
# path=model.model_path, error=str(e))
models_result = await self.db.execute(
delete(TrainedModel).where(
TrainedModel.tenant_id == tenant_id
)
)
result.deleted_counts["trained_models"] = models_result.rowcount
logger.info(
"training.tenant_deletion.models_deleted",
tenant_id=tenant_id,
count=models_result.rowcount
)
# Step 6: Delete audit logs
logger.info("training.tenant_deletion.deleting_audit_logs", tenant_id=tenant_id)
audit_result = await self.db.execute(
delete(AuditLog).where(
AuditLog.tenant_id == tenant_id
)
)
result.deleted_counts["audit_logs"] = audit_result.rowcount
logger.info(
"training.tenant_deletion.audit_logs_deleted",
tenant_id=tenant_id,
count=audit_result.rowcount
)
# Commit the transaction
await self.db.commit()
# Calculate total deleted
total_deleted = sum(result.deleted_counts.values())
logger.info(
"training.tenant_deletion.completed",
tenant_id=tenant_id,
total_deleted=total_deleted,
breakdown=result.deleted_counts,
note="Physical model files should be cleaned up separately"
)
result.success = True
except Exception as e:
await self.db.rollback()
error_msg = f"Failed to delete training data for tenant {tenant_id}: {str(e)}"
logger.error(
"training.tenant_deletion.failed",
tenant_id=tenant_id,
error=str(e),
exc_info=True
)
result.errors.append(error_msg)
result.success = False
return result
def get_training_tenant_deletion_service(
db: AsyncSession
) -> TrainingTenantDeletionService:
"""
Factory function to create TrainingTenantDeletionService instance
Args:
db: AsyncSession database session
Returns:
TrainingTenantDeletionService instance
"""
return TrainingTenantDeletionService(db)