def _process_goat(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) :param clear_session: (bool) """ if worker_id is None: worker_id = generate_worker_id("goat") path = f"/tasks/goat/?job_id={job_id}&worker_id={worker_id}" if bonus_mode == "random": data = {field: random.randint(0, 1) for field in goat.FIELDS} elif bonus_mode == "none": data = {field: 0 for field in goat.FIELDS} else: data = {field: 1 for field in goat.FIELDS} with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) with app.test_request_context( f"/tasks/goat/check/?job_id={job_id}&woker_id={worker_id}"): for key, value in data.items(): if value: client.get( f"/tasks/goat/check/?job_id={job_id}&worker_id={worker_id}&cell={key}" ) return client.post(path, data=data, follow_redirects=True).data
def _process_cg(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) """ if worker_id is None: worker_id = generate_worker_id("cg") path = f"/tasks/cg/?job_id={job_id}&worker_id={worker_id}" if bonus_mode == "random": data = { field: random.choice([0, 5, 10, 15, 20]) if random.random() > 0.6 else 0 for field in cg.FIELDS } elif bonus_mode == "none": data = {field: 20 for field in cg.FIELDS} elif bonus_mode == "full": data = {field: 0 for field in cg.FIELDS} with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) return client.post(path, data=data, follow_redirects=True).data
def _process_ras(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True, url_kwargs=None): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) :param clear_session: (bool) """ if worker_id is None: worker_id = generate_worker_id("ras") path = f"/tasks/ras/?job_id={job_id}&worker_id={worker_id}" if url_kwargs: for k, v in url_kwargs.items(): path += f"&{k}={v}" app.logger.debug("PATH: " + str(path)) data = {field: random.choice(list(ras.SCALAS)) for field in ras.FIELDS} with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) return client.post(path, data=data, follow_redirects=True).data
def _process_eff(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) """ if worker_id is None: worker_id = generate_worker_id("eff") #worker_id = generate_worker_id("eff_FORCED") path = f"/tasks/eff/?job_id={job_id}&worker_id={worker_id}" if bonus_mode == "random": data = { field: "".join(random.choices(string.ascii_lowercase, k=4)) if random.random() > 0.6 else eff.SOLUTIONS[field] for field in eff.SOLUTIONS } elif bonus_mode == "none": data = {field: "----" for field in eff.SOLUTIONS} else: data = { field: str(solution) for field, solution in eff.SOLUTIONS.items() } with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) return client.post(path, data=data, follow_redirects=True).data
def _process_crt(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) """ if worker_id is None: worker_id = generate_worker_id("crt") path = f"/tasks/crt/?job_id={job_id}&worker_id={worker_id}" if bonus_mode == "random": data = { field: random.randint(0, 100) if random.random() > 0.6 else crt.SOLUTIONS[field] for field in crt.SOLUTIONS } elif bonus_mode == "none": data = {field: 0 for field in crt.SOLUTIONS} else: data = crt.SOLUTIONS with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) return client.post(path, data=data, follow_redirects=True).data
def _process_resp(client, treatment, job_id="test", worker_id=None, min_offer=MIN_OFFER, clear_session=True, path=None, dss_available=False, resp_feedback_fields=None): app.logger.error("_process_resp") MODEL_KEY = f"{TREATMENTS_MODEL_REFS[treatment.upper()]}_MODEL" dss_available = bool(app.config.get(MODEL_KEY)) dss_available = dss_available and treatment[:3] not in { "t00", "t10", "t11" } #Treatments where the responder isn't informed about the DSS if worker_id is None: worker_id = generate_worker_id() if path is None: path = f"/{treatment}/resp/?job_id={job_id}&worker_id={worker_id}" if resp_feedback_fields is None: resp_feedback_fields = [ "feedback_alternative", "feedback_fairness", "feedback_accuracy" ] with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path, follow_redirects=True) data = { "min_offer": min_offer, } res = client.post(path, data=data, follow_redirects=True) if dss_available: path_dss = f"/{treatment}/resp_dss/?job_id={job_id}&worker_id={worker_id}" client.get(path_dss, follow_redirects=True) data_dss = { "min_offer": min_offer, } client.post(path_dss, data=data_dss, follow_redirects=True) data_feedback = { field: random.choice(AI_FEEDBACK_SCALAS_KEYS) for field in resp_feedback_fields } path_feedback = f"/{treatment}/resp_feedback/?job_id={job_id}&worker_id={worker_id}" res = client.post(path_feedback, data=data_feedback, follow_redirects=True) return res
def _process_cc(client, job_id="test", worker_id=None, bonus_mode="random", clear_session=True, url_kwargs=None): """ :param client: (flask.testclient) :param job_id: (str) :param worker_id: (str) :param bonus_mode: (str: random|full|none) :param clear_session: (bool) """ if worker_id is None: worker_id = generate_worker_id("cc") path = f"/tasks/cc/?job_id={job_id}&worker_id={worker_id}" path_check = f"/tasks/cc/check/?job_id={job_id}&worker_id={worker_id}" if url_kwargs: for k, v in url_kwargs.items(): path += f"&{k}={v}" app.logger.debug("PATH: " + str(path)) letters = list(cc.ITEMS) random.shuffle(letters) delays = [random.randint(0, 1000) for _ in letters] data = dict() if bonus_mode == "random": clicked = [random.choice([True, False]) for _ in letters] elif bonus_mode == "none": clicked = [False] * len(letters) else: clicked = [letter == cc.LETTER_SIGNAL for letter in letters] data = json.dumps({ "letters": letters, "delays": delays, "clicked": clicked, }) with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) client.post(path_check, data=data, follow_redirects=True) return client.post(path, follow_redirects=True).data
def _process_hexaco(client, job_id="test", worker_id=None, clear_session=True, url_kwargs=None): if worker_id is None: worker_id = generate_worker_id("hexaco") path = f"/tasks/hexaco/?job_id={job_id}&worker_id={worker_id}" if url_kwargs: for k, v in url_kwargs.items(): path += f"&{k}={v}" app.logger.debug("PATH: " + str(path)) with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path) return client.post( path, data={f"q{qid}": random.randint(1, 5) for qid in range(1, 31)}, follow_redirects=True).data
def _process_prop(client, treatment, job_id="test", worker_id=None, offer=OFFER, clear_session=True, response_available=False, path=None, auto_finalize=False, nb_dss_check=None): app.logger.debug("_process_prop") MODEL_KEY = f"{TREATMENTS_MODEL_REFS[treatment.upper()]}_MODEL" dss_available = bool(app.config.get(MODEL_KEY)) if worker_id is None: worker_id = generate_worker_id("prop") if path is None: path = f"/{treatment}/prop/?job_id={job_id}&worker_id={worker_id}" path_dss = f"/{treatment}/prop_dss/?job_id={job_id}&worker_id={worker_id}" path_feedback = f"/{treatment}/prop_feedback/?job_id={job_id}&worker_id={worker_id}" path_check = f"{treatment}/prop/check/?secret_key_hash={get_secret_key_hash()}" if auto_finalize is True: path += "&auto_finalize=1" path_dss += "&auto_finalize=1" path_feedback += "&auto_finalize=1" if not response_available: _process_resp_tasks(client, treatment, job_id=job_id) with app.test_request_context(path): if clear_session: with client: with client.session_transaction() as sess: sess.clear() client.get(path, follow_redirects=True) app.logger.debug( f"Path: {path}, Path2: {path_dss}, Path-check: {path_check}") if dss_available: # gather the "optimal" offer from the dss if offer == "auto": info = client.get(path_check, follow_redirects=True) try: offer = json.loads(info.data)["ai_offer"] except Exception as err: app.logger.error( f"couldn't access to the ai_offer: - {info.data}") app.log_exception(err) offer = -1 data = {"offer": offer} res = client.post(path, data=data, follow_redirects=True) data_dss = { "offer_dss": offer, } # if nb_dss_check is None: # res = client.post(path_dss, data=data_dss, follow_redirects=True) if nb_dss_check is not None: client.get(path_dss, follow_redirects=True) client.get(path_check) for _ in range(nb_dss_check): client.get( f"{path_check}?offer={random.choice(list(range(0, MAX_GAIN+1, 5)))}" ) res = client.post(path_dss, data=data_dss, follow_redirects=True) data_feedback = { "feedback_alternative": random.choice(AI_FEEDBACK_SCALAS_KEYS), "feedback_understanding": random.choice(AI_FEEDBACK_SCALAS_KEYS), "feedback_explanation": random.choice(AI_FEEDBACK_SCALAS_KEYS), "feedback_accuracy": random.choice(AI_FEEDBACK_ACCURACY_SCALAS_KEYS), } res = client.post(path_feedback, data=data_feedback, follow_redirects=True) else: if offer == "auto": offer = MAX_GAIN // 2 res = client.post(path, data={"offer": offer}, follow_redirects=True) return res