Skip to content

Commit

Permalink
Merge 9b5bee4 into 8b90e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeddy committed Oct 16, 2019
2 parents 8b90e2e + 9b5bee4 commit b4d00e1
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
38 changes: 38 additions & 0 deletions synprov/controllers/activities_controller.py
@@ -1,6 +1,7 @@
import connexion
import six

from synprov.models import Activity # noqa: E501
from synprov.models import ActivityForm # noqa: E501
from synprov.models import Neo4jGraph # noqa: E501
from synprov.models import Node # noqa: E501
Expand Down Expand Up @@ -153,6 +154,43 @@ def get_agent_subgraph(
)


def get_reference_activities(
target_id,
direction='down',
sort_by='created_at',
order='desc',
limit=3,
q='*:*'
): # noqa: E501
"""Get subgraph connected to an entity
Retrieve the Activity objects in a neighborhood around a specified entity. # noqa: E501
:param target_id: entity ID
:type target_id: str
:param direction: direction in which to collect connected activities
:type direction: str
:param sort_by: logic by which to sort matched activities
:type sort_by: str
:param order: sort order (ascending or descending)
:type order: str
:param limit: maximum number of connected activities to return
:type limit: int
:param q: filter results using Lucene Query Syntax in the format of propertyName:value, propertyName:[num1 TO num2] and date range format, propertyName:[yyyyMMdd TO yyyyMMdd]
:type q: str
:rtype: List[Activity]
"""
return controller.get_reference_activities(
target_id=target_id,
direction=direction,
sort_by=sort_by,
order=order,
limit=limit,
q=q
)


def get_reference_subgraph(
target_id,
direction='down',
Expand Down
70 changes: 67 additions & 3 deletions synprov/graph/controllers/activities_controller.py
Expand Up @@ -11,7 +11,11 @@
from synprov.models.activity import Activity
from synprov.config import neo4j_connection as graph
from synprov.graph import ActivityBuilder, ActivityEditor
from synprov.util import neo4j_to_d3, neo4j_export, convert_keys, quote_string
from synprov.util import (neo4j_to_d3,
neo4j_export,
convert_keys,
quote_string,
node_to_dict)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,7 +141,6 @@ def get_activities_graph(
key = ATTR_MAP[tree.name]
val = quote_string(tree.children[0].value)
filter_properties = f' {{{key}: {val}}}'
logger.info(f"FILTER PROPS: {filter_properties}")

query_base = (
'''
Expand Down Expand Up @@ -192,7 +195,6 @@ def get_agent_subgraph(
key = ATTR_MAP[tree.name]
val = quote_string(tree.children[0].value)
filter_properties = f' {{{key}: {val}}}'
logger.info(f"FILTER PROPS: {filter_properties}")

query_base = (
'''
Expand All @@ -218,6 +220,68 @@ def get_agent_subgraph(
return convert_keys(neo4j_export(results.data()))


def get_reference_activities(
target_id,
direction='down',
sort_by='created_at',
order='desc',
limit=3,
q='*:*'
): # noqa: E501
"""Get subgraph connected to an entity
Retrieve the Activity objects in a neighborhood around a specified entity. # noqa: E501
:param target_id: entity ID
:type target_id: str
:param direction: direction in which to collect connected activities
:type direction: str
:param sort_by: logic by which to sort matched activities
:type sort_by: str
:param order: sort order (ascending or descending)
:type order: str
:param limit: maximum number of connected activities to return
:type limit: int
:param q: filter results using Lucene Query Syntax in the format of propertyName:value, propertyName:[num1 TO num2] and date range format, propertyName:[yyyyMMdd TO yyyyMMdd]
:type q: str
:rtype: List[Activity]
"""
filter_properties = ''
if q != '*:*':
tree = parser.parse(q)
key = ATTR_MAP[tree.name]
val = quote_string(tree.children[0].value)
filter_properties = f' {{{key}: {val}}}'

direction_rels = {
'up': '-[r:WASGENERATEDBY]->',
'down': '<-[r:USED]-'
}
query_base = (
'''
MATCH (t:Reference {{target_id: {{target_id}}}}){dir_rel}(s:Activity{filter})
WITH s
ORDER BY s.{key}{dir}
WITH collect(s) as activities
UNWIND activities[0..{lim}] as a
RETURN a as activity
'''
).format(
dir_rel=direction_rels[direction],
key=sort_by,
dir=(' ' + order.upper()) if order == 'desc' else '',
lim=limit,
filter=filter_properties
)

results = graph.run(
query_base,
target_id=target_id
)
return list(map(lambda a: node_to_dict(a['activity']), results.data()))


def get_reference_subgraph(
target_id,
direction='down',
Expand Down
35 changes: 35 additions & 0 deletions synprov/openapi/openapi.yaml
Expand Up @@ -197,6 +197,33 @@ paths:
tags:
- Activities
x-openapi-router-controller: synprov.controllers.activities_controller
/activities/byReference/{targetId}:
get:
description: |
Retrieve the Activity objects in a neighborhood around a specified entity.
operationId: get_reference_activities
parameters:
- description: entity ID
explode: false
in: path
name: targetId
required: true
schema:
type: string
example: resource0
style: simple
- $ref: '#/components/parameters/directionParam'
- $ref: '#/components/parameters/sortParam'
- $ref: '#/components/parameters/orderParam'
- $ref: '#/components/parameters/limitParam'
- $ref: '#/components/parameters/filterParam'
responses:
200:
$ref: '#/components/responses/200ActivitiesFound'
summary: Get subgraph connected to an entity
tags:
- Activities
x-openapi-router-controller: synprov.controllers.activities_controller
components:
examples:
activityNode:
Expand Down Expand Up @@ -323,6 +350,14 @@ components:
# oneOf:
# - $ref: '#/components/schemas/D3Graph'
$ref: '#/components/schemas/Neo4jGraph'
200ActivitiesFound:
description: Found requested Activities.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Activity'
404NotFound:
description: The specified resource was not found.
parameters:
Expand Down
4 changes: 4 additions & 0 deletions synprov/util.py
Expand Up @@ -247,6 +247,10 @@ def _convert_relationship(neo4j_rel):
}


def node_to_dict(neo4j_node):
return convert_keys(dict(neo4j_node))


def is_open(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
Expand Down

0 comments on commit b4d00e1

Please sign in to comment.