From deed94b346bf6bf381cf845c10e07dc41b2ff118 Mon Sep 17 00:00:00 2001 From: Enya-Yx Date: Tue, 16 Aug 2022 19:31:37 +0800 Subject: [PATCH] Optimize purview query for getting features --- registry/purview-registry/main.py | 14 ++--------- .../registry/purview_registry.py | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/registry/purview-registry/main.py b/registry/purview-registry/main.py index 24bd117bb..5818cd513 100644 --- a/registry/purview-registry/main.py +++ b/registry/purview-registry/main.py @@ -64,18 +64,8 @@ def get_project_datasources(project: str) -> list: @router.get("/projects/{project}/features",tags=["Project"]) def get_project_features(project: str, keyword: Optional[str] = None) -> list: - if keyword is None or keyword.strip()=='': - p = registry.get_entity(project,True) - feature_ids = [s.id for s in p.attributes.anchor_features] + \ - [s.id for s in p.attributes.derived_features] - features = registry.get_entities(feature_ids,True) - return list([to_camel(e.to_dict()) for e in features]) - else: - efs = registry.search_entity( - keyword, [EntityType.AnchorFeature, EntityType.DerivedFeature],project=project) - feature_ids = [ef.id for ef in efs] - features = registry.get_entities(feature_ids) - return list([to_camel(e.to_dict()) for e in features]) + atlasEntities = registry.get_project_features(project, keywords=keyword) + return list([to_camel(e.to_dict()) for e in atlasEntities]) @router.get("/features/{feature}",tags=["Feature"]) diff --git a/registry/purview-registry/registry/purview_registry.py b/registry/purview-registry/registry/purview_registry.py index 3e78ece80..3436acca3 100644 --- a/registry/purview-registry/registry/purview_registry.py +++ b/registry/purview-registry/registry/purview_registry.py @@ -189,13 +189,30 @@ def _create_edge_from_process(self, name:str, guid: str) -> Edge: names = name.split(self.registry_delimiter) return Edge(guid, names[1], names[2], RelationshipType.new(names[0])) + def get_project_features(self, project:str, keywords:Optional[str] = None) -> list[Entity]: + project_id = self.get_entity_id(project) + if not project_id: + return None + guidAtlasEntityMap = self.purview_client.get_entity_lineage(project_id, depth=1, width=1, direction="OUTPUT")['guidEntityMap'] + atlasEntities = [] + + for entity in guidAtlasEntityMap.values(): + type = entity['typeName'] + if type != "Process": + type = EntityType.new(type) + if type == EntityType.AnchorFeature or type == EntityType.DerivedFeature: + attributes = entity['attributes'] + if not keywords or (attributes and keywords in attributes['qualifiedName']): + atlasEntity = self._atlasEntity_to_entity(entity) + atlasEntities.append(atlasEntity) + return atlasEntities + def get_project(self, id_or_name: Union[str, UUID]) -> EntitiesAndRelations: project_id = self.get_entity_id(id_or_name) if not project_id: return None lineage = self.purview_client.get_entity_lineage(project_id) guidAtlasEntityMap = lineage['guidEntityMap'] - guidEntityMap = {} finalGuidEntityMap = {} edges = [] @@ -249,7 +266,7 @@ def search_entity(self, Search entities with specified type that also match the keyword in a project """ query_result = self.purview_client.search_entities(keyword) - result = [] + result = [] for entity in query_result: qualified_name = entity["qualifiedName"] entity_id = entity['id'] @@ -592,4 +609,5 @@ def _get_id_by_qualfiedName(self, qualifiedName): for entity in entities: if entity.get('qualifiedName') == qualifiedName: return entity.get('id') - \ No newline at end of file + +