diff options
Diffstat (limited to 'jb/managers')
| -rw-r--r-- | jb/managers/amt.py | 270 | ||||
| -rw-r--r-- | jb/managers/assignment.py | 240 | ||||
| -rw-r--r-- | jb/managers/bonus.py | 51 | ||||
| -rw-r--r-- | jb/managers/hit.py | 327 | ||||
| -rw-r--r-- | jb/managers/worker.py | 14 |
5 files changed, 0 insertions, 902 deletions
diff --git a/jb/managers/amt.py b/jb/managers/amt.py deleted file mode 100644 index 488bbb8..0000000 --- a/jb/managers/amt.py +++ /dev/null @@ -1,270 +0,0 @@ -import logging -from datetime import datetime, timezone -from typing import Any - -import botocore.exceptions -from generalresearch.currency import USDCent -from mypy_boto3_mturk import MTurkClient -from mypy_boto3_mturk.type_defs import ( - AssignmentTypeDef, - BonusPaymentTypeDef, - CreateHITTypeResponseTypeDef, - CreateHITWithHITTypeResponseTypeDef, - GetHITResponseTypeDef, -) -from pydantic import ValidationError - -from jb.config import TOPIC_ARN -from jb.models.amt import AMTAccount -from jb.models.assignment import Assignment -from jb.models.bonus import Bonus -from jb.models.definitions import HitStatus -from jb.models.hit import Hit, HitQuestion, HitType - -LOG = logging.getLogger("amtjb") - -REJECT_MESSAGE_UNKNOWN_ASSIGNMENT = "Unknown assignment" -REJECT_MESSAGE_NO_WORK = "Assignment was submitted with no attempted work." -REJECT_MESSAGE_BADDIE = "Quality has dropped below an acceptable level" -APPROVAL_MESSAGE = "Thank you!" -NO_WORK_APPROVAL_MESSAGE = ( - REJECT_MESSAGE_NO_WORK + " In the future, if you are not sent into a task, " - "please return the assignment, otherwise it will be rejected!" -) -BONUS_MESSAGE = "Great job! Bonus for a survey complete" - - -class AMTManager: - """ - I am type annotating this more than needed (depending on the editor), - however it's only because AMT on boto3 is not intuitive... at all. - """ - - def __init__( - self, - amt_client: MTurkClient, - **kwargs, # type: ignore - ): - super().__init__(**kwargs) - self.amt_client = amt_client - - def fetch_account(self) -> AMTAccount: - res = self.amt_client.get_account_balance() - - return AMTAccount.model_validate( - { - "available_balance": res["AvailableBalance"], - "onhold_balance": res.get("OnHoldBalance", 0), - } - ) - - def get_hit_if_exists(self, amt_hit_id: str) -> tuple[Hit | None, str | None]: - try: - res: GetHITResponseTypeDef = self.amt_client.get_hit(HITId=amt_hit_id) - - except self.amt_client.exceptions.RequestError as e: - msg = e.response.get("Error", {}).get("Message", "") - return None, msg - - hit = Hit.from_amt_get_hit(res["HIT"]) - return hit, None - - def get_hit_status(self, amt_hit_id: str) -> HitStatus: - res, msg = self.get_hit_if_exists(amt_hit_id=amt_hit_id) - - if res is None: - if msg is None: - return HitStatus.Unassignable - - if " does not exist. (" in msg: - return HitStatus.Disposed - - else: - LOG.warning(msg) - return HitStatus.Unassignable - - return res.status - - def create_hit_type(self, hit_type: HitType) -> HitType: - assert hit_type.amt_hit_type_id is None - - res: CreateHITTypeResponseTypeDef = self.amt_client.create_hit_type( - **hit_type.to_api_request_body() - ) # type: ignore - - hit_type.amt_hit_type_id = res["HITTypeId"] - - # TODO: Assert / Check that the SNS Notification was - # successfully created. - self.amt_client.update_notification_settings( - HITTypeId=hit_type.amt_hit_type_id, - Notification={ - "Destination": TOPIC_ARN, - "Transport": "SNS", - "Version": "2006-05-05", - # you can add more events, see mypy_boto3_mturk.literals.EventTypeType - "EventTypes": ["AssignmentSubmitted"], - }, - Active=True, - ) - - return hit_type - - def create_hit_with_hit_type(self, hit_type: HitType, question: HitQuestion) -> Hit: - """ - HITTypeId: str - LifetimeInSeconds: int - MaxAssignments: NotRequired[int] - Question: NotRequired[str] - UniqueRequestToken: NotRequired[str] - """ - assert hit_type.id is not None - assert hit_type.amt_hit_type_id is not None - - data = hit_type.generate_hit_amt_request(question=question) - res: CreateHITWithHITTypeResponseTypeDef = ( - self.amt_client.create_hit_with_hit_type(**data) - ) - - return Hit.from_amt_create_hit(res["HIT"], hit_type=hit_type, question=question) - - def get_assignment(self, amt_assignment_id: str) -> Assignment: - """ - You CANNOT get an Assignment if it has been only ACCEPTED (by the - worker). The api is stupid, it will only show up once it is Submitted - """ - res = self.amt_client.get_assignment(AssignmentId=amt_assignment_id) - ass_res: AssignmentTypeDef = res["Assignment"] - try: - assignment = Assignment.from_amt_get_assignment(ass_res) - except ValidationError as e: - # Baddies have been known to submit assignments with purposely - # malformed "answer" (xml) section, which will raise - # a pydantic validation error. Try to parse again with no Answer. - LOG.exception(e) - ass_res["Answer"] = None - # If it wasn't the Answer that caused the ValidationError, it'll raise again - assignment = Assignment.from_amt_get_assignment(ass_res) - - # to be clear, this has not checked whether it exists in our db - assert assignment.id is None - return assignment - - def get_assignment_if_exists(self, amt_assignment_id: str) -> Assignment | None: - expected_err_msg = f"Assignment {amt_assignment_id} does not exist" - - try: - return self.get_assignment(amt_assignment_id=amt_assignment_id) - except botocore.exceptions.ClientError as e: - LOG.warning(e) - error_code = e.response["Error"]["Code"] - error_msg = e.response["Error"]["Message"] - if error_code == "RequestError" and expected_err_msg in error_msg: - return None - raise e - - def reject_assignment_if_possible( - self, amt_assignment_id: str, msg: str = REJECT_MESSAGE_UNKNOWN_ASSIGNMENT - ) -> dict[str, Any] | None: - - # Unclear to me when this would fail - try: - return self.amt_client.reject_assignment( - AssignmentId=amt_assignment_id, RequesterFeedback=msg - ) - - except botocore.exceptions.ClientError as e: - LOG.warning(e) - return None - - def approve_assignment_if_possible( - self, - amt_assignment_id: str, - msg: str = APPROVAL_MESSAGE, - override_rejection: bool = False, - ) -> dict[str, Any] | None: - - # Unclear to me when this would fail - try: - return self.amt_client.approve_assignment( - AssignmentId=amt_assignment_id, - RequesterFeedback=msg, - OverrideRejection=override_rejection, - ) - - except botocore.exceptions.ClientError as e: - LOG.warning(e) - return None - - def update_hit_review_status(self, amt_hit_id: str, revert: bool = False) -> None: - try: - # Reviewable to Reviewing - self.amt_client.update_hit_review_status(HITId=amt_hit_id, Revert=revert) - - except botocore.exceptions.ClientError as e: - LOG.warning(f"{amt_hit_id=}, {e}") - error_msg = e.response["Error"]["Message"] - - if "does not exist" in error_msg: - raise ValueError(error_msg) - - # elif "This HIT is currently in the state 'Reviewing'" in error_msg: - # logging.warning(error_msg) - - def send_bonus( - self, - amt_worker_id: str, - amount: USDCent, - amt_assignment_id: str, - reason: str, - unique_request_token: str, - ) -> dict[str, Any] | None: - try: - return self.amt_client.send_bonus( - WorkerId=amt_worker_id, - BonusAmount=str(amount.to_usd()), - AssignmentId=amt_assignment_id, - Reason=reason, - UniqueRequestToken=unique_request_token, - ) - - except botocore.exceptions.ClientError as e: - LOG.warning(f"{amt_worker_id=} {amt_assignment_id=}, {e}") - return None - - def get_bonus(self, amt_assignment_id: str, payout_event_id: str) -> Bonus | None: - - res: list[BonusPaymentTypeDef] = self.amt_client.list_bonus_payments( - AssignmentId=amt_assignment_id - )["BonusPayments"] - - assert len(res) <= 1, ( - f"{amt_assignment_id=} Expected 1 or 0 bonuses, got {len(res)}" - ) - d = res[0] if res else None - - if d: - return Bonus.model_validate( - { - "amt_worker_id": d["WorkerId"], - "amount": USDCent(round(float(d["BonusAmount"]) * 100)), - "amt_assignment_id": d["AssignmentId"], - "reason": d["Reason"], - "grant_time": d["GrantTime"].astimezone(tz=timezone.utc), - "payout_event_id": payout_event_id, - } - ) - - return None - - def expire_all_hits(self) -> None: - # Used in testing only (or in an emergency I guess) - now = datetime.now(tz=timezone.utc) - paginator = self.amt_client.get_paginator("list_hits") - - for page in paginator.paginate(): - for hit in page["HITs"]: - if hit["HITStatus"] in ("Assignable", "Reviewable", "Reviewing"): - self.amt_client.update_expiration_for_hit( - HITId=hit["HITId"], ExpireAt=now - ) diff --git a/jb/managers/assignment.py b/jb/managers/assignment.py deleted file mode 100644 index 2740aee..0000000 --- a/jb/managers/assignment.py +++ /dev/null @@ -1,240 +0,0 @@ -from datetime import datetime, timezone - -from psycopg import sql -from pydantic import NonNegativeInt, PositiveInt - -from jb.managers.base import PostgresManager -from jb.models.assignment import Assignment, AssignmentStub -from jb.models.definitions import AssignmentStatus - - -class AssignmentManager(PostgresManager): - - def create_stub(self, stub: AssignmentStub) -> None: - assert stub.id is None - data = stub.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_assignment - (amt_assignment_id, amt_worker_id, status, - created_at, modified_at, hit_id) - VALUES - (%(amt_assignment_id)s, %(amt_worker_id)s, %(status)s, - %(created_at)s, %(modified_at)s, %(hit_id)s) - RETURNING id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - pk = c.fetchone()["id"] # type: ignore - conn.commit() - stub.id = pk - - def create(self, assignment: Assignment) -> None: - # Typically this is NOT used (we'd create the stub when HIT is - # accepted, then update it once the assignment is submitted), however - # there may be cases where an assignment is submitted and the stub - # was never created (probably due to error/downtime/baddie). - - assert assignment.id is None - data = assignment.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_assignment - (amt_assignment_id, amt_worker_id, status, - created_at, modified_at, hit_id, - auto_approval_time, accept_time, submit_time, - approval_time, rejection_time, requester_feedback, - tsid) - VALUES - (%(amt_assignment_id)s, %(amt_worker_id)s, %(status)s, - %(created_at)s, %(modified_at)s, %(hit_id)s, - %(auto_approval_time)s, %(accept_time)s, %(submit_time)s, - %(approval_time)s, %(rejection_time)s, %(requester_feedback)s, - %(tsid)s) - RETURNING id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - pk = c.fetchone()["id"] # type: ignore - conn.commit() - assignment.id = pk - - def get_stub(self, amt_assignment_id: str) -> AssignmentStub: - res = self.pg_config.execute_sql_query( - """ - SELECT ma.id, ma.hit_id, - ma.amt_assignment_id, ma.amt_worker_id, - ma.status, ma.created_at, ma.modified_at, - mh.amt_hit_id - FROM mtwerk_assignment ma - JOIN mtwerk_hit mh ON ma.hit_id = mh.id - WHERE amt_assignment_id = %(amt_assignment_id)s - LIMIT 1; - """, - params={"amt_assignment_id": amt_assignment_id}, - ) - assert len(res) == 1 - return AssignmentStub.model_validate(res[0]) - - def get_stub_if_exists(self, amt_assignment_id: str) -> AssignmentStub | None: - try: - return self.get_stub(amt_assignment_id=amt_assignment_id) - except AssertionError: - return None - - def get(self, amt_assignment_id: str) -> Assignment: - res = self.pg_config.execute_sql_query( - query=""" - SELECT mh.amt_hit_id, ma.id, ma.amt_assignment_id, - ma.amt_worker_id, ma.status, ma.auto_approval_time, - ma.accept_time, ma.submit_time, ma.approval_time, - ma.rejection_time, ma.requester_feedback, - ma.created_at, ma.modified_at, ma.tsid, ma.hit_id - FROM mtwerk_assignment ma - JOIN mtwerk_hit mh ON ma.hit_id = mh.id - WHERE amt_assignment_id = %(amt_assignment_id)s - LIMIT 1; - """, - params={"amt_assignment_id": amt_assignment_id}, - ) - assert len(res) == 1 - return Assignment.model_validate(res[0]) - - def update_answer(self, assignment: Assignment) -> None: - # We're assuming a stub already exists - # The assignment was submitted, but we haven't made a decision yet - now = datetime.now(tz=timezone.utc) - data = { - "status": assignment.status.value, - "submit_time": assignment.submit_time, - "auto_approval_time": assignment.auto_approval_time, - "tsid": assignment.tsid, - "amt_assignment_id": assignment.amt_assignment_id, - "modified_at": now, - } - query = sql.SQL(""" - UPDATE mtwerk_assignment - SET submit_time = %(submit_time)s, - auto_approval_time = %(auto_approval_time)s, - status = %(status)s, - tsid = %(tsid)s, - modified_at = %(modified_at)s - WHERE amt_assignment_id = %(amt_assignment_id)s - """) - # We force this to fail if the assignment doesn't already exist in the db - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - assert c.rowcount == 1, f"Expected 1 row, got {c.rowcount}" - conn.commit() - - def reject(self, assignment: Assignment) -> None: - assert assignment.status == AssignmentStatus.Rejected - assert assignment.rejection_time is not None - assert assignment.approval_time is None - assert assignment.requester_feedback is not None - now = datetime.now(tz=timezone.utc) - data = { - "status": assignment.status.value, - "submit_time": assignment.submit_time, - "rejection_time": assignment.rejection_time, - "requester_feedback": assignment.requester_feedback, - "amt_assignment_id": assignment.amt_assignment_id, - "auto_approval_time": assignment.auto_approval_time, - "accept_time": assignment.accept_time, - "modified_at": now, - } - query = sql.SQL(""" - UPDATE mtwerk_assignment - SET submit_time = %(submit_time)s, - rejection_time = %(rejection_time)s, - status = %(status)s, - requester_feedback = %(requester_feedback)s, - auto_approval_time = %(auto_approval_time)s, - accept_time = %(accept_time)s, - modified_at = %(modified_at)s - WHERE amt_assignment_id = %(amt_assignment_id)s - """) - # We force this to fail if the assignment doesn't already exist in the db - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - assert c.rowcount == 1, f"Expected 1 row, got {c.rowcount}" - conn.commit() - - def approve(self, assignment: Assignment) -> None: - assert assignment.status == AssignmentStatus.Approved - # # We can override a rejection and the rejection time is not None - # assert assignment.rejection_time is None - assert assignment.approval_time is not None - assert assignment.requester_feedback is not None - now = datetime.now(tz=timezone.utc) - data = { - "status": assignment.status.value, - "submit_time": assignment.submit_time, - "approval_time": assignment.approval_time, - "requester_feedback": assignment.requester_feedback, - "amt_assignment_id": assignment.amt_assignment_id, - "auto_approval_time": assignment.auto_approval_time, - "accept_time": assignment.accept_time, - "modified_at": now, - } - query = sql.SQL(""" - UPDATE mtwerk_assignment - SET submit_time = %(submit_time)s, - approval_time = %(approval_time)s, - status = %(status)s, - requester_feedback = %(requester_feedback)s, - auto_approval_time = %(auto_approval_time)s, - accept_time = %(accept_time)s, - modified_at = %(modified_at)s - WHERE amt_assignment_id = %(amt_assignment_id)s - """) - # We force this to fail if the assignment doesn't already exist in the db - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - assert c.rowcount == 1, f"Expected 1 row, got {c.rowcount}" - conn.commit() - - def missing_tsid_count( - self, amt_worker_id: str, lookback_hrs: PositiveInt = 24 - ) -> NonNegativeInt: - """ - Look at this user's previous N hrs of submitted assignments. - Count how many assignments they have submitted without a tsid. - """ - res = self.pg_config.execute_sql_query( - query=""" - SELECT COUNT(1) AS c - FROM mtwerk_assignment - WHERE amt_worker_id = %(amt_worker_id)s - AND submit_time > NOW() - (%(lookback_interval)s)::interval - AND tsid IS NULL; - """, - params={ - "amt_worker_id": amt_worker_id, - "lookback_interval": f"{lookback_hrs} hour", - }, - ) - return int(res[0]["c"]) # type: ignore - - def rejected_count( - self, amt_worker_id: str, lookback_hrs: int = 24 - ) -> NonNegativeInt: - """ - Look at this user's previous N hrs of submitted assignments. - Count how many rejected assignments they have. - """ - res = self.pg_config.execute_sql_query( - query=""" - SELECT COUNT(1) AS c - FROM mtwerk_assignment - WHERE amt_worker_id = %(amt_worker_id)s - AND submit_time > NOW() - (%(lookback_interval)s)::interval - AND status = %(status)s; - """, - params={ - "amt_worker_id": amt_worker_id, - "lookback_interval": f"{lookback_hrs} hour", - "status": AssignmentStatus.Rejected.value, - }, - ) - return int(res[0]["c"]) # type: ignore diff --git a/jb/managers/bonus.py b/jb/managers/bonus.py deleted file mode 100644 index b649103..0000000 --- a/jb/managers/bonus.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Any - -from psycopg import sql - -from jb.managers.base import PostgresManager -from jb.models.bonus import Bonus - - -class BonusManager(PostgresManager): - - def create(self, bonus: Bonus) -> None: - assert bonus.id is None - data = bonus.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_bonus - (payout_event_id, amt_worker_id, amount, grant_time, assignment_id, reason) - VALUES ( - %(payout_event_id)s, - %(amt_worker_id)s, - %(amount)s, - %(grant_time)s, - ( - SELECT id - FROM mtwerk_assignment - WHERE amt_assignment_id = %(amt_assignment_id)s - LIMIT 1 - ), - %(reason)s - ) - RETURNING id, assignment_id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - res = c.fetchone() - conn.commit() - bonus.id = res["id"] # type: ignore - bonus.assignment_id = res["assignment_id"] # type: ignore - - def filter(self, amt_assignment_id: str) -> list[Bonus]: - res: list[Any] = self.pg_config.execute_sql_query( - """ - SELECT mb.*, ma.amt_assignment_id - FROM mtwerk_bonus mb - JOIN mtwerk_assignment ma ON ma.id = mb.assignment_id - WHERE amt_assignment_id = %(amt_assignment_id)s; - """, - params={"amt_assignment_id": amt_assignment_id}, - ) - - return [Bonus.from_postgres(data=x) for x in res] diff --git a/jb/managers/hit.py b/jb/managers/hit.py deleted file mode 100644 index bbae92b..0000000 --- a/jb/managers/hit.py +++ /dev/null @@ -1,327 +0,0 @@ -from datetime import datetime, timezone - -from psycopg import sql - -from jb.managers.base import PostgresManager -from jb.models.definitions import HitStatus -from jb.models.hit import Hit, HitQuestion, HitType - - -class HitQuestionManager(PostgresManager): - - def create(self, question: HitQuestion) -> None: - assert question.id is None - data = question.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_question (url, height) - VALUES (%(url)s, %(height)s) - RETURNING id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - pk = c.fetchone()["id"] # type: ignore - conn.commit() - question.id = pk - - def get_by_id(self, question_id: int) -> HitQuestion: - res = self.pg_config.execute_sql_query( - """ - SELECT * - FROM mtwerk_question - WHERE id = %(question_id)s - LIMIT 1; - """, - params={"question_id": question_id}, - ) - assert len(res) == 1 - return HitQuestion.model_validate(res[0]) - - def get_by_values_if_exists(self, url: str, height: int) -> HitQuestion | None: - res = self.pg_config.execute_sql_query( - """ - SELECT * - FROM mtwerk_question - WHERE url = %(url)s AND height = %(height)s - LIMIT 2; - """, - params={"url": url, "height": height}, - ) - assert len(res) != 2, "More than 1 result!" - if len(res) == 0: - return None - - return HitQuestion.model_validate(res[0]) - - def get_or_create(self, question: HitQuestion) -> HitQuestion: - res = self.get_by_values_if_exists(url=question.url, height=question.height) - if res: - return res - self.create(question=question) - return question - - -class HitTypeManager(PostgresManager): - - def create(self, hit_type: HitType) -> None: - assert hit_type.id is None - assert hit_type.amt_hit_type_id is not None - - data = hit_type.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_hittype ( - amt_hit_type_id, - title, - description, - reward, - assignment_duration, - auto_approval_delay, - keywords, - min_active - ) - VALUES ( - %(amt_hit_type_id)s, - %(title)s, - %(description)s, - %(reward)s, - %(assignment_duration)s, - %(auto_approval_delay)s, - %(keywords)s, - %(min_active)s - ) - RETURNING id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - pk = c.fetchone()["id"] # type: ignore - conn.commit() - hit_type.id = pk - - def filter_active(self) -> list[HitType]: - res = self.pg_config.execute_sql_query(""" - SELECT * - FROM mtwerk_hittype - WHERE min_active > 0 - LIMIT 50 - """) - - if len(res) == 50: - raise ValueError("Too many HitTypes!") - - return [HitType.from_postgres(i) for i in res] - - def get(self, amt_hit_type_id: str) -> HitType: - res = self.pg_config.execute_sql_query( - """ - SELECT * - FROM mtwerk_hittype - WHERE amt_hit_type_id = %(amt_hit_type_id)s - """, - params={"amt_hit_type_id": amt_hit_type_id}, - ) - assert len(res) == 1 - return HitType.from_postgres(res[0]) - - def get_if_exists(self, amt_hit_type_id: str) -> HitType | None: - try: - return self.get(amt_hit_type_id=amt_hit_type_id) - except AssertionError: - return None - - def get_or_create(self, hit_type: HitType) -> HitType: - res = self.get_if_exists(amt_hit_type_id=hit_type.amt_hit_type_id) - if res: - return res - - self.create(hit_type=hit_type) - return self.get(amt_hit_type_id=hit_type.amt_hit_type_id) - - def set_min_active(self, hit_type: HitType) -> None: - assert hit_type.id, "must be in the db first!" - query = sql.SQL(""" - UPDATE mtwerk_hittype - SET min_active = %(min_active)s - WHERE id = %(id)s - """) - data = {"id": hit_type.id, "min_active": hit_type.min_active} - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - conn.commit() - row_cnt = c.rowcount - - assert row_cnt == 1, f"Expected 1 row updated, got {row_cnt}" - - -class HitManager(PostgresManager): - - def create(self, hit: Hit): - assert hit.amt_hit_id is not None - assert hit.id is None - data = hit.to_postgres() - query = sql.SQL(""" - INSERT INTO mtwerk_hit ( - amt_hit_id, - hit_type_id, - amt_group_id, - status, - review_status, - creation_time, - expiration, - question_id, - created_at, - modified_at, - max_assignments, - assignment_pending_count, - assignment_completed_count, - assignment_available_count - ) - VALUES ( - %(amt_hit_id)s, - %(hit_type_id)s, - %(amt_group_id)s, - %(status)s, - %(review_status)s, - %(creation_time)s, - %(expiration)s, - %(question_id)s, - %(created_at)s, - %(modified_at)s, - %(max_assignments)s, - %(assignment_pending_count)s, - %(assignment_completed_count)s, - %(assignment_available_count)s - ) - RETURNING id; - """) - - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - pk = c.fetchone()["id"] # type: ignore - conn.commit() - hit.id = pk - return hit - - def update_status(self, amt_hit_id: str, hit_status: HitStatus): - now = datetime.now(tz=timezone.utc) - query = sql.SQL(""" - UPDATE mtwerk_hit - SET status = %(status)s, modified_at = %(modified_at)s - WHERE amt_hit_id = %(amt_hit_id)s; - """) - - data = { - "amt_hit_id": amt_hit_id, - "status": hit_status.value, - "modified_at": now, - } - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - conn.commit() - assert c.rowcount == 1, c.rowcount - - def update_hit(self, hit: Hit): - hit.modified_at = datetime.now(tz=timezone.utc) - # fields expected to change: - fields = { - "status", - "review_status", - "assignment_pending_count", - "assignment_available_count", - "assignment_completed_count", - "amt_hit_id", - "modified_at", - } - query = sql.SQL(""" - UPDATE mtwerk_hit - SET status = %(status)s, review_status = %(review_status)s, - assignment_pending_count = %(assignment_pending_count)s, - assignment_available_count = %(assignment_available_count)s, - assignment_completed_count = %(assignment_completed_count)s, - modified_at = %(modified_at)s - WHERE amt_hit_id = %(amt_hit_id)s - RETURNING id; - """) - - data = hit.model_dump(mode="json", include=fields) - with self.pg_config.make_connection() as conn, conn.cursor() as c: - c.execute(query, data) - conn.commit() - assert c.rowcount == 1, c.rowcount - hit.id = c.fetchone()["id"] # type: ignore - - def get_from_amt_id(self, amt_hit_id: str) -> Hit: - res = self.pg_config.execute_sql_query( - """ - SELECT - mh.id, - mh.amt_hit_id, - mh.amt_group_id, - mh.status, - mh.review_status, - mh.creation_time, - mh.expiration, - mh.created_at, - mh.modified_at, - mh.assignment_available_count, - mh.assignment_completed_count, - mh.assignment_pending_count, - mh.max_assignments, - mht.amt_hit_type_id, - mht.title, - mht.description, - mht.reward, - mht.assignment_duration, - mht.auto_approval_delay, - mht.keywords, - mq.id as question_id, - mq.height, - mq.url - FROM mtwerk_hit mh - JOIN mtwerk_hittype mht ON mh.hit_type_id = mht.id - JOIN mtwerk_question mq ON mh.question_id = mq.id - WHERE amt_hit_id = %(amt_hit_id)s - LIMIT 2; - """, - params={"amt_hit_id": amt_hit_id}, - ) - assert len(res) == 1, "Incorrect number of results" - res = res[0] - - question_xml = HitQuestion.model_validate( - {"height": res.pop("height"), "url": res.pop("url")} - ).xml - - res["question_id"] = res["question_id"] - res["hit_question_xml"] = question_xml - - return Hit.from_postgres(res) - - def get_from_amt_id_if_exists(self, amt_hit_id: str) -> Hit | None: - try: - return self.get_from_amt_id(amt_hit_id=amt_hit_id) - - except (AssertionError, Exception): - return None - - def get_active_count(self, hit_type_id: int) -> int: - return self.pg_config.execute_sql_query( - query=""" - SELECT COUNT(1) as active_count - FROM mtwerk_hit - WHERE status = %(status)s - AND hit_type_id = %(hit_type_id)s; - """, - params={"status": HitStatus.Assignable, "hit_type_id": hit_type_id}, - )[0]["active_count"] - - def filter_active_ids(self, hit_type_id: int) -> set[str]: - res = self.pg_config.execute_sql_query( - """ - SELECT mh.amt_hit_id - FROM mtwerk_hit mh - WHERE hit_type_id = %(hit_type_id)s; - """, - params={"hit_type_id": hit_type_id}, - ) - return {x["amt_hit_id"] for x in res} diff --git a/jb/managers/worker.py b/jb/managers/worker.py deleted file mode 100644 index 9e7d5e3..0000000 --- a/jb/managers/worker.py +++ /dev/null @@ -1,14 +0,0 @@ -from mypy_boto3_mturk.type_defs import WorkerBlockTypeDef - -from jb.decorators import AMT_CLIENT - - -class WorkerManager: - - @staticmethod - def fetch_worker_blocks() -> list[WorkerBlockTypeDef]: - p = AMT_CLIENT.get_paginator("list_worker_blocks") - res: list[WorkerBlockTypeDef] = [] - for item in p.paginate(): - res.extend(item["WorkerBlocks"]) - return res |
