예제 #1
0
파일: models.py 프로젝트: wguanicedew/ESSV2
class Request(BASE, ModelBase):
    """Represents a pre-cache request from other service"""
    __tablename__ = 'ess_requests'
    request_id = Column(BigInteger, Sequence('ESS_REQUEST_ID_SEQ'))
    scope = Column(String(SCOPE_LENGTH))
    name = Column(String(NAME_LENGTH))
    data_type = Column(DataType.db_type(name='ESS_REQUESTS_DATA_TYPE'),
                       default=DataType.DATASET)
    granularity_type = Column(
        GranularityType.db_type(name='ESS_REQUESTS_GRANULARITY_TYPE'),
        default=GranularityType.PARTIAL)
    granularity_level = Column(Integer())
    priority = Column(Integer())
    edge_id = Column(Integer)
    status = Column(RequestStatus.db_type(name='ESS_REQUESTS_STATUS'),
                    default=RequestStatus.NEW)
    request_meta = Column(JSON())  # task id, job id, pandq queues inside
    processing_meta = Column(JSON())  # collection_id or file_id inside
    errors = Column(JSON())
    _table_args = (PrimaryKeyConstraint('request_id', name='ESS_REQUESTS_PK'),
                   ForeignKeyConstraint(['edge_id'], ['ess_edges.edge_id'],
                                        name='ESS_REQUESTS_EDGE_ID_FK'),
                   CheckConstraint('status IS NOT NULL',
                                   name='ESS_REQ_STATUS_ID_NN'),
                   Index('ESS_REQUESTS_SCOPE_NAME_IDX', 'scope', 'name',
                         'data_type', 'request_id'),
                   Index('ESS_REQUESTS_STATUS_PRIORITY_IDX', 'status',
                         'priority', 'request_id'))
예제 #2
0
def add_request(scope, name, data_type=DataType.DATASET, granularity_type=GranularityType.FILE,
                granularity_level=None, priority=0, edge_id=None, status=RequestStatus.NEW,
                request_meta=None, processing_meta=None, errors=None, session=None):
    """
    Add a request.

    :param scope: The scope of the request data.
    :param name: The name of the request data.
    :param data_type: The type of the request data.
    :param granularity_type: The granularity type, for example File or Partial.
    :param granularity_level: The granularity level, for example, number of events.
    :param priority: The priority as an integer.
    :param edge_id: The id of the assigned edge.
    :param status: The status of the request.
    :param request_meta: The metadata of the request, as Json.
    :param processing_meta: The processing metadata, as Json.
    :param errors: The processing errors, as Json.
    :param session: The database session in use.

    :raises DuplicatedObject: If an request with the same name exists.
    :raises DatabaseException: If there is a database error.

    :returns: request id.
    """
    if isinstance(data_type, str) or isinstance(data_type, unicode):
        data_type = DataType.from_sym(str(data_type))

    if isinstance(granularity_type, str) or isinstance(granularity_type, unicode):
        granularity_type = GranularityType.from_sym(str(granularity_type))

    if isinstance(status, str) or isinstance(status, unicode):
        status = RequestStatus.from_sym(str(status))

    new_request = models.Request(scope=scope, name=name, data_type=data_type, granularity_type=granularity_type,
                                 granularity_level=granularity_level, priority=priority, edge_id=edge_id, status=status,
                                 request_meta=request_meta, processing_meta=processing_meta, errors=errors)

    try:
        new_request.save(session=session)
    except IntegrityError as error:
        raise exceptions.DuplicatedObject('Request %s:%s already exists!: %s' % (scope, name, error))
    except DatabaseError as error:
        raise exceptions.DatabaseException(error.args)

    return new_request.request_id
예제 #3
0
def get_requests(status=None, edge_name=None, edge_id=None, session=None):
    """
    Get requests.

    :param status: The status of the request data.
    :param edge_name: The name of the edge.
    :param edge_id: The id of the edge
    :param session: The database session in use.

    :raises NoObject: If no request is founded.

    :returns: Request models.
    """

    try:
        if edge_name and not edge_id:
            edge_id = get_edge_id(edge_name)

        if status:
            if (isinstance(status, str) or isinstance(status, unicode)):
                status = RequestStatus.from_sym(status)

            if edge_id:
                requests = session.query(models.Request).filter_by(
                    status=status, edge_id=edge_id).all()
            else:
                requests = session.query(
                    models.Request).filter_by(status=status).all()
        else:
            if edge_id:
                requests = session.query(
                    models.Request).filter_by(edge_id=edge_id).all()
            else:
                requests = session.query(models.Request).all()

        for request in requests:
            request['data_type'] = request.data_type
            request['granularity_type'] = request.granularity_type
            request['status'] = request.status
        return requests
    except sqlalchemy.orm.exc.NoResultFound:
        raise exceptions.NoObject('Cannot find request with status: %s' %
                                  (status))
예제 #4
0
def update_request(request_id, parameters, session=None):
    """
    update an request.

    :param request_id: the request id.
    :param parameters: A dictionary of parameters.
    :param session: The database session in use.

    :raises NoObject: If no request is founded.
    :raises DatabaseException: If there is a database error.

    :returns: request id.
    """
    try:
        if 'data_type' in parameters and \
           (isinstance(parameters['data_type'], str) or isinstance(parameters['data_type'], unicode)):
            parameters['data_type'] = DataType.from_sym(
                str(parameters['data_type']))

        if 'granularity_type' in parameters and \
           (isinstance(parameters['granularity_type'], str) or isinstance(parameters['granularity_type'], unicode)):
            parameters['granularity_type'] = GranularityType.from_sym(
                str(parameters['granularity_type']))

        if 'status' in parameters and \
           (isinstance(parameters['status'], str) or isinstance(parameters['status'], unicode)):
            parameters['status'] = RequestStatus.from_sym(
                str(parameters['status']))

        request = session.query(
            models.Request).filter_by(request_id=request_id).one()
    except sqlalchemy.orm.exc.NoResultFound:
        raise exceptions.NoObject('Request %s cannot be found' % request_id)

    try:
        request.update(parameters)
    except DatabaseError as error:
        raise exceptions.DatabaseException(error.args)

    return request.request_id