Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akihikokuroda committed Jan 23, 2024
1 parent 25c5341 commit 81a53be
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 111 deletions.
3 changes: 3 additions & 0 deletions gateway/api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Permissions."""

from rest_framework import permissions
from .models import RuntimeJob


class IsOwner(permissions.BasePermission):
Expand All @@ -9,4 +10,6 @@ class IsOwner(permissions.BasePermission):
"""

def has_object_permission(self, request, view, obj):
if isinstance(obj, RuntimeJob):
return obj.job.author == request.user
return obj.author == request.user
5 changes: 0 additions & 5 deletions gateway/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,5 @@
router.register(
r"files", v1_views.FilesViewSet, basename=v1_views.FilesViewSet.BASE_NAME
)
router.register(
r"runtime_jobs",
v1_views.RuntimeJobViewSet,
basename=v1_views.RuntimeJobViewSet.BASE_NAME,
)

urlpatterns = router.urls
15 changes: 1 addition & 14 deletions gateway/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


from api import views
from api.models import Program, Job, RuntimeJob
from api.models import Program, Job
from api.permissions import IsOwner
from . import serializers as v1_serializers
from . import services as v1_services
Expand Down Expand Up @@ -68,16 +68,3 @@ class FilesViewSet(views.FilesViewSet):
"""

permission_classes = [permissions.IsAuthenticated, IsOwner]


class RuntimeJobViewSet(views.RuntimeJobViewSet): # pylint: disable=too-many-ancestors
"""
RuntimeJob view set.
"""

queryset = RuntimeJob.objects.all()
serializer_class = v1_serializers.RuntimeJobSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]

def get_serializer_class(self):
return v1_serializers.RuntimeJobSerializer
46 changes: 2 additions & 44 deletions gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
from utils import sanitize_file_path

from .exceptions import InternalServerErrorException, ResourceNotFoundException
from .models import Program, Job, RuntimeJob
from .models import Program, Job
from .ray import get_job_handler
from .serializers import (
JobSerializer,
ExistingProgramSerializer,
JobConfigSerializer,
)
from .serializers import JobSerializer, ExistingProgramSerializer, JobConfigSerializer
from .services import JobService, ProgramService, JobConfigService

logger = logging.getLogger("gateway")
Expand Down Expand Up @@ -472,41 +468,3 @@ def upload(self, request): # pylint: disable=invalid-name
destination.write(chunk)
return Response({"message": file_path})
return Response("server error", status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class RuntimeJobViewSet(viewsets.ModelViewSet): # pylint: disable=too-many-ancestors
"""ViewSet for runtime job operations handling."""

BASE_NAME = "utils"

def get_serializer_class(self):
return self.serializer_class

@action(methods=["POST"], detail=True)
def add(self, request, pk=None): # pylint: disable=invalid-name,unused-argument
"""Create runtime job with job."""
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.runtimejob.add", context=ctx):
runtimejob = RuntimeJob(
runtime_job=request.data.get("runtime_job"),
job=Job.objects.all().filter(id=request.data.get("job")).first(),
)
runtimejob.save()
serializer_class = self.get_serializer_class()
serializer = serializer_class(runtimejob) # pylint: disable=not-callable
return Response(serializer.data, status=status.HTTP_201_CREATED)

@action(methods=["POST"], detail=True)
def issued(self, request, pk=None): # pylint: disable=invalid-name,unused-argument
"""List runtime jobs for job."""
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.job.primitive", context=ctx):
job = request.data.get("job")
runtime_jobs = RuntimeJob.objects.all().filter(job=job)
serializer_class = self.get_serializer_class()
serializer = serializer_class( # pylint: disable=not-callable
runtime_jobs, many=True
)
return Response(serializer.data, status=status.HTTP_200_OK)
48 changes: 0 additions & 48 deletions gateway/tests/api/test_v1_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,51 +81,3 @@ def test_run_existing(self):
self.assertEqual(job.config.max_workers, 5)
self.assertEqual(job.config.workers, None)
self.assertEqual(job.config.auto_scaling, True)

def test_runtimejob(self):
"""Tests runtimejob setting."""
auth = reverse("rest_login")
response = self.client.post(
auth, {"username": "test_user", "password": "123"}, format="json"
)
token = response.data.get("access")
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)

runtime_job_response = self.client.post(
"/api/v1/runtime_jobs/utils/add/",
data={
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
"runtime_job": "runtimejob1",
},
format="json",
)
self.assertEqual(
runtime_job_response.json()["job"]["id"],
"1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
)
self.assertEqual(runtime_job_response.json()["runtime_job"], "runtimejob1")

runtime_job_response = self.client.get(
"/api/v1/runtime_jobs/",
format="json",
)
self.assertEqual(
runtime_job_response.json()["results"][0]["job"]["id"],
"1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
)
self.assertEqual(
runtime_job_response.json()["results"][0]["runtime_job"], "runtimejob1"
)

runtime_job_response = self.client.post(
"/api/v1/runtime_jobs/utils/issued/",
data={
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
},
format="json",
)
self.assertEqual(
runtime_job_response.json()[0]["job"]["id"],
"1a7947f9-6ae8-4e3d-ac1e-e7d608deec82",
)
self.assertEqual(runtime_job_response.json()[0]["runtime_job"], "runtimejob1")

0 comments on commit 81a53be

Please sign in to comment.