Skip to content

Commit

Permalink
add basic controllers for ref/agt subgraph endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeddy committed Jul 19, 2019
1 parent 8915938 commit fd8809e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
25 changes: 23 additions & 2 deletions synprov/graph/controllers/agents_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

from synprov.config import neo4j_connection as graph
from synprov.graph import GraphActivity, GraphReference, GraphAgent
from synprov.util import neo4j_to_d3


def get_agent_subgraph(id, limit=None): # noqa: E501
def get_agent_subgraph(id, limit=3, direction='down'): # noqa: E501
"""Get subgraph connected to an agent.
Retrieve all nodes and relationships in the graph that pass filters. # noqa: E501
Expand All @@ -19,4 +20,24 @@ def get_agent_subgraph(id, limit=None): # noqa: E501
:rtype: Graph
"""
return 'Not Implemented', 501
direction_rels = {
'down': '<-[r:WASASSOCIATEDWITH]-'
}
query_base = (
'''
MATCH (t:Agent {{user_id: {{id}}}}){dir_rel}(s:Activity)
WITH collect(s) as activities
UNWIND activities[0..{lim}] as t
MATCH (s)-[r]-(t)
RETURN s as source,r as relationship,t as target
'''
).format(
lim=limit,
dir_rel=direction_rels[direction]
)

results = graph.run(
query_base,
id=id
)
return neo4j_to_d3(results.data())
26 changes: 24 additions & 2 deletions synprov/graph/controllers/references_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

from synprov.config import neo4j_connection as graph
from synprov.graph import GraphActivity, GraphReference, GraphAgent
from synprov.util import neo4j_to_d3


def get_reference_subgraph(id, limit=None, direction=None): # noqa: E501
def get_reference_subgraph(id, limit=3, direction='down'): # noqa: E501
"""Get subgraph connected to an entity.
Retrieve the nodes and relationships in a neighborhood around a specified entity. # noqa: E501
Expand All @@ -19,4 +20,25 @@ def get_reference_subgraph(id, limit=None, direction=None): # noqa: E501
:rtype: Graph
"""
return 'Not Implemented', 501
direction_rels = {
'up': '-[r:WASGENERATEDBY]->',
'down': '<-[r:USED]-'
}
query_base = (
'''
MATCH (t:Reference {{target_id: {{id}}}}){dir_rel}(s:Activity)
WITH collect(s) as activities
UNWIND activities[0..{lim}] as t
MATCH (s)-[r]-(t)
RETURN s as source,r as relationship,t as target
'''
).format(
lim=limit,
dir_rel=direction_rels[direction]
)

results = graph.run(
query_base,
id=id
)
return neo4j_to_d3(results.data())

0 comments on commit fd8809e

Please sign in to comment.