Beispiel #1
0
def paths(id):
    collection = obj_or_404(Collection.by_id(id))
    authz.require(authz.collection_read(collection.id))
    start_entity_id = request.args.get('entity_id')
    labels = request.args.getlist('label')
    types = request.args.getlist('type')
    collection_id = request.args.getlist('collection_id')
    end_collection_id = authz.collections_intersect(authz.READ, collection_id)
    q = Path.find(collection, start_entity_id=start_entity_id, labels=labels,
                  types=types, end_collection_id=end_collection_id)
    data = Pager(q, id=collection.id).to_dict()
    data['facets'] = Path.facets(collection, start_entity_id=start_entity_id,
                                 labels=labels, types=types,
                                 end_collection_id=end_collection_id,
                                 collection_id=authz.collections(authz.READ))
    return jsonify(data)
Beispiel #2
0
def generate_paths(graph, entity, ignore_types=SKIP_TYPES):
    """Generate all possible paths which end in a different collection."""
    Path.delete_by_entity(entity.id)
    if graph is None or entity.state != entity.STATE_ACTIVE:
        return
    log.info("Generating graph path cache: %r", entity)
    # TODO: should max path length be configurable?
    q = "MATCH pth = (start:Aleph:Entity)-[*1..3]-(end:Aleph:Entity) " \
        "MATCH (start)-[startpart:PART_OF]->(startcoll:Collection) " \
        "MATCH (end)-[endpart:PART_OF]->(endcoll:Collection) " \
        "WHERE start.fingerprint = {entity_fp} AND " \
        "startpart.alephCanonical = {entity_id} AND " \
        "startcoll.alephCollection <> endcoll.alephCollection AND " \
        "all(r IN relationships(pth) WHERE NOT type(r) IN {ignore_types}) " \
        "WITH DISTINCT start, end, " \
        " COLLECT(DISTINCT extract(x IN nodes(pth) | x.id)) AS paths, " \
        " COLLECT(DISTINCT extract(x IN nodes(pth) | labels(x))) AS labels, " \
        " COLLECT(DISTINCT extract(r IN relationships(pth) | type(r))) AS types, " \
        " COLLECT(DISTINCT endcoll.alephCollection) AS end_collection_id " \
        "RETURN start, end, paths, types, labels, end_collection_id "
    count = 0
    for row in graph.run(q,
                         entity_id=entity.id,
                         entity_fp=entity.fingerprint,
                         ignore_types=ignore_types):
        labels = unwind(row.get('labels'))
        labels = [l for l in labels if l != BASE_NODE]
        types = unwind(row.get('types'))
        if len(types) == 1 and 'AKA' in types:
            continue
        Path.from_data(entity, row.get('end_collection_id'), row.get('paths'),
                       types, labels, NodeType.dict(row.get('start')),
                       NodeType.dict(row.get('end')))
        count += 1
    db.session.commit()
    # TODO: send email to collection owners?
    log.info("Generated %s paths for %r", count, entity)
Beispiel #3
0
def delete_paths(entity_id):
    """Delete the paths based on this entity."""
    Path.delete_by_entity(entity_id)
    db.session.commit()