Esempio n. 1
0
def get_match_contents(coll_scope,
                       coll_name,
                       scope,
                       name,
                       min_id=None,
                       max_id=None,
                       request_id=None,
                       workload_id=None,
                       relation_type=None,
                       only_return_best_match=False,
                       session=None):
    """
    Get matched contents with collection scope, collection name, scope, name, min_id, max_id,
    request id, workload id and only_return_best_match.

    :param coll_scope: scope of the collection.
    :param coll_name: name the the collection.
    :param scope: scope of the content.
    :param name: name of the content.
    :param min_id: min_id of the content.
    :param max_id: max_id of the content.
    :param request_id: the request id.
    :param workload_id: The workload_id of the request.
    :param only_return_best_match: only return best matched content if it's true.
    :param session: The database session in use.

    :returns: list of contents
    """

    if (request_id is None and
            workload_id is None) or coll_scope is None or coll_name is None:
        msg = "Only one of (request_id, workload_id) can be None. All other parameters should not be None: "
        msg += "request_id=%s, workload_id=%s, coll_scope=%s, coll_name=%s" % (
            request_id, workload_id, coll_scope, coll_name)
        raise exceptions.WrongParameterException(msg)

    coll_id = orm_collections.get_collection_id_by_scope_name(coll_scope,
                                                              coll_name,
                                                              request_id,
                                                              relation_type,
                                                              session=session)
    contents = orm_contents.get_match_contents(coll_id=coll_id,
                                               scope=scope,
                                               name=name,
                                               min_id=min_id,
                                               max_id=max_id,
                                               session=session)

    if not only_return_best_match:
        return contents

    if len(contents) == 1:
        return contents

    content = None
    for row in contents:
        if (not content) or (content['max_id'] - content['min_id'] >
                             row['max_id'] - row['min_id']):
            content = row
    return [content]
Esempio n. 2
0
def get_match_contents(coll_scope, coll_name, scope, name, min_id=None, max_id=None,
                       request_id=None, workload_id=None, relation_type=None,
                       only_return_best_match=False, to_json=False, session=None):
    """
    Get matched contents with collection scope, collection name, scope, name, min_id, max_id,
    request id, workload id and only_return_best_match.

    :param coll_scope: scope of the collection.
    :param coll_name: name the the collection.
    :param scope: scope of the content.
    :param name: name of the content.
    :param min_id: min_id of the content.
    :param max_id: max_id of the content.
    :param request_id: the request id.
    :param workload_id: The workload_id of the request.
    :param only_return_best_match: only return best matched content if it's true.
    :param session: The database session in use.

    :returns: list of contents
    """
    transform_ids = orm_transforms.get_transform_ids(request_id=request_id,
                                                     workload_id=workload_id,
                                                     session=session)

    if transform_ids:
        collections = orm_collections.get_collections(scope=coll_scope, name=coll_name, transform_id=transform_ids,
                                                      relation_type=relation_type, session=session)
    else:
        collections = []

    coll_def = "request_id=%s, workload_id=%s, coll_scope=%s" % (request_id, workload_id, coll_scope)
    coll_def += ", coll_name=%s, relation_type: %s" % (coll_name, relation_type)

    if len(collections) != 1:
        msg = "There should be only one collection matched. However there are %s collections" % len(collections)
        msg += coll_def
        raise exceptions.WrongParameterException(msg)

    coll_id = collections[0]['coll_id']

    contents = orm_contents.get_match_contents(coll_id=coll_id, scope=scope, name=name, min_id=min_id, max_id=max_id,
                                               to_json=to_json, session=session)

    if not only_return_best_match:
        return contents

    if len(contents) == 1:
        return contents

    content = None
    for row in contents:
        if (not content) or (content['max_id'] - content['min_id'] > row['max_id'] - row['min_id']):
            content = row
    return [content]
    def test_contents_orm(self):
        """ Contents (ORM): Test contents """

        req_properties = get_request_properties()
        trans_properties = get_transform_properties()
        coll_properties = get_collection_properties()
        content_properties = get_content_properties()

        request_id = add_request(**req_properties)
        trans_properties['request_id'] = request_id

        trans_id = add_transform(**trans_properties)

        coll_properties['transform_id'] = trans_id
        coll_id = add_collection(**coll_properties)

        content_properties['coll_id'] = coll_id
        origin_content_id = add_content(**content_properties)
        content_properties1 = copy.deepcopy(content_properties)
        content_properties1['min_id'] = 101
        content_properties1['max_id'] = 200
        origin_content_id1 = add_content(**content_properties1)
        content_properties2 = copy.deepcopy(content_properties)
        content_properties2['min_id'] = 0
        content_properties2['max_id'] = 200
        origin_content_id2 = add_content(**content_properties2)
        content_properties3 = copy.deepcopy(content_properties)
        content_properties3['name'] = content_properties3['name'] + '_1'
        origin_content_id3 = add_content(**content_properties3)
        origin_content_ids = [
            origin_content_id, origin_content_id1, origin_content_id2,
            origin_content_id3
        ]

        contents = get_contents(coll_id=coll_id)
        assert_equal(len(contents), 4)
        for content in contents:
            assert_in(content['content_id'], origin_content_ids)

        contents = get_contents(scope=content_properties['scope'],
                                name=content_properties['name'],
                                coll_id=coll_id)
        assert_equal(len(contents), 3)
        for content in contents:
            assert_in(content['content_id'], origin_content_ids)

        contents = get_contents(scope=content_properties3['scope'],
                                name=content_properties3['name'],
                                coll_id=coll_id)
        assert_equal(len(contents), 1)
        assert_equal(contents[0]['content_id'], origin_content_id3)

        contents = get_match_contents(coll_id=content_properties['coll_id'],
                                      scope=content_properties['scope'],
                                      name=content_properties['name'],
                                      min_id=content_properties['min_id'],
                                      max_id=content_properties['max_id'])
        assert_equal(len(contents), 2)
        for content in contents:
            assert_in(content['content_id'],
                      [origin_content_id, origin_content_id2])

        to_updates = [{
            'path': 'test_path1',
            'status': ContentStatus.Processing,
            'content_id': origin_content_id
        }, {
            'path': 'test_path2',
            'status': ContentStatus.Processing,
            'content_id': origin_content_id1
        }]
        update_contents(to_updates)
        content = get_content(content_id=origin_content_id)
        assert_equal(content['status'], ContentStatus.Processing)
        assert_equal(content['path'], 'test_path1')
        content = get_content(content_id=origin_content_id1)
        assert_equal(content['status'], ContentStatus.Processing)
        assert_equal(content['path'], 'test_path2')