Skip to content

Commit

Permalink
Merge pull request #2078 from FedML-AI/bhargav191098/adding_tags_to_d…
Browse files Browse the repository at this point in the history
…ata_storage

Adding tags to data storage object and the corresponding pretty table…
  • Loading branch information
alaydshah authored May 2, 2024
2 parents 19f93d2 + 06ed07f commit a271f18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
5 changes: 2 additions & 3 deletions python/fedml/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,12 @@ def cluster_killall(api_key=None) -> bool:
return cluster.kill(cluster_names=(), api_key=api_key)


def upload(data_path, api_key=None, service="R2", name=None, description=None, metadata=None, show_progress=False,
def upload(data_path, api_key=None, tag_list=[], service="R2", name=None, description=None, metadata=None, show_progress=False,
out_progress_to_err=True, progress_desc=None) -> FedMLResponse:
return storage.upload(data_path=data_path, api_key=api_key, name=name, description=description,
return storage.upload(data_path=data_path, api_key=api_key, name=name, description=description, tag_list =tag_list,
service=service, progress_desc=progress_desc, show_progress=show_progress,
out_progress_to_err=out_progress_to_err, metadata=metadata)


def get_storage_user_defined_metadata(data_name, api_key=None) -> FedMLResponse:
return storage.get_user_metadata(data_name=data_name, api_key=api_key)

Expand Down
5 changes: 3 additions & 2 deletions python/fedml/api/modules/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ def __init__(self, data: dict):
self.createdAt = data.get("createTime", None)
self.updatedAt = data.get("updateTime", None)
self.size = _get_size(data.get("fileSize",None))
self.tag_list = data.get("tags", None)


# Todo (alaydshah): Store service name in metadata
# Todo (alaydshah): If data already exists, don't upload again. Instead suggest to use update command

def upload(data_path, api_key, name, description, service, show_progress, out_progress_to_err, progress_desc,
def upload(data_path, api_key, name, description, tag_list, service, show_progress, out_progress_to_err, progress_desc,
metadata) -> FedMLResponse:
api_key = authenticate(api_key)

Expand Down Expand Up @@ -58,7 +59,7 @@ def upload(data_path, api_key, name, description, service, show_progress, out_pr
"description": description,
"fileSize": file_size,
"fileUrl": file_uploaded_url,
"tagNameList": [],
"tagNameList": tag_list,
}

try:
Expand Down
20 changes: 14 additions & 6 deletions python/fedml/cli/modules/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def validate_argument(ctx, param, value):
@click.option("--user_metadata", "-um", type=str, help="User-defined metadata in the form of a dictionary, for instance, "
" {'name':'value'} within double quotes. "" "
"Defaults to None.")
@click.option("--tags", "-t", type=str, help="Add tags to your data to store. Give tags in comma separated form like 'cv,unet,segmentation' If not provided, the tags will be empty.")
@click.option('--service', "-s", type=click.Choice(['R2']), default="R2", help="Storage service for object storage. "
"Only R2 is supported as of now")
@click.option(
Expand All @@ -65,10 +66,11 @@ def validate_argument(ctx, param, value):
default="release",
help=version_help,
)
def upload(data_path: str, name: str, user_metadata: str, description: str, version: str, api_key: str, service):
def upload(data_path: str, name: str, user_metadata: str, description: str, version: str, api_key: str, tags:str, service):
metadata = _parse_metadata(user_metadata)
tag_list = _parse_tags(tags)
fedml.set_env_version(version)
response = fedml.api.upload(data_path=data_path, api_key=api_key, name=name, service=service, show_progress=True,
response = fedml.api.upload(data_path=data_path, api_key=api_key, name=name, tag_list = tag_list, service=service, show_progress=True,
description=description, metadata=metadata)
if response.code == ResponseCode.SUCCESS:
click.echo(f"Data uploaded successfully. | url: {response.data}")
Expand Down Expand Up @@ -96,10 +98,10 @@ def list_data(version, api_key):
if not response.data:
click.echo(f"No stored objects found for account linked with apikey: {api_key}")
return
object_list_table = PrettyTable(["Data Name", "Data Size", "Description", "Created At", "Updated At"])
object_list_table = PrettyTable(["Data Name", "Data Size", "Description", "Data Tags","Created At", "Updated At"])
for stored_object in response.data:
object_list_table.add_row(
[stored_object.dataName, stored_object.size, stored_object.description, stored_object.createdAt, stored_object.updatedAt])
[stored_object.dataName, stored_object.size, stored_object.description, stored_object.tag_list,stored_object.createdAt, stored_object.updatedAt])
click.echo(object_list_table)
else:
click.echo(f"Failed to list stored objects for account linked with apikey {api_key}. "
Expand Down Expand Up @@ -157,8 +159,8 @@ def get_metadata(data_name, version, api_key):
return
click.echo(f"Successfully fetched metadata for object {data_name}:")
# Todo (alaydshah): Add file size and tags
metadata_table = PrettyTable(["Data Name","Data Size","Description", "Created At", "Updated At"])
metadata_table.add_row([metadata.dataName,metadata.size,metadata.description, metadata.createdAt, metadata.updatedAt])
metadata_table = PrettyTable(["Data Name","Data Size","Description","Data Tags","Created At", "Updated At"])
metadata_table.add_row([metadata.dataName,metadata.size,metadata.description,metadata.tag_list,metadata.createdAt, metadata.updatedAt])
click.echo(metadata_table)
click.echo("")
else:
Expand Down Expand Up @@ -238,3 +240,9 @@ def _parse_metadata(metadata: str):
click.echo(
f"Input metadata cannot be evaluated. Please make sure metadata is in the correct format. Error: {e}.")
exit()

def _parse_tags(tags:str):
if not tags:
return []
tag_list = tags.split(",")
return tag_list

0 comments on commit a271f18

Please sign in to comment.