def test_index_auto(client, treatment, prefix=""): #takes the role assigned by the system and solves the corresponding tasks client = None job_id = "test_auto" for _ in range(TASK_REPETITION): auto_worker_id = generate_worker_id(f"{prefix}index_auto") path = f"/{treatment}/?worker_id={auto_worker_id}&job_id={job_id}" with app.test_request_context(path): client = get_client() res = client.get(path, follow_redirects=True) time.sleep(0.001) # Play as responder # print(res.data) if b"role of a RESPONDER" in res.data: # res = _process_resp(client, treatment, job_id=job_id, worker_id=auto_worker_id, min_offer=MIN_OFFER) # process_tasks(client, job_id=job_id, worker_id=auto_worker_id, bonus_mode="full", url_kwargs={"auto_finalize": 1, "treatment": treatment}) res = _process_resp_tasks(client, treatment, job_id=job_id, worker_id=auto_worker_id, min_offer=get_min_offer()) assert b"resp:" in res.data assert is_worker_available(auto_worker_id, get_table("resp", job_id=job_id, schema="result")) # Play a proposer elif b"role of a PROPOSER" in res.data: app.logger.warning("PROPOSER") res = client.get(path, follow_redirects=True) res = _process_prop_round(client, treatment, job_id=job_id, worker_id=auto_worker_id, offer=get_offer(), response_available=True) assert b"prop:" in res.data assert is_worker_available(auto_worker_id, get_table("resp", job_id=job_id, schema="result")) else: assert False, "Nooooooooooooooo" time.sleep(WEBHOOK_DELAY)
def get_previous_worker_code(job_id, worker_id, treatment): """ Generate a code for the user in case he already took the main task """ resp_table = get_table(resp_BASE, job_id=job_id, schema="result", treatment=treatment) prop_table = get_table(prop_BASE, job_id=job_id, schema="result", treatment=treatment) worker_code = None if is_worker_available(worker_id, resp_table): worker_code = get_db().execute( f"SELECT completion_code from {resp_table} where worker_id=?", [worker_id]).fetchone()[0] if not worker_code: worker_code = generate_completion_code(resp_BASE, job_id) if is_worker_available(worker_id, prop_table): worker_code = get_db().execute( f"SELECT completion_code from {prop_table} where worker_id=?", [worker_id]).fetchone()[0] if not worker_code: worker_code = generate_completion_code(prop_BASE, job_id) return worker_code
def test_webhook(client, treatment, resp_feedback_fields=None): # In real conditions, the tasks/webhook are delayed with +500 ms job_id = "test" resp_worker_id = generate_worker_id("webhook_resp") prop_worker_id = generate_worker_id("webhook_prop") process_tasks(client, job_id=job_id, worker_id=resp_worker_id, bonus_mode="random") _process_resp(client, treatment, job_id=job_id, worker_id=resp_worker_id, min_offer=get_min_offer(), resp_feedback_fields=resp_feedback_fields) emit_webhook(client, url=f"/{treatment}/webhook/", treatment=treatment, worker_id=resp_worker_id, by_get=True) time.sleep(WEBHOOK_DELAY) _process_prop(client, treatment, job_id=job_id, worker_id=prop_worker_id, offer=get_offer(), response_available=True) emit_webhook(client, url=f"/{treatment}/webhook/", treatment=treatment, worker_id=prop_worker_id, by_get=True) time.sleep(WEBHOOK_DELAY) with app.app_context(): assert is_worker_available( resp_worker_id, get_table("resp", job_id=job_id, schema="result", treatment=treatment)) assert is_worker_available( prop_worker_id, get_table("prop", job_id=job_id, schema="result", treatment=treatment))
def test_eff(client): with app.app_context(): job_id = "test" worker_id = generate_worker_id("task") res = _process_eff(client, job_id=job_id, worker_id=worker_id) assert b"eff:" in res assert is_worker_available(worker_id, get_table("eff", job_id, "result")) assert has_worker_and_features("eff", eff, job_id, worker_id)
def test_auto_finalize(client, treatment, resp_feedback_fields=None): # Test automatic webhook triggering to finalize tasks job_id = "test" resp_worker_id = generate_worker_id("auto_resp") prop_worker_id = generate_worker_id("auto_prop") _process_resp(client, treatment, job_id=job_id, worker_id=resp_worker_id, min_offer=get_min_offer(), resp_feedback_fields=resp_feedback_fields) process_tasks(client, job_id=job_id, worker_id=resp_worker_id, bonus_mode="random", url_kwargs={ "auto_finalize": 1, "treatment": treatment }) time.sleep(WEBHOOK_DELAY) res = _process_prop(client, treatment, job_id=job_id, worker_id=prop_worker_id, offer=get_offer(), response_available=True, auto_finalize=True) time.sleep(WEBHOOK_DELAY) with app.app_context(): assert is_worker_available( resp_worker_id, get_table("resp", job_id=job_id, schema="result", treatment=treatment)) assert is_worker_available( prop_worker_id, get_table("prop", job_id=job_id, schema="result", treatment=treatment))
def test_auto_finalize(client, treatment, resp_feedback_fields=None): # Test automatic webhook triggering to finalize tasks job_id = "test" resp_worker_id = generate_worker_id("auto_resp") _process_resp(client, treatment, job_id=job_id, worker_id=resp_worker_id, min_offer=get_min_offer(), resp_feedback_fields=resp_feedback_fields) process_tasks(client, job_id=job_id, worker_id=resp_worker_id, bonus_mode="random", url_kwargs={"auto_finalize": 1, "treatment": treatment}) time.sleep(WEBHOOK_DELAY) with app.app_context(): # let the auto-responder kick-in for _ in range(NB_MAX_AUTO_PLAY_RETRIES): if is_resp_in_prop_result(resp_worker_id, job_id, treatment): break time.sleep(AUTO_PROP_DELAY) assert is_worker_available(resp_worker_id, get_table("resp", job_id=job_id, schema="result", treatment=treatment)) assert is_resp_in_prop_result(resp_worker_id, job_id, treatment)
def test_webhook(client, treatment, resp_feedback_fields=None): # In real conditions, the tasks/webhook are delayed with +500 ms job_id = "test" resp_worker_id = generate_worker_id("webhook_resp") process_tasks(client, job_id=job_id, worker_id=resp_worker_id, bonus_mode="random") _process_resp(client, treatment, job_id=job_id, worker_id=resp_worker_id, min_offer=get_min_offer(), resp_feedback_fields=resp_feedback_fields) emit_webhook(client, url=f"/{treatment}/webhook/", job_id=job_id, treatment=treatment, worker_id=resp_worker_id, by_get=True) time.sleep(WEBHOOK_DELAY) with app.app_context(): # let the auto-responder kick-in for _ in range(NB_MAX_AUTO_PLAY_RETRIES): if is_resp_in_prop_result(resp_worker_id, job_id, treatment): break time.sleep(AUTO_PROP_DELAY) assert is_worker_available(resp_worker_id, get_table("resp", job_id=job_id, schema="result", treatment=treatment)) assert is_resp_in_prop_result(resp_worker_id, job_id, treatment)
def handle_survey_feedback(treatment=None, template=None, code_prefixes=None, form_class=None, overview_url=None): app.logger.info("handle_survey_feedback") if code_prefixes is None: code_prefixes = { "code_cpc": "cpc:", "code_exp": "exp:", "code_risk": "risk:", "code_cc": "cc:", "code_ras": "ras:" } if form_class is None: form_class = MainFormFeeback if template is None: template = "txx/survey_feedback.html" if overview_url is None: if treatment and treatment.upper() in TREATMENTS_AUTO_DSS: overview_url = url_for("overview_auto_prop_feedback") else: overview_url = url_for("overview_feedback") cookie_obj = get_cookie_obj(BASE) adapter_cookie = get_adapter_from_dict(cookie_obj.get("adapter", {})) adapter_args = get_adapter_from_dict(request.args) adapter_referrer = get_adapter() if adapter_referrer.job_id not in {"", "na", None}: adapter = adapter_referrer elif adapter_cookie.job_id not in {"", "na", None}: adapter = adapter_cookie else: adapter = adapter_args app.logger.debug(f"adapter: {adapter.to_dict()}") worker_code_key = f"{BASE}_worker_code" arg_job_id = adapter.get_job_id() arg_worker_id = adapter.get_worker_id() job_id = arg_job_id or f"na" worker_id = arg_worker_id if worker_id in {"", "na", None}: worker_id = str(uuid.uuid4()) adapter.worker_id = worker_id max_judgments = 0 #not set if adapter.has_api(): api = adapter.get_api() max_judgments = api.get_max_judgments() else: try: max_judgments = int( request.args.get("max_judgments", max_judgments)) except: pass job_config = get_job_config(get_db(), job_id) max_judgments = max(max_judgments, job_config.expected_judgments) next_player = check_is_proposer_next(job_id, worker_id, treatment, max_judgments=max_judgments) table_all = get_table("txx", "all", schema=None) # The task was already completed, so we skip to the completion code display if cookie_obj.get(BASE) and cookie_obj.get(worker_code_key): req_response = make_response( redirect(url_for(f"{treatment}.survey.survey_done", **request.args))) set_cookie_obj(req_response, BASE, cookie_obj) return req_response if treatment is None: treatment = get_latest_treatment() cookie_obj["job_id"] = job_id cookie_obj["worker_id"] = worker_id cookie_obj["assignment_id"] = adapter.get_assignment_id() cookie_obj["treatment"] = treatment cookie_obj["adapter"] = adapter.to_dict() cookie_obj[BASE] = True form = form_class(request.form) drop = request.form.get("drop") con = get_db() # if table_exists(con, table_all): # with con: # res = con.execute(f"SELECT * from {table_all} WHERE worker_id=?", (worker_id,)).fetchone() # if res: # flash(f"You already took part on this survey. You can just ignore this HIT for the assignment to be RETURNED later to another worker or you can submit right now for a REJECTION using the survey code provided.") # req_response = make_response(render_template("error.html", worker_code=WORKER_CODE_DROPPED)) # return req_response #The next player should be a proposer but some responders may still be processing data ## Should not matter in feedback surveys if next_player == NEXT_IS_WAITING: resp_table = get_table(base="resp", job_id=job_id, schema="result", treatment=treatment) prop_table = get_table(base="prop", job_id=job_id, schema="result", treatment=treatment) # We make sure the user didn't accidentaly refreshed the page after processing the main task if (not is_worker_available(worker_id, resp_table) and not is_worker_available(worker_id, prop_table)): flash( "Unfortunately there is no task available right now. Please check again in 15 minutes. Otherwise you can just ignore this HIT for the assignment to be RETURNED later to another worker or you can submit right now for a REJECTION using the survey code provided." ) return render_template("error.html", worker_code=WORKER_CODE_DROPPED) if adapter.is_preview() or job_id == "na": flash( "Please do note that you are currently in the preview mode of the survey. You SHOULD NOT FILL NEITHER SUBMIT the survey in this mode. Please go back to Mturk and read the instructions about how to correctly start this survey." ) if request.method == "POST" and (drop == "1" or form.validate_on_submit()): form = form_class(request.form) response = request.form.to_dict() response["timestamp"] = str(datetime.datetime.now()) cookie_obj["response"] = response is_codes_valid = True # Responders have to fill and submit tasks if is_codes_valid and job_id != "na" and worker_id != "na": cookie_obj["response"] = response #NOTE: the url should be pointing to handle_survey_feedback_done req_response = make_response( redirect( url_for(f"{treatment}.survey.survey_done", **request.args))) set_cookie_obj(req_response, BASE, cookie_obj) return req_response elif is_codes_valid: flash( "Your data was submitted and validated but not save as you are currently in the preview mode of the survey. Please go back to Mturk and read the instructions about how to correctly start this survey." ) req_response = make_response( render_template(template, job_id=job_id, worker_id=worker_id, treatment=treatment, form=form, max_judgments=max_judgments, max_gain=MAX_GAIN, maximum_control_mistakes=MAXIMUM_CONTROL_MISTAKES, overview_url=overview_url, tasks=app.config["TASKS"])) set_cookie_obj(req_response, BASE, cookie_obj) return req_response