"/response" endpoint is now functional

This commit is contained in:
Alexis Métaireau 2023-10-05 11:05:51 +02:00
parent f41e74d402
commit fa092ad75b
4 changed files with 23 additions and 27 deletions

View file

@ -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]): async def post_results(client: httpx.AsyncClient, server: str, results: List[ClientResult]):
json_results = [r.model_dump_json() for r in results] data = [r.model_dump() for r in results]
response = await client.post(f"{server}/results", json={"results": json_results}) response = await client.post(f"{server}/results", json=data)
if response.status_code == httpx.codes.OK: if response.status_code == httpx.codes.CREATED:
logger.info("Successfully posted results") logger.error(f"Successfully posted results {response.json()}")
else: else:
logger.error(f"Failed to post results: {response.read()}") logger.error(f"Failed to post results: {response.read()}")

View file

@ -41,14 +41,10 @@ async def read_tasks(request: Request, limit: int = 20, db: Session = Depends(ge
return tasks return tasks
class Results(BaseModel):
results: List[ClientResult]
@app.post("/results", status_code=201) @app.post("/results", status_code=201)
async def create_result(results: Results, db: Session = Depends(get_db)): async def create_result(results: List[ClientResult], db: Session = Depends(get_db)):
result_ids = [] db_results = []
for client_result in results: for client_result in results:
result_id = await queries.create_result(db, client_result) db_results.append(await queries.create_result(db, client_result))
db.commit() db.commit()
result_ids.append(result_id) return {"result_ids": [r.id for r in db_results]}
return {"result_ids": result_ids}

View file

@ -1,5 +1,5 @@
from typing import List from typing import List, Literal
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime, Enum
from sqlalchemy.orm import relationship, Mapped, mapped_column, DeclarativeBase from sqlalchemy.orm import relationship, Mapped, mapped_column, DeclarativeBase
from sqlalchemy_utils import ChoiceType from sqlalchemy_utils import ChoiceType
from sqlalchemy.orm import mapped_column from sqlalchemy.orm import mapped_column
@ -40,5 +40,6 @@ class Result(Base):
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True)
submitted_at: Mapped[datetime] = mapped_column() submitted_at: Mapped[datetime] = mapped_column()
success: Mapped[bool] = mapped_column() status: Mapped[Literal["success", "failure", "error"]] =\
content: Mapped[str] = mapped_column() mapped_column(Enum("success", "failure", "error"))
context: Mapped[dict] = mapped_column()

View file

@ -10,8 +10,7 @@ from datetime import datetime
async def list_tasks(db: Session, client_id: str, limit: int = 100): 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() tasks = db.query(Task).where(Task.selected_by == None).limit(limit).all()
now = datetime.now() now = datetime.now()
# XXX: Deactivated for now, as it simplifies testing. # 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() # db.commit()
return tasks return tasks
async def create_result(db: Session, client_result:schemas.ClientResult):
result = Result()
result.submitted_at = datetime.now() async def create_result(db: Session, client_result: schemas.ClientResult):
result.success = True if client_result.status == "success" else False result = Result(
result.content = str(client_result.context) submitted_at=datetime.now(),
status=client_result.status,
return db.add(result) context=client_result.context,
)
db.add(result)
return result
async def update_from_config(db: Session, config: schemas.Config): 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)) url = urljoin(domain, str(p.path))
for check in p.checks: for check in p.checks:
for check_key, expected in check.items(): for check_key, expected in check.items():
# Check the db for already existing tasks. # Check the db for already existing tasks.
existing_task = db.query( existing_task = db.query(
exists().where( exists().where(