def neo4j_available(): try: logger.info("Checking connection at {}".format( neo4j_connection.database.uri)) neo4j_connection.run('MATCH () RETURN 1 LIMIT 1') return True, "neo4j ok" except AttributeError: return False, "neo4j connection not found"
def get_agent_subgraph(id, sort_by=None, order=None, limit=None): # noqa: E501 """Get subgraph connected to an agent Retrieve the nodes and relationships in a neighborhood around a specified user. # noqa: E501 :param id: user ID :type id: 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 :rtype: D3Graph """ query_base = (''' MATCH (t:Agent {{user_id: {{id}}}})<-[r:WASASSOCIATEDWITH]-(s:Activity) WITH s ORDER BY s.{key}{dir} 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(key=sort_by, dir=(' ' + order.upper()) if order == 'desc' else '', lim=limit) results = graph.run(query_base, id=id) return neo4j_to_d3(results.data())
def get_activities_graph(sort_by=None, order=None, limit=None): # noqa: E501 """Get provenance graph Retrieve all nodes and relationships in the graph that pass filters. # noqa: E501 :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 :rtype: D3Graph """ query_base = (''' MATCH (s:Activity) WITH s ORDER BY s.{key}{dir} 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(key=sort_by, dir=(' ' + order.upper()) if order == 'desc' else '', lim=limit) results = graph.run(query_base, ) return neo4j_to_d3(results.data())
def add_activities(kt): result = graph.run(''' MATCH (:Activity) RETURN count(*) as count ''') offset = result.data()[0]['count'] x = [] for i in range(kt): tmp = MockActivity(name='Activity_' + str(i + 1 + offset), class_idx=0) tmp.select_class(randrange(tmp.get_class_count())) x.append(tmp) time.sleep(0.5) return x
def _find_activity(self): activities = graph.run(''' WITH {e_ids} as refs MATCH (r:Reference)-[:WASGENERATEDBY]->(act:Activity) WHERE r.target_id in refs WITH act, size(refs) as inputCnt, count(DISTINCT r) as cnt WHERE cnt = inputCnt RETURN act ''', e_ids=[e.target_id for e in self.generated]).data() if len(activities) > 1: raise ValueError elif len(activities): return activities[0]['act']
def get_reference_subgraph(id, direction='down', sort_by='created_at', order='desc', limit=3): # noqa: E501 """Get subgraph connected to an entity Retrieve the nodes and relationships in a neighborhood around a specified entity. # noqa: E501 :param id: entity ID :type 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 :rtype: D3Graph """ direction_rels = {'up': '-[r:WASGENERATEDBY]->', 'down': '<-[r:USED]-'} query_base = (''' MATCH (t:Reference {{target_id: {{id}}}}){dir_rel}(s:Activity) WITH s ORDER BY s.{key}{dir} 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(dir_rel=direction_rels[direction], key=sort_by, dir=(' ' + order.upper()) if order == 'desc' else '', lim=limit) results = graph.run(query_base, id=id) return neo4j_to_d3(results.data())