Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

base_trace_instance.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from abc import ABC, abstractmethod
  2. from sqlalchemy import select
  3. from sqlalchemy.orm import Session
  4. from core.ops.entities.config_entity import BaseTracingConfig
  5. from core.ops.entities.trace_entity import BaseTraceInfo
  6. from extensions.ext_database import db
  7. from models import Account, App, TenantAccountJoin
  8. class BaseTraceInstance(ABC):
  9. """
  10. Base trace instance for ops trace services
  11. """
  12. @abstractmethod
  13. def __init__(self, trace_config: BaseTracingConfig):
  14. """
  15. Abstract initializer for the trace instance.
  16. Distribute trace tasks by matching entities
  17. """
  18. self.trace_config = trace_config
  19. @abstractmethod
  20. def trace(self, trace_info: BaseTraceInfo):
  21. """
  22. Abstract method to trace activities.
  23. Subclasses must implement specific tracing logic for activities.
  24. """
  25. ...
  26. def get_service_account_with_tenant(self, app_id: str) -> Account:
  27. """
  28. Get service account for an app and set up its tenant.
  29. Args:
  30. app_id: The ID of the app
  31. Returns:
  32. Account: The service account with tenant set up
  33. Raises:
  34. ValueError: If app, creator account or tenant cannot be found
  35. """
  36. with Session(db.engine, expire_on_commit=False) as session:
  37. # Get the app to find its creator
  38. app_stmt = select(App).where(App.id == app_id)
  39. app = session.scalar(app_stmt)
  40. if not app:
  41. raise ValueError(f"App with id {app_id} not found")
  42. if not app.created_by:
  43. raise ValueError(f"App with id {app_id} has no creator (created_by is None)")
  44. account_stmt = select(Account).where(Account.id == app.created_by)
  45. service_account = session.scalar(account_stmt)
  46. if not service_account:
  47. raise ValueError(f"Creator account with id {app.created_by} not found for app {app_id}")
  48. current_tenant = (
  49. session.query(TenantAccountJoin).filter_by(account_id=service_account.id, current=True).first()
  50. )
  51. if not current_tenant:
  52. raise ValueError(f"Current tenant not found for account {service_account.id}")
  53. service_account.set_tenant_id(current_tenant.tenant_id)
  54. return service_account