Skip to content

Commit

Permalink
end of summer camp
Browse files Browse the repository at this point in the history
  • Loading branch information
forest1040 committed Oct 14, 2024
1 parent 5ffaeb4 commit 2e7802f
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 40 deletions.
6 changes: 3 additions & 3 deletions backend/oas/provider/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ paths:
/jobs/unfetched:
get:
summary: Fetch jobs for device
description: 'Fetches jobs for execution/cancel<br/><br/>Operation is valid only for job with status: submitted or cancelling. After the operation job status is changed to appropriate FETCHED state (QUEUED_FETCHED or CANCELLING_FETCHED)'
description: 'Fetches jobs for execution/cancel<br/><br/>Operation is valid only for job with status: submitted or cancelling. After the operation job status is changed to appropriate ready state (submitted or cancelled)'
operationId: getUnfetchedJobs
security: []
tags:
Expand Down Expand Up @@ -458,9 +458,9 @@ components:
jobs.InternalFetchableJobStatus:
type: string
enum:
- queued
- submitted
- cancelling
example: queued
example: submitted
jobs.UnfetchedJobsResponse:
type: array
items:
Expand Down
2 changes: 1 addition & 1 deletion backend/oas/provider/paths/jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ jobs.jobId:
jobs.unfetched:
get:
summary: "Fetch jobs for device"
description: "Fetches jobs for execution/cancel<br/><br/>Operation is valid only for job with status: submitted or cancelling. After the operation job status is changed to appropriate FETCHED state (QUEUED_FETCHED or CANCELLING_FETCHED)"
description: "Fetches jobs for execution/cancel<br/><br/>Operation is valid only for job with status: submitted or cancelling. After the operation job status is changed to appropriate ready state (submitted or cancelled)"
operationId: getUnfetchedJobs
security: []
tags:
Expand Down
4 changes: 2 additions & 2 deletions backend/oas/provider/schemas/jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ jobs.UnfetchedJobsResponse:

jobs.InternalFetchableJobStatus:
type: string
enum: ["queued", "cancelling"]
example: "queued"
enum: ["submitted", "cancelling"]
example: "submitted"

62 changes: 46 additions & 16 deletions backend/oqtopus_cloud/provider/routers/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional

from fastapi import APIRouter, Depends
from oqtopus_cloud.common.model_util import model_to_schema_dict
from oqtopus_cloud.common.models.job import Job
from oqtopus_cloud.common.session import get_db
from oqtopus_cloud.provider.conf import logger, tracer
Expand Down Expand Up @@ -35,7 +36,7 @@

@router.get(
"/jobs",
response_model=list[JobId],
response_model=list[JobDef],
)
@tracer.capture_method
def get_jobs(
Expand All @@ -55,7 +56,7 @@ def get_jobs(
if max_results is not None:
query = query.limit(max_results)
jobs = query.all()
return jobs
return [model_to_schema(job) for job in jobs]


@router.get(
Expand Down Expand Up @@ -104,22 +105,18 @@ def get_unfetched_jobs(


@router.get(
"/jobs/{jobId}",
"/jobs/{job_id}",
response_model=JobId,
responses={404: {"model": Detail}, 400: {"model": Detail}, 500: {"model": Detail}},
)
@tracer.capture_method
def get_job(
jobId: str,
job_id: str,
db: Session = Depends(get_db),
) -> JobId | ErrorResponse:
logger.info("invoked get_job")
try:
id = uuid.UUID(jobId).bytes
except Exception:
return BadRequestResponse("Invalid jobId")
try:
job = db.get(Job, id)
job = db.get(Job, job_id)
if job is None:
return NotFoundErrorResponse("Job not found")
return job.id
Expand All @@ -128,25 +125,23 @@ def get_job(


@router.patch(
"/jobs/{jobId}",
"/jobs/{job_id}",
response_model=JobStatusUpdateResponse,
responses={404: {"model": Detail}, 400: {"model": Detail}, 500: {"model": Detail}},
)
@tracer.capture_method
def update_job(
jobId: str,
job_id: str,
request: JobStatusUpdate,
db: Session = Depends(get_db),
) -> JobStatusUpdateResponse | ErrorResponse:
logger.info("invoked get_job")
try:
id = uuid.UUID(jobId).bytes
except Exception:
return BadRequestResponse("Invalid jobId")
try:
job = (
db.query(Job)
.filter(Job.id == id, or_(Job.status == "ready", Job.status == "running"))
.filter(
Job.id == job_id, or_(Job.status == "ready", Job.status == "running")
)
.first()
)
if job is None:
Expand All @@ -157,3 +152,38 @@ def update_job(
return JobStatusUpdateResponse(message="Job status updated")
except Exception as e:
return InternalServerErrorResponse(f"Error: {str(e)}")


MAP_MODEL_TO_SCHEMA = {
"id": "id",
"owner": "owner",
"status": "status",
"name": "name",
"description": "description",
"device_id": "device_id",
"job_info": "job_info",
"transpiler_info": "transpiler_info",
"simulator_info": "simulator_info",
"mitigation_info": "mitigation_info",
"job_type": "job_type",
"shots": "shots",
"status": "status",
"created_at": "created_at",
"updated_at": "updated_at",
}


def model_to_schema(model: Job) -> JobDef:
schema_dict = model_to_schema_dict(model, MAP_MODEL_TO_SCHEMA)

# load as json if not None.
# if schema_dict["basis_gates"]:
# schema_dict["basis_gates"] = json.loads(schema_dict["basis_gates"])
# logger.info("schema_dict!!!:", schema_dict)
if schema_dict["created_at"]:
schema_dict["created_at"] = schema_dict["created_at"].astimezone(jst)
if schema_dict["updated_at"]:
schema_dict["updated_at"] = schema_dict["updated_at"].astimezone(jst)

response = JobDef(**schema_dict)
return response
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/provider/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:32+00:00
# timestamp: 2024-10-14T08:52:30+00:00
# version: 0.25.9
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/provider/schemas/devices.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:32+00:00
# timestamp: 2024-10-14T08:52:30+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/provider/schemas/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:32+00:00
# timestamp: 2024-10-14T08:52:30+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down
6 changes: 3 additions & 3 deletions backend/oqtopus_cloud/provider/schemas/jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:32+00:00
# timestamp: 2024-10-14T08:52:30+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down Expand Up @@ -93,8 +93,8 @@ class JobStatusUpdateResponse(BaseModel):
message: str


class InternalFetchableJobStatus(RootModel[Literal["queued", "cancelling"]]):
root: Annotated[Literal["queued", "cancelling"], Field(examples=["queued"])]
class InternalFetchableJobStatus(RootModel[Literal["submitted", "cancelling"]]):
root: Annotated[Literal["submitted", "cancelling"], Field(examples=["submitted"])]


class UnfetchedJobsResponse(RootModel[list[Union[JobDef, JobId]]]):
Expand Down
7 changes: 0 additions & 7 deletions backend/oqtopus_cloud/user/routers/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ def get_jobs(
logger.info("invoked!", extra={"owner": owner})
stmt = select(Job).filter(Job.owner == owner).order_by(Job.created_at)
jobs = db.scalars(stmt).all()
# TODO: get_jobsでの詰替え
# 外向け(for UI)と内部(for edge)でデータの出し分けをした方がよい
return [model_to_schema(job) for job in jobs]
# for job in jobs:
# logger.info(f"job: {job.device_id}")
# #yield model_to_schema(job)

# return None
except Exception as e:
logger.info(f"error: {str(e)}")
return InternalServerErrorResponse(detail=str(e))
Expand Down
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/user/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:24+00:00
# timestamp: 2024-10-14T08:52:23+00:00
# version: 0.25.9
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/user/schemas/devices.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:24+00:00
# timestamp: 2024-10-14T08:52:23+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/user/schemas/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:24+00:00
# timestamp: 2024-10-14T08:52:23+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/user/schemas/jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:24+00:00
# timestamp: 2024-10-14T08:52:23+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion backend/oqtopus_cloud/user/schemas/success.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-14T08:42:24+00:00
# timestamp: 2024-10-14T08:52:23+00:00
# version: 0.25.9

from __future__ import annotations
Expand Down

0 comments on commit 2e7802f

Please sign in to comment.