Skip to content

Commit

Permalink
Merge pull request #614 from sennetconsortium/libpitt/594-prov
Browse files Browse the repository at this point in the history
Libpitt/594 prov
  • Loading branch information
maxsibilla authored Feb 12, 2025
2 parents b9478bc + abc655b commit a41cdd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 5 additions & 7 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,23 +807,21 @@ def get_entity_provenance(id):
if 'depth' in request.args:
depth = int(request.args.get('depth'))

authorized = user_in_sennet_read_group(request)
data_access_level = 'public' if authorized is False else None

# Convert neo4j json to dict
neo4j_result = app_neo4j_queries.get_provenance(neo4j_driver_instance, uuid, depth)
neo4j_result = app_neo4j_queries.get_provenance(neo4j_driver_instance, uuid, depth, data_access_level=data_access_level)
raw_provenance_dict = dict(neo4j_result['json'])

raw_descendants_dict = None
if bool(request.args):
# The parsed query string value is a string 'true'
return_descendants = request.args.get('return_descendants')

# The value should be in a format expected by the apoc.path.subgraphAll.labelFilter config param
label_filter = request.args.get('filter', '')
allowable_filter_chars = "[a-zA-Z+/>\-|]"
label_filter = ''.join(re.findall(allowable_filter_chars, label_filter))

if (return_descendants is not None) and (return_descendants.lower() == 'true'):
neo4j_result_descendants = app_neo4j_queries.get_provenance(neo4j_driver_instance, uuid, depth, True,
label_filter)
data_access_level=data_access_level)
raw_descendants_dict = dict(neo4j_result_descendants['json'])

# Normalize the raw provenance nodes based on the yaml schema
Expand Down
16 changes: 10 additions & 6 deletions src/app_neo4j_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ def add_entities_to_collection(neo4j_driver, collection_uuid, entitiy_uuids_list
"""


def get_provenance(neo4j_driver, uuid, depth, return_descendants=None, query_filter=None):
def get_provenance(neo4j_driver, uuid, depth, return_descendants=None, data_access_level=None):
# max_level_str is the string used to put a limit on the number of levels to traverse
max_level_str = ''
if depth is not None and len(str(depth)) > 0:
Expand All @@ -1475,14 +1475,18 @@ def get_provenance(neo4j_driver, uuid, depth, return_descendants=None, query_fil
if return_descendants:
relationship_filter = '<USED|<WAS_GENERATED_BY'

label_filter = ''
if query_filter is not None and len(query_filter) > 0:
label_filter = f", labelFilter:'{query_filter}'"
predicate = ''
allow_nodes = ''
if data_access_level:
allow_nodes = ', allowlistNodes: allowlistNodes'
predicate = ("MATCH(allowedEntity:Entity)-[:WAS_GENERATED_BY]->(allowedActivity:Activity) "
f"WHERE (allowedEntity.data_access_level = '{data_access_level}' OR allowedEntity.status = 'Published') "
"WITH n, collect(allowedEntity)+collect(allowedActivity) AS allowlistNodes ")

# More info on apoc.path.subgraphAll() procedure: https://neo4j.com/labs/apoc/4.0/graph-querying/expand-subgraph/
query = (f"MATCH (n:Entity) "
f"WHERE n.uuid = '{uuid}' "
f"CALL apoc.path.subgraphAll(n, {{ {max_level_str} relationshipFilter:'{relationship_filter}' {label_filter} }}) "
f"WHERE n.uuid = '{uuid}' {predicate} "
f"CALL apoc.path.subgraphAll(n, {{ {max_level_str} relationshipFilter:'{relationship_filter}' {allow_nodes} }}) "
f"YIELD nodes, relationships "
f"WITH [node in nodes | node {{ .*, label:labels(node)[0] }} ] as nodes, "
f"[rel in relationships | rel {{ .*, fromNode: {{ label:labels(startNode(rel))[0], uuid:startNode(rel).uuid }}, toNode: {{ label:labels(endNode(rel))[0], uuid:endNode(rel).uuid }}, rel_data: {{ type: type(rel) }} }} ] as rels "
Expand Down

0 comments on commit a41cdd9

Please sign in to comment.