Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/save result into result store #1570

Merged
merged 22 commits into from
Jan 29, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
retrieve job results
  • Loading branch information
paaragon committed Jan 28, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 16651685e80b8c7bcf78798beb241dab20d0b229
1 change: 0 additions & 1 deletion gateway/api/access_policies/jobs.py
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@ def can_access(user: User, job: Job) -> bool:
bool: True or False in case the user has access
"""

print(type(user))
is_provider_job = job.program and job.program.provider
if is_provider_job:
provider_groups = job.program.provider.admin_groups.all()
17 changes: 10 additions & 7 deletions gateway/api/services/result_storage.py
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ def __get_result_path(self, job_id: str) -> str:
self.user_results_directory, f"{job_id}{self.RESULT_FILE_EXTENSION}"
)

def get(self, job_id: str) -> Optional[Tuple[FileWrapper, str, int]]:
def get(self, job_id: str) -> Optional[str]:
"""
Retrieve a result file for the given job ID.

@@ -50,13 +50,16 @@ def get(self, job_id: str) -> Optional[Tuple[FileWrapper, str, int]]:
)
return None

with open(result_path, "rb") as result_file:
file_wrapper = FileWrapper(result_file)
file_type = (
mimetypes.guess_type(result_path)[0] or "application/octet-stream"
try:
with open(result_path, "r", encoding="utf-8") as result_file:
return result_file.read()
except (UnicodeDecodeError, IOError) as e:
logger.error(
"Failed to read result file for job ID '%s': %s",
job_id,
str(e),
)
file_size = os.path.getsize(result_path)
return file_wrapper, file_type, file_size
return None

def save(self, job_id: str, result: str) -> None:
"""
12 changes: 6 additions & 6 deletions gateway/api/views/jobs.py
Original file line number Diff line number Diff line change
@@ -105,12 +105,13 @@ def retrieve(self, request, pk=None): # pylint: disable=unused-argument
if is_provider_job:
serializer = self.get_serializer_job_without_result(job)
return Response(serializer.data)

result_store = ResultStorage(author.username)
result = result_store.get(job.id)
if result is not None:
job.result = result

serializer = self.get_serializer_job(job)
result_store = ResultStorage(author.username)
results = result_store.get(job.id)
if results is not None:
serializer.results = results

return Response(serializer.data)

@@ -140,8 +141,7 @@ def result(self, request, pk=None): # pylint: disable=invalid-name,unused-argum
can_access = JobAccessPolocies.can_save_result(author, job)
if not can_access:
return Response(
{"message": f"Job [{
job.id}] nor found for user [{author}]"},
{"message": f"Job [{job.id}] nor found for user [{author}]"},
status=status.HTTP_404_NOT_FOUND,
)

15 changes: 13 additions & 2 deletions gateway/tests/api/test_job.py
Original file line number Diff line number Diff line change
@@ -82,7 +82,17 @@ def test_job_detail(self):
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("status"), "SUCCEEDED")
self.assertEqual(jobs_response.data.get("result"), '{"ultimate": 42}')

def test_job_detail_without_result_file(self):
"""Tests job detail authorized."""
self._authorize()

jobs_response = self.client.get(
reverse("v1:jobs-detail", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec83"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("result"), '{"somekey":1}')

def test_job_provider_detail(self):
@@ -96,7 +106,7 @@ def test_job_provider_detail(self):
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("status"), "QUEUED")
self.assertEqual(jobs_response.data.get("result"), '{"somekey":1}')
self.assertEqual(jobs_response.data.get("result"), None)

def test_not_authorized_job_detail(self):
"""Tests job detail fails trying to access to other user job."""
@@ -133,6 +143,7 @@ def test_job_save_result(self):
settings.MEDIA_ROOT, "test_user", "results", f"{job_id}.json"
)
self.assertTrue(os.path.exists(result_path))
os.remove(result_path)

def test_not_authorized_job_save_result(self):
"""Tests job results save."""

This file was deleted.