67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
# [DEF:backend.src.services.mapping_service:Module]
|
|
#
|
|
# @SEMANTICS: service, mapping, fuzzy-matching, superset
|
|
# @PURPOSE: Orchestrates database fetching and fuzzy matching suggestions.
|
|
# @LAYER: Service
|
|
# @RELATION: DEPENDS_ON -> backend.src.core.superset_client
|
|
# @RELATION: DEPENDS_ON -> backend.src.core.utils.matching
|
|
#
|
|
# @INVARIANT: Suggestions are based on database names.
|
|
|
|
# [SECTION: IMPORTS]
|
|
from typing import List, Dict
|
|
from backend.src.core.superset_client import SupersetClient
|
|
from backend.src.core.utils.matching import suggest_mappings
|
|
from superset_tool.models import SupersetConfig
|
|
# [/SECTION]
|
|
|
|
# [DEF:MappingService:Class]
|
|
# @PURPOSE: Service for handling database mapping logic.
|
|
class MappingService:
|
|
|
|
# [DEF:MappingService.__init__:Function]
|
|
def __init__(self, config_manager):
|
|
self.config_manager = config_manager
|
|
|
|
# [DEF:MappingService._get_client:Function]
|
|
# @PURPOSE: Helper to get an initialized SupersetClient for an environment.
|
|
def _get_client(self, env_id: str) -> SupersetClient:
|
|
envs = self.config_manager.get_environments()
|
|
env = next((e for e in envs if e.id == env_id), None)
|
|
if not env:
|
|
raise ValueError(f"Environment {env_id} not found")
|
|
|
|
superset_config = SupersetConfig(
|
|
env=env.name,
|
|
base_url=env.url,
|
|
auth={
|
|
"provider": "db",
|
|
"username": env.username,
|
|
"password": env.password,
|
|
"refresh": "false"
|
|
}
|
|
)
|
|
return SupersetClient(superset_config)
|
|
|
|
# [DEF:MappingService.get_suggestions:Function]
|
|
# @PURPOSE: Fetches databases from both environments and returns fuzzy matching suggestions.
|
|
# @PARAM: source_env_id (str) - Source environment ID.
|
|
# @PARAM: target_env_id (str) - Target environment ID.
|
|
# @RETURN: List[Dict] - Suggested mappings.
|
|
async def get_suggestions(self, source_env_id: str, target_env_id: str) -> List[Dict]:
|
|
"""
|
|
Get suggested mappings between two environments.
|
|
"""
|
|
source_client = self._get_client(source_env_id)
|
|
target_client = self._get_client(target_env_id)
|
|
|
|
source_dbs = source_client.get_databases_summary()
|
|
target_dbs = target_client.get_databases_summary()
|
|
|
|
return suggest_mappings(source_dbs, target_dbs)
|
|
# [/DEF:MappingService.get_suggestions]
|
|
|
|
# [/DEF:MappingService]
|
|
|
|
# [/DEF:backend.src.services.mapping_service]
|