Esempio n. 1
0
def is_dataprovider_allowed_to_upload_and_lock(db, dp_id):
    """
    This method returns true if the data provider is allowed to upload their encodings.

    A dataprovider is not allowed to upload clks if they has already uploaded them, or if the upload is in progress.
    This method will lock the resource by setting the upload state to `in_progress` and returning `true`.
    Note that the upload state can be `error`, in which case we allow the dataprovider to re-try uploading
    her clks not to block a project if a failure occurred.
    """
    logger.debug("Setting dataprovider {} upload state to `in_progress``".format(dp_id))
    sql_update = """
        UPDATE dataproviders
        SET uploaded = 'in_progress'
        WHERE id = %s and uploaded != 'done' and uploaded != 'in_progress'
        RETURNING id, uploaded
        """
    query_response = query_db(db, sql_update, [dp_id])
    length = len(query_response)
    if length < 1:
        return False
    elif length > 1:
        logger.error("{} rows in the table `dataproviders` are associated to the same dataprovider id {}, while each"
                     " dataprovider id should be unique.".format(length, dp_id))
        raise ValueError("Houston, we have a problem!!! This dataprovider has uploaded multiple times")
    return True
Esempio n. 2
0
def set_dataprovider_upload_state(db, dp_id, state='error'):
    logger.debug("Setting dataprovider {} upload state to {}".format(dp_id, state))
    sql_update = """
        UPDATE dataproviders
        SET uploaded = %s
        WHERE id = %s
        """

    with db.cursor() as cur:
        cur.execute(sql_update, [state, dp_id])
Esempio n. 3
0
def set_project_encoding_size(db, project_id, encoding_size):
    sql_query = """
        UPDATE projects
        SET
          encoding_size = %s
        WHERE
          project_id = %s
        """
    logger.debug("Updating database with project encoding size")
    with db.cursor() as cur:
        cur.execute(sql_query, [encoding_size, project_id])
Esempio n. 4
0
def select_dataprovider_id(db, project_id, receipt_token):
    """
    Given a receipt token get the id of the matching data provider.

    Returns None if token is incorrect.
    """
    sql_query = """
        SELECT dp from dataproviders, uploads
        WHERE
          uploads.dp = dataproviders.id AND
          dataproviders.project = %s AND
          uploads.token = %s
        """
    query_result = query_db(db,
                            sql_query, [project_id, receipt_token],
                            one=True)
    logger.debug("Looking up data provider with auth. {}".format(query_result))
    return query_result['dp'] if query_result is not None else None