diff --git a/argos/client/cli.py b/argos/client/cli.py index 951b6f1..cca7d2f 100644 --- a/argos/client/cli.py +++ b/argos/client/cli.py @@ -25,11 +25,11 @@ async def complete_task(client: httpx.AsyncClient, task: dict) -> dict: async def post_results(client: httpx.AsyncClient, server: str, results: List[ClientResult]): - json_results = [r.model_dump_json() for r in results] - response = await client.post(f"{server}/results", json={"results": json_results}) + data = [r.model_dump() for r in results] + response = await client.post(f"{server}/results", json=data) - if response.status_code == httpx.codes.OK: - logger.info("Successfully posted results") + if response.status_code == httpx.codes.CREATED: + logger.error(f"Successfully posted results {response.json()}") else: logger.error(f"Failed to post results: {response.read()}") diff --git a/argos/server/api.py b/argos/server/api.py index 2a73a9c..4ee985e 100644 --- a/argos/server/api.py +++ b/argos/server/api.py @@ -41,14 +41,10 @@ async def read_tasks(request: Request, limit: int = 20, db: Session = Depends(ge return tasks -class Results(BaseModel): - results: List[ClientResult] - @app.post("/results", status_code=201) -async def create_result(results: Results, db: Session = Depends(get_db)): - result_ids = [] +async def create_result(results: List[ClientResult], db: Session = Depends(get_db)): + db_results = [] for client_result in results: - result_id = await queries.create_result(db, client_result) - db.commit() - result_ids.append(result_id) - return {"result_ids": result_ids} \ No newline at end of file + db_results.append(await queries.create_result(db, client_result)) + db.commit() + return {"result_ids": [r.id for r in db_results]} \ No newline at end of file diff --git a/argos/server/models.py b/argos/server/models.py index ac1de6b..253469e 100644 --- a/argos/server/models.py +++ b/argos/server/models.py @@ -1,5 +1,5 @@ -from typing import List -from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime +from typing import List, Literal +from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime, Enum from sqlalchemy.orm import relationship, Mapped, mapped_column, DeclarativeBase from sqlalchemy_utils import ChoiceType from sqlalchemy.orm import mapped_column @@ -40,5 +40,6 @@ class Result(Base): id: Mapped[int] = mapped_column(primary_key=True) submitted_at: Mapped[datetime] = mapped_column() - success: Mapped[bool] = mapped_column() - content: Mapped[str] = mapped_column() + status: Mapped[Literal["success", "failure", "error"]] =\ + mapped_column(Enum("success", "failure", "error")) + context: Mapped[dict] = mapped_column() diff --git a/argos/server/queries.py b/argos/server/queries.py index a09dce1..406941d 100644 --- a/argos/server/queries.py +++ b/argos/server/queries.py @@ -10,8 +10,7 @@ from datetime import datetime async def list_tasks(db: Session, client_id: str, limit: int = 100): - """List tasks and mark them as selected - """ + """List tasks and mark them as selected""" tasks = db.query(Task).where(Task.selected_by == None).limit(limit).all() now = datetime.now() # XXX: Deactivated for now, as it simplifies testing. @@ -21,14 +20,15 @@ async def list_tasks(db: Session, client_id: str, limit: int = 100): # db.commit() return tasks -async def create_result(db: Session, client_result:schemas.ClientResult): - result = Result() - result.submitted_at = datetime.now() - result.success = True if client_result.status == "success" else False - result.content = str(client_result.context) - - return db.add(result) +async def create_result(db: Session, client_result: schemas.ClientResult): + result = Result( + submitted_at=datetime.now(), + status=client_result.status, + context=client_result.context, + ) + db.add(result) + return result async def update_from_config(db: Session, config: schemas.Config): @@ -38,7 +38,6 @@ async def update_from_config(db: Session, config: schemas.Config): url = urljoin(domain, str(p.path)) for check in p.checks: for check_key, expected in check.items(): - # Check the db for already existing tasks. existing_task = db.query( exists().where(