Exemple #1
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    if args.db is None:
        with app.app_context():
            db = app.config["DATABASE"]
    else:
        db = args.db
    con = get_con(db)
    
    treatment = args.treatment

    if args.output_dir is None:
        output_dir = get_output_dir(treatment)
    else:
        output_dir = args.output_dir

    export(treatment, con, output_dir)
Exemple #2
0
def _process_judgments(signal,
                       payload,
                       job_id,
                       job_config,
                       treatment,
                       auto_finalize=False):
    """
    :param signal: (str)
    :param payload: (dict)
    :param job_id: (int|str)
    :param job_config: (JobConfig)
    :param auto_finalize (bool)
    """
    error_happened = False
    app.logger.debug(
        f"_process_judgments: {signal}, job_id: {job_id}, auto_finalize: {auto_finalize}"
    )
    with app.app_context():
        try:
            if signal == "new_judgments":
                judgments_count = payload['judgments_count']
                fig8 = FigureEight(job_id, job_config["api_key"])
                for idx in range(judgments_count):
                    if auto_finalize == True:
                        try:
                            con = get_db("RESULT")
                            worker_judgment = payload['results']['judgments'][
                                idx]
                            worker_id = worker_judgment["worker_id"]
                            app.logger.debug(
                                f"_process_judgments: {signal}, job_id: {job_id}, worker_id: {worker_id}"
                            )
                            is_responder = False
                            is_proposer = False
                            table_resp = get_table(resp_BASE,
                                                   job_id=job_id,
                                                   schema="result",
                                                   treatment=treatment)
                            table_prop = get_table(prop_BASE,
                                                   job_id=job_id,
                                                   schema="result",
                                                   treatment=treatment)
                            with con:
                                if table_exists(con, table_resp):
                                    res = con.execute(
                                        f"SELECT * from {table_resp} WHERE job_id=? and worker_id=?",
                                        (job_id, worker_id)).fetchone()
                                    if res:
                                        is_responder = True
                                if not is_responder and table_exists(
                                        con, table_prop):
                                    res = con.execute(
                                        f"SELECT * from {table_prop} WHERE job_id=? and worker_id=?",
                                        (job_id, worker_id)).fetchone()
                                    if res:
                                        is_proposer = True
                            if is_responder:
                                finalize_resp(job_id=job_id,
                                              worker_id=worker_id,
                                              treatment=treatment)
                            elif is_proposer:
                                finalize_round(job_id=job_id,
                                               prop_worker_id=worker_id,
                                               treatment=treatment)
                            else:
                                app.logger.error(
                                    f"Error: unknown worker_id: {worker_id} for job_id: {job_id}"
                                )
                        except Exception as err:
                            if not error_happened:
                                app.log_exception(err)
                                error_happened = True
                    else:
                        worker_judgment = payload['results']['judgments'][idx]
                        worker_id = worker_judgment["worker_id"]
                        pay_worker_bonus(job_id, worker_id, fig8)

            elif signal == "unit_complete":
                judgments_count = payload['judgments_count']
                fig8 = FigureEight(job_id, job_config["api_key"])
                for idx in range(judgments_count):
                    if auto_finalize == False:
                        worker_judgment = payload['results']['judgments'][idx]
                        worker_id = worker_judgment["worker_id"]
                        # PAY_WORKER won't pay someone twice.
                        pay_worker_bonus(job_id, worker_id, fig8)

                #TODO: may process the whole unit here
                pass
        except Exception as err:
            app.log_exception(err)
    app.logger.debug(f"_process_judgments: {signal}, job_id: {job_id} - done")
Exemple #3
0
from survey._app import app, csrf_protect, CODE_DIR
from survey.txx.prop import handle_check, handle_done, handle_index, insert_row, handle_index_dss, get_row_ignore_job, handle_feedback, create_prop_data_table
from survey.globals import AI_SYSTEM_DESCRIPTION_BRIEF_PROPOSER, AI_SYSTEM_DESCRIPTION_EXTENDED_PROPOSER, AI_SYSTEM_DESCRIPTION_USAGE_PROPOSER

############ Consts #################################
TREATMENT = os.path.split(os.path.split(__file__)[0])[1]
BASE = os.path.splitext(os.path.split(__file__)[1])[0]

MODEL_INFOS_KEY = f"{TREATMENT.upper()}_MODEL_INFOS"
MODEL_KEY = f"{TREATMENT.upper()}_MODEL"

bp = Blueprint(f"{TREATMENT}.{BASE}", __name__)

REF = "t11c"

with app.app_context():
    create_prop_data_table(TREATMENT, REF)
######################################################



############# HELPERS   ###########################

@csrf_protect.exempt
@bp.route("/prop/", methods=["GET", "POST"])
def index():
    messages = []
    return handle_index(TREATMENT, messages=messages, get_row_func=get_row_ignore_job)

@bp.route("/prop_dss/", methods=["GET", "POST"])
def index_dss():
Exemple #4
0
def emit_webhook(client,
                 url,
                 job_id="test",
                 worker_id=None,
                 signal="new_judgments",
                 unit_state="finalized",
                 treatment="t10",
                 by_get=True):
    """
    :param client:
    :param url: (str) relative path to target api
    :param job_id: (str)
    :param worker_id: (str)
    :param signal: (new_judgments|unit_complete)
    :param unit_state: (finalized|new|judging|judgeable?)
    :param by_get: (True|False) simulate a setup were each user triggers the webhook using a get request
    """
    app.logger.debug(
        f"emit_webhook: job_id: {job_id}, worker_id: {worker_id}, treatment: {treatment}"
    )
    proceed = False
    max_retries = 5
    with app.app_context():
        while not proceed and max_retries > 0:
            table_resp = get_table("resp",
                                   job_id,
                                   "result",
                                   treatment=treatment)
            table_prop = get_table("prop",
                                   job_id,
                                   "result",
                                   treatment=treatment)
            app.logger.debug("Waiting for the db...")

            with get_db() as con:
                if con.execute(f"SELECT * FROM {table_resp} where worker_id=?",
                               (worker_id, )).fetchone():
                    proceed = True
                elif con.execute(
                        f"SELECT * FROM {table_prop} where worker_id=?",
                    (worker_id, )).fetchone():
                    proceed = True
            con = None
            time.sleep(0.01)
            max_retries -= 1
        data_dict = dict(webhook_data_template)
        data_dict["signal"] = signal
        data_dict["payload"]["job_id"] = job_id
        data_dict["payload"]["results"]["judgments"][0]["job_id"] = job_id
        data_dict["payload"]["results"]["judgments"][0][
            "worker_id"] = worker_id
        data_dict["payload"]["results"]["judgments"][0][
            "unit_state"] = unit_state
        with get_db() as con:
            job_config = get_job_config(con, job_id)
        payload = json.dumps(data_dict["payload"])
        payload_ext = payload + str(job_config["api_key"])
        signature = hashlib.sha1(payload_ext.encode()).hexdigest()
        data = {"signal": signal, "payload": payload, "signature": signature}
        if by_get:
            # An empty form is submitted when triggering the webhook by click
            data = {}
            if "?" in url:
                url += f"&job_id={job_id}&worker_id={worker_id}"
            else:
                url += f"?job_id={job_id}&worker_id={worker_id}"
            res = client.get(url, follow_redirects=True).status
        else:
            res = client.post(url, data=data, follow_redirects=True).status
        return res