Skip to content

Commit

Permalink
add endpoint to upload uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
mam10eks committed Dec 2, 2024
1 parent 9ecab9e commit 80c17d8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
64 changes: 64 additions & 0 deletions application/src/tira_app/endpoints/v1/_anonymous.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import json
from pathlib import Path

from django.conf import settings
from django.core.cache import cache
from django.http import HttpResponseServerError
from django.urls import path
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response
from tira.check_format import _fmt, check_format, lines_if_valid
from tira.third_party_integrations import temporary_directory

from ... import model as modeldb
from ... import tira_model as model


@api_view(["GET"])
Expand All @@ -30,6 +36,64 @@ def read_anonymous_submission(request: Request, submission_uuid: str) -> Respons
)


@api_view(["POST"])
def claim_submission(request: Request, submission_uuid: str) -> Response:

try:
upload = modeldb.AnonymousUploads.objects.get(uuid=submission_uuid)
except:
return HttpResponseServerError(
json.dumps({"status": 1, "message": f"Run with uuid {submission_uuid} does not exist."})
)

if (
not upload
or not upload.dataset
or not upload.dataset.format
or not upload.dataset.default_task
or not upload.dataset.default_task.task_id
):
return HttpResponseServerError(json.dumps({"status": 1, "message": f"Unexpected format."}))

body = request.body.decode("utf-8")
body = json.loads(body)
result_dir = Path(settings.TIRA_ROOT) / "data" / "anonymous-uploads" / submission_uuid
format = json.loads(upload.dataset.format)[0]
status_code, message = check_format(result_dir, format)

if status_code != _fmt.OK:
HttpResponseServerError(json.dumps({"status": 1, "message": message}))

task_id = upload.dataset.default_task.task_id
dataset_id = upload.dataset.dataset_id
vm_id = body["vm_id"]

if "upload_group" not in body:
body["upload_group"] = model.add_upload(
task_id,
)
model.model.update_upload_metadata(
task_id, vm_id, body["upload_group"], body["display_name"], body["description"], body["paper_link"]
)

tmp_dir = temporary_directory()
uploaded_file = tmp_dir / format
with open(uploaded_file, "w") as f:
for l in lines_if_valid(result_dir, format):
f.write(l + "\n")

status_code, message = check_format(tmp_dir, format)
if status_code != _fmt.OK:
HttpResponseServerError(json.dumps({"status": 1, "message": message}))

new_run = model.model.add_uploaded_run(task_id, vm_id, dataset_id, body["upload_group"], uploaded_file)
if model.model.git_pipeline_is_enabled_for_task(task_id, cache):
model.run_eval(request=request, vm_id=vm_id, dataset_id=dataset_id, run_id=new_run["run"]["run_id"])

return Response({"upload_group": body["upload_group"]})


endpoints = [
path("claim/<str:submission_uuid>", claim_submission),
path("<str:submission_uuid>", read_anonymous_submission),
]
6 changes: 2 additions & 4 deletions application/src/tira_app/endpoints/vm_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import shutil
import tempfile
import uuid
import zipfile
from functools import wraps
Expand All @@ -17,6 +16,7 @@
from grpc import RpcError, StatusCode
from markdown import markdown
from tira.check_format import _fmt, check_format
from tira.third_party_integrations import temporary_directory

from .. import tira_model as model
from ..authentication import auth
Expand Down Expand Up @@ -534,9 +534,7 @@ def anonymous_upload(request, dataset_id):
uploaded_file = request.FILES["file"]
upload_id = str(uuid.uuid4())

result_dir = tempfile.TemporaryDirectory(prefix="tira-upload").name
result_dir = Path(result_dir)
result_dir.mkdir(parents=True, exist_ok=True)
result_dir = temporary_directory()

with open(result_dir / "upload.zip", "wb+") as destination:
for chunk in uploaded_file.chunks():
Expand Down
2 changes: 1 addition & 1 deletion application/src/tira_app/tira_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def get_software_by_task(task_id, vm_id):


def add_upload(task_id, vm_id, rename_to: Optional[str] = None):
""" " Add empty new upload"""
"""Add empty new upload"""
return model.add_upload(task_id, vm_id, rename_to)


Expand Down
15 changes: 12 additions & 3 deletions python-client/tira/rest_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests
from tqdm import tqdm

from tira.check_format import check_format
from tira.check_format import _fmt, check_format
from tira.local_execution_integration import LocalExecutionIntegration
from tira.pandas_integration import PandasIntegration
from tira.profiling_integration import ProfilingIntegration
Expand Down Expand Up @@ -43,6 +43,7 @@ def __init__(
failsave_max_delay: int = 15,
api_user_name: Optional[str] = None,
tira_cache_dir: Optional[str] = None,
verify: bool = True,
):
self.base_url = base_url or "https://www.tira.io"
self.tira_cache_dir = (
Expand All @@ -69,6 +70,7 @@ def __init__(

self.failsave_retries = failsave_retries
self.failsave_max_delay = failsave_max_delay
self.verify = verify

def load_settings(self):
try:
Expand Down Expand Up @@ -764,7 +766,11 @@ def upload_run_anonymous(self, file_path: Path, dataset_id: str):
file_path = Path(file_path)

# TODO use format from upload_to_tira instead of hard-coded run.txt
check_format(file_path, "run.txt")
status_code, msg = check_format(file_path, "run.txt")

if status_code != _fmt.OK:
print(msg)
raise ValueError(msg)

zip_file = temporary_directory()
zip_file = zip_file / "tira-upload.zip"
Expand All @@ -780,7 +786,10 @@ def upload_run_anonymous(self, file_path: Path, dataset_id: str):
files = {"file": open(zip_file, "rb")}

resp = requests.post(
url=f"{self.base_url}/api/v1/anonymous-uploads/{upload_to_tira['dataset_id']}", files=files, headers=headers
url=f"{self.base_url}/api/v1/anonymous-uploads/{upload_to_tira['dataset_id']}",
files=files,
headers=headers,
verify=self.verify,
)

if resp.status_code not in {200, 202}:
Expand Down

0 comments on commit 80c17d8

Please sign in to comment.