Skip to content

Commit 4666fa8

Browse files
authored
Merge pull request #132 from usnistgov/127-upload-datasets-to-tiled
Tiled Data Upload
2 parents 126b00d + af57c23 commit 4666fa8

8 files changed

Lines changed: 1036 additions & 22 deletions

File tree

AFL/automation/APIServer/APIServer.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ def add_standard_routes(self):
254254
methods=['POST', 'GET'])
255255
self.app.add_url_rule('/retrieve_obj', 'retrieve_obj', self.retrieve_obj,
256256
methods=['POST', 'GET'])
257+
self.app.add_url_rule('/tiled_upload_data', 'tiled_upload_data', self.tiled_upload_data,
258+
methods=['POST'])
257259

258260
# self.init is now called from run()/run_threaded due to Flask 3 removal
259261
# of the before_first_request hook
@@ -599,6 +601,59 @@ def retrieve_obj(self):
599601
result = serialization.serialize(result)
600602
return jsonify({'obj':result}),200
601603

604+
def tiled_upload_data(self):
605+
"""Upload an xarray/csv/tsv payload to Tiled via multipart form data."""
606+
upload_file = request.files.get('file')
607+
if upload_file is None:
608+
return jsonify({
609+
'status': 'error',
610+
'message': 'No file payload provided in form field "file".',
611+
}), 400
612+
613+
upload_bytes = upload_file.read()
614+
if not upload_bytes:
615+
return jsonify({
616+
'status': 'error',
617+
'message': 'Uploaded file is empty.',
618+
}), 400
619+
620+
form = request.form
621+
metadata_payload = form.get('metadata', '')
622+
623+
upload_kwargs = {
624+
'upload_bytes': upload_bytes,
625+
'filename': upload_file.filename or '',
626+
'file_format': form.get('file_format', ''),
627+
'coordinate_column': form.get('coordinate_column', ''),
628+
'metadata': metadata_payload,
629+
'delimiter': form.get('delimiter', ''),
630+
}
631+
632+
# Allow direct metadata fields in form without requiring JSON blob.
633+
metadata_keys = [
634+
'sample_name',
635+
'sample_uuid',
636+
'AL_campaign_name',
637+
'AL_uuid',
638+
'task_name',
639+
'driver_name',
640+
]
641+
for key in metadata_keys:
642+
value = form.get(key, '')
643+
if value:
644+
upload_kwargs[key] = value
645+
646+
result = self.driver.tiled_upload_dataset(**upload_kwargs)
647+
if isinstance(result, dict) and result.get('status') == 'success':
648+
return jsonify(result), 200
649+
650+
if isinstance(result, dict):
651+
return jsonify(result), 400
652+
return jsonify({
653+
'status': 'error',
654+
'message': 'Unexpected response from tiled upload handler.',
655+
}), 500
656+
602657

603658
@jwt_required()
604659
def set_driver_object(self):

AFL/automation/APIServer/Driver.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,27 @@ def tiled_get_gantt_metadata(self, entry_ids, **kwargs):
423423
def tiled_download_combined_dataset(self, entry_ids, **kwargs):
424424
"""Download concatenated xarray dataset as NetCDF."""
425425
return super().tiled_download_combined_dataset(entry_ids, **kwargs)
426+
427+
@unqueued()
428+
def tiled_upload_dataset(
429+
self,
430+
dataset=None,
431+
upload_bytes=None,
432+
filename='',
433+
file_format='',
434+
coordinate_column='',
435+
metadata=None,
436+
delimiter='',
437+
**kwargs,
438+
):
439+
"""Upload xarray/csv/tsv data into Tiled."""
440+
return super().tiled_upload_dataset(
441+
dataset=dataset,
442+
upload_bytes=upload_bytes,
443+
filename=filename,
444+
file_format=file_format,
445+
coordinate_column=coordinate_column,
446+
metadata=metadata,
447+
delimiter=delimiter,
448+
**kwargs,
449+
)

0 commit comments

Comments
 (0)