diff options
| author | Max Nanis | 2026-02-24 17:26:15 -0500 |
|---|---|---|
| committer | Max Nanis | 2026-02-24 17:26:15 -0500 |
| commit | 8c1940445503fd6678d0961600f2be81622793a2 (patch) | |
| tree | b9173562b8824b5eaa805e446d9d780e1f23fb2a /jb/views | |
| parent | 25d8c3c214baf10f6520cc1351f78473150e5d7a (diff) | |
| download | amt-jb-8c1940445503fd6678d0961600f2be81622793a2.tar.gz amt-jb-8c1940445503fd6678d0961600f2be81622793a2.zip | |
Extensive use of type checking. Movement of pytest conf towards handling managers (for db agnostic unittest). Starting to organize pytests.
Diffstat (limited to 'jb/views')
| -rw-r--r-- | jb/views/common.py | 68 | ||||
| -rw-r--r-- | jb/views/tasks.py | 9 | ||||
| -rw-r--r-- | jb/views/utils.py | 7 |
3 files changed, 14 insertions, 70 deletions
diff --git a/jb/views/common.py b/jb/views/common.py index 46ac608..0dc8b56 100644 --- a/jb/views/common.py +++ b/jb/views/common.py @@ -11,7 +11,7 @@ from starlette.responses import RedirectResponse from jb.config import settings, JB_EVENTS_STREAM from jb.decorators import REDIS, HM from jb.flow.monitoring import emit_assignment_event, emit_mturk_notification_event -from jb.models.currency import USDCent +from generalresearchutils.currency import USDCent from jb.models.definitions import ReportValue, AssignmentStatus from jb.models.event import MTurkEvent from jb.settings import BASE_HTML @@ -71,6 +71,7 @@ async def work(request: Request): url=f"/preview/?{request.url.query}" if request.url.query else "/preview/", status_code=302, ) + if amt_assignment_id is None or amt_assignment_id == "ASSIGNMENT_ID_NOT_AVAILABLE": # Worker is previewing the HIT amt_hit_type_id = "unknown" @@ -91,71 +92,6 @@ async def work(request: Request): return HTMLResponse(BASE_HTML) -@common_router.get(path="/survey/", response_class=JSONResponse) -def survey( - request: Request, - worker_id: str = Query(), - duration: int = Query(default=1200), -): - if not worker_id: - raise HTTPException(status_code=400, detail="Missing worker_id") - - # (1) Check wallet - wallet_url = f"{settings.fsb_host}{settings.product_id}/wallet/" - wallet_res = requests.get(wallet_url, params={"bpuid": worker_id}) - if wallet_res.status_code != 200: - raise HTTPException(status_code=502, detail="Wallet check failed") - - wallet_data = wallet_res.json() - wallet_balance = wallet_data["wallet"]["amount"] - if wallet_balance < -100: - return JSONResponse( - { - "total_surveys": 0, - "link": None, - "duration": None, - "payout": None, - } - ) - - # (2) Get offerwall - client_ip = "69.253.144.55" if settings.debug else request.client.host - offerwall_url = f"{settings.fsb_host}{settings.product_id}/offerwall/d48cce47/" - offerwall_res = requests.get( - offerwall_url, - params={ - "bpuid": worker_id, - "ip": client_ip, - "n_bins": 1, - "duration": duration, - }, - ) - - if offerwall_res.status_code != 200: - raise HTTPException(status_code=502, detail="Offerwall request failed") - - try: - rj = offerwall_res.json() - bucket = rj["offerwall"]["buckets"][0] - return JSONResponse( - { - "total_surveys": rj["offerwall"]["availability_count"], - "link": bucket["uri"], - "duration": round(bucket["duration"]["q2"] / 60), - "payout": USDCent(bucket["payout"]["q2"]).to_usd_str(), - } - ) - except Exception: - return JSONResponse( - { - "total_surveys": 0, - "link": None, - "duration": None, - "payout": None, - } - ) - - @common_router.post(path=f"/{settings.sns_path}/", include_in_schema=False) async def mturk_notifications(request: Request): """ diff --git a/jb/views/tasks.py b/jb/views/tasks.py index 7176b29..313295f 100644 --- a/jb/views/tasks.py +++ b/jb/views/tasks.py @@ -18,18 +18,23 @@ def process_request(request: Request) -> None: amt_assignment_id = request.query_params.get("assignmentId", None) if amt_assignment_id == "ASSIGNMENT_ID_NOT_AVAILABLE": raise ValueError("shouldn't happen") + amt_hit_id = request.query_params.get("hitId", None) amt_worker_id = request.query_params.get("workerId", None) print(f"process_request: {amt_assignment_id=} {amt_worker_id=} {amt_hit_id=}") assert amt_worker_id and amt_hit_id and amt_assignment_id # Check that the HIT is still valid - hit = HM.get_from_amt_id(amt_hit_id=amt_hit_id) + hit = HM.get_from_amt_id_if_exists(amt_hit_id=amt_hit_id) + if not hit: + raise ValueError(f"Hit {amt_hit_id} not found in DB") + _ = check_hit_status(amt_hit_id=amt_hit_id, amt_hit_type_id=hit.amt_hit_type_id) emit_assignment_event( status=AssignmentStatus.Accepted, amt_hit_type_id=hit.amt_hit_type_id, ) + # I think it won't be assignable anymore? idk # assert hit_status == HitStatus.Assignable, f"hit {amt_hit_id} {hit_status=}. Expected Assignable" @@ -53,7 +58,7 @@ def process_request(request: Request) -> None: assert assignment_stub.amt_worker_id == amt_worker_id assert assignment_stub.amt_assignment_id == amt_assignment_id assert assignment_stub.created_at > ( - datetime.now(tz=timezone.utc) - timedelta(minutes=90) + datetime.now(tz=timezone.utc) - timedelta(minutes=90) ) return None diff --git a/jb/views/utils.py b/jb/views/utils.py index 39db5d2..0d08e9b 100644 --- a/jb/views/utils.py +++ b/jb/views/utils.py @@ -8,8 +8,11 @@ def get_client_ip(request: Request) -> str: """ ip = request.headers.get("X-Forwarded-For") if not ip: - ip = request.client.host + ip = request.client.host # type: ignore elif ip == "testclient" or ip.startswith("10."): forwarded = request.headers.get("X-Forwarded-For") - ip = forwarded.split(",")[0].strip() if forwarded else request.client.host + ip = ( + forwarded.split(",")[0].strip() if forwarded else request.client.host # type: ignore + ) + return ip |
