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.2KB

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