From be6123b10e16b88c067984e4b315e2a7f338a5b6 Mon Sep 17 00:00:00 2001 From: "Akihiko (Aki) Kuroda" <16141898+akihikokuroda@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:09:10 -0400 Subject: [PATCH] Rename function.get_jobs to function.jobs and make it return Job objects (#1480) * rename get_jobs to jobs and return Job objects * lint * fix tests * lint * lint * lint * lint * disable cyclic-import lint check * restore image version * restore compose file * review comments * review comments * review comments * review comments * review comments --- client/.pylintrc | 3 ++- client/qiskit_serverless/core/function.py | 13 +++++++++++-- gateway/api/v1/views.py | 4 ++++ gateway/api/views.py | 18 +++++++++++------- tests/basic/06_function.py | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/client/.pylintrc b/client/.pylintrc index 6add8c53..074dbeaa 100644 --- a/client/.pylintrc +++ b/client/.pylintrc @@ -81,7 +81,8 @@ disable=raw-checker-failed, suppressed-message, useless-suppression, deprecated-pragma, - use-symbolic-message-instead + use-symbolic-message-instead, + cyclic-import # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option diff --git a/client/qiskit_serverless/core/function.py b/client/qiskit_serverless/core/function.py index 685a7a72..22caa3b4 100644 --- a/client/qiskit_serverless/core/function.py +++ b/client/qiskit_serverless/core/function.py @@ -118,7 +118,7 @@ class QiskitFunction: # pylint: disable=too-many-instance-attributes config=config, ) - def get_jobs(self): + def jobs(self): """Run function Raises: @@ -127,6 +127,10 @@ class QiskitFunction: # pylint: disable=too-many-instance-attributes Returns: Job ids : job executed this function """ + from qiskit_serverless.core.job import ( # pylint: disable=import-outside-toplevel + Job, + ) + if self.job_client is None: raise ValueError("No clients specified for a function.") @@ -138,10 +142,15 @@ class QiskitFunction: # pylint: disable=too-many-instance-attributes f"Function validation failed. Validation errors:\n {error_string}", ) - return self.job_client.get_jobs( + response = self.job_client.get_jobs( title=self.title, provider=self.provider, ) + jobs = [ + Job(job_id=job.get("id"), job_client=self.job_client, raw_data=job) + for job in response + ] + return jobs def _validate_function(self) -> Tuple[bool, List[str]]: """Validate function arguments using schema provided. diff --git a/gateway/api/v1/views.py b/gateway/api/v1/views.py index 7edd823d..3fd71432 100644 --- a/gateway/api/v1/views.py +++ b/gateway/api/v1/views.py @@ -38,6 +38,10 @@ class ProgramViewSet(views.ProgramViewSet): def get_serializer_run_job(*args, **kwargs): return v1_serializers.RunJobSerializer(*args, **kwargs) + @staticmethod + def get_serializer_job(*args, **kwargs): + return v1_serializers.JobSerializer(*args, **kwargs) + @swagger_auto_schema( operation_description="List author Qiskit Functions", responses={status.HTTP_200_OK: v1_serializers.ProgramSerializer(many=True)}, diff --git a/gateway/api/views.py b/gateway/api/views.py index 75ed4c42..70687767 100644 --- a/gateway/api/views.py +++ b/gateway/api/views.py @@ -45,6 +45,7 @@ from .ray import get_job_handler from .serializers import ( JobConfigSerializer, RunJobSerializer, + JobSerializer, RunProgramSerializer, UploadProgramSerializer, RetrieveCatalogSerializer, @@ -105,6 +106,14 @@ class ProgramViewSet(viewsets.GenericViewSet): return RunJobSerializer(*args, **kwargs) + @staticmethod + def get_serializer_job(*args, **kwargs): + """ + This method returns the job serializer + """ + + return JobSerializer(*args, **kwargs) + def get_serializer_class(self): return self.serializer_class @@ -412,13 +421,8 @@ class ProgramViewSet(viewsets.GenericViewSet): jobs = Job.objects.filter(program=program) else: jobs = Job.objects.filter(program=program, author=request.user) - return Response( - list( - jobs.values( - "status", "result", "id", "created", "version", "arguments" - ) - ) - ) + serializer = self.get_serializer_job(jobs, many=True) + return Response(serializer.data) class JobViewSet(viewsets.GenericViewSet): diff --git a/tests/basic/06_function.py b/tests/basic/06_function.py index 5f551e33..d98d934f 100644 --- a/tests/basic/06_function.py +++ b/tests/basic/06_function.py @@ -35,5 +35,5 @@ job = my_function.run(message="Argument for the custum function") print(job.result()) print(job.logs()) -jobs = my_function.get_jobs() +jobs = my_function.jobs() print(jobs)