Ejemplo n.º 1
0
    def get_matrix():
        identifying_names = Controller.get_arg_list('identifying_name')
        dataset = Controller.get_arg('dataset')

        neo4j_requester = Neo4jRequester(dataset)
        relations = neo4j_requester.get_matrix_for_identifying_names(
            identifying_names)
        community_count = neo4j_requester.get_feature_count('community')
        role_count = neo4j_requester.get_feature_count('role')

        matrix = Matrix.build_matrix(relations, community_count, role_count)

        return matrix
    def search_correspondents():
        dataset = Controller.get_arg('dataset')

        search_phrase = Controller.get_arg('search_phrase')
        match_exact = Controller.get_arg('match_exact', arg_type=bool, default=False, required=False)
        offset = Controller.get_arg('offset', arg_type=int, default=0, required=False)
        limit = Controller.get_arg('limit', arg_type=int, default=10, required=False)
        search_fields = Controller.get_arg_list(
            'search_field', default=['identifying_name'], required=False
        )

        allowed_search_field_values = {'identifying_name', 'email_addresses', 'aliases'}
        if not set(search_fields).issubset(allowed_search_field_values):
            raise Exception('Allowed values for arg search_fields are ' + str(allowed_search_field_values))

        neo4j_requester = Neo4jRequester(dataset)
        results, numFound = neo4j_requester.get_correspondents_for_search_phrase(
            search_phrase, match_exact, search_fields, offset, limit
        )
        return {
            'results': [dict(result) for result in default_network_analysis(results)],
            'numFound': numFound
        }
    def get_graph():
        dataset = Controller.get_arg('dataset')
        is_correspondent_view = Controller.get_arg('is_correspondent_view',
                                                   required=False)
        identifying_names = Controller.get_arg_list('identifying_name')
        neo4j_requester = Neo4jRequester(dataset)
        start_date = Controller.get_arg('start_date', required=False)
        start_stamp = time.mktime(
            datetime.datetime.strptime(
                start_date, '%Y-%m-%d').timetuple()) if start_date else 0
        end_date = Controller.get_arg('end_date', required=False)
        end_stamp = time.mktime(
            datetime.datetime.strptime(
                end_date,
                '%Y-%m-%d').timetuple()) if end_date else time.time()

        graph = {'nodes': [], 'links': []}
        visited_nodes = []

        for node in neo4j_requester.get_nodes_for_identifying_names(
                identifying_names):
            if not node['id'] in visited_nodes:
                visited_nodes.append(node['id'])
                graph['nodes'].append(
                    build_node(node['id'], node['identifying_name']))

            if is_correspondent_view == 'true':
                for neighbour in neo4j_requester.get_neighbours_for_node(
                        node['id'], start_stamp, end_stamp):
                    if not neighbour['id'] in visited_nodes:
                        visited_nodes.append(neighbour['id'])
                        graph['nodes'].append(
                            build_node(neighbour['id'],
                                       neighbour['identifying_name']))

            for relation in neo4j_requester.get_relations_for_nodes(
                    visited_nodes, start_stamp, end_stamp):
                graph['links'].append(
                    build_edge(relation['relation_id'], relation['source_id'],
                               relation['target_id']))

        # add hops to connect lonely nodes with other nodes in graph
        if is_correspondent_view == 'false':  # in correspondent view, no nodes without links should appear
            nodes = list(graph['nodes'])
            links = list(graph['links'])
            for node in nodes:
                has_links = False
                for link in links:
                    if (node['id'] == link['source']) or (node['id']
                                                          == link['target']):
                        if link['source'] != link['target']:
                            has_links = True

                if has_links:
                    continue
                other_nodes = list(visited_nodes)
                other_nodes.remove(node['id'])
                for hop in neo4j_requester.get_path_between_nodes(
                        node['id'], other_nodes):
                    if not hop['hop_id'] in visited_nodes:
                        visited_nodes.append(hop['hop_id'])
                        graph['nodes'].append(
                            build_node(hop['hop_id'],
                                       hop['hop_identifying_name']))
                        graph['links'].append(
                            build_edge(hop['r1_id'], hop['source_id'],
                                       hop['hop_id']))
                        graph['links'].append(
                            build_edge(hop['r2_id'], hop['hop_id'],
                                       hop['target_id']))

        return graph