From abe009929581e51a6872d39ece017587be8792b9 Mon Sep 17 00:00:00 2001 From: Sierra Guequierre Date: Thu, 19 Sep 2024 10:20:45 -0400 Subject: [PATCH] DOCS-2899: Clean up data client API code samples (#729) --- src/viam/app/data_client.py | 120 ++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 47 deletions(-) diff --git a/src/viam/app/data_client.py b/src/viam/app/data_client.py index 938d1a991..5719025c6 100644 --- a/src/viam/app/data_client.py +++ b/src/viam/app/data_client.py @@ -177,17 +177,16 @@ async def tabular_data_by_filter( :: - from viam.utils import create_filter - my_data = [] + my_filter = create_filter(component_name="motor-1") last = None - my_filter = create_filter(component_name="left_motor") while True: - tabular_data, count, last = await data_client.tabular_data_by_filter(my_filter, last) + tabular_data, count, last = await data_client.tabular_data_by_filter(my_filter, last=last) if not tabular_data: break my_data.extend(tabular_data) + print(f"My data: {my_data}") Args: filter (viam.proto.app.data.Filter): Optional `Filter` specifying tabular data to retrieve. No `Filter` implies all tabular @@ -245,7 +244,10 @@ async def tabular_data_by_sql(self, organization_id: str, sql_query: str) -> Lis :: - data = await data_client.tabular_data_by_sql(organization_id="", sql_query="SELECT * FROM readings LIMIT 5") + data = await data_client.tabular_data_by_sql( + org_id="", + sql_query="SELECT * FROM readings LIMIT 5" + ) Args: @@ -267,20 +269,24 @@ async def tabular_data_by_mql(self, organization_id: str, mql_binary: List[bytes :: - # using bson import bson - tabular_data = await data_client.tabular_data_by_mql(org_id="", mql_binary=[ - bson.dumps({ '$match': { 'location_id': '' } }), - bson.dumps({ "$limit": 5 }) + + # using bson package (pip install bson) + tabular_data = await data_client.tabular_data_by_mql(organization_id="", mql_binary=[ + bson.dumps({ '$match': { 'location_id': '' } }), + bson.dumps({ '$limit': 5 }) ]) - # using pymongo - import bson - tabular_data = await data_client.tabular_data_by_mql(org_id="", mql_binary=[ - bson.encode({ '$match': { 'location_id': '' } }), + print(f"Tabular Data 1: {tabular_data}") + + # using pymongo package (pip install pymongo) + tabular_data = await data_client.tabular_data_by_mql(organization_id="", mql_binary=[ + bson.encode({ '$match': { 'location_id': '' } }), bson.encode({ "$limit": 5 }) ]) + print(f"Tabular Data 2: {tabular_data}") + Args: organization_id (str): The ID of the organization that owns the data. @@ -317,17 +323,20 @@ async def binary_data_by_filter( from viam.utils import create_filter from viam.proto.app.data import Filter, TagsFilter, TagsFilterType - # Get data captured from camera components my_data = [] last = None - my_filter = create_filter(component_name="camera") + my_filter = create_filter(component_name="camera-1") + while True: - data, count, last = await data_client.binary_data_by_filter(my_filter, last) + data, count, last = await data_client.binary_data_by_filter( + my_filter, limit=1, last=last) if not data: break my_data.extend(data) + print(f"My data: {my_data}") + # Get untagged data from a dataset my_untagged_data = [] @@ -405,7 +414,7 @@ async def binary_data_by_ids( from viam.proto.app.data import BinaryID - binary_metadata, _, _ = await data_client.binary_data_by_filter( + binary_metadata, count, last = await data_client.binary_data_by_filter( include_binary_data=False ) @@ -450,12 +459,10 @@ async def delete_tabular_data(self, organization_id: str, delete_older_than_days :: - from viam.utils import create_filter - - my_filter = create_filter(component_name="left_motor") - days_of_data_to_delete = 10 tabular_data = await data_client.delete_tabular_data( - org_id="a12b3c4e-1234-1abc-ab1c-ab1c2d345abc", days_of_data_to_delete) + organization_id="", + delete_older_than_days=150 + ) Args: organization_id (str): ID of organization to delete data from. @@ -483,12 +490,14 @@ async def delete_binary_data_by_filter(self, filter: Optional[Filter]) -> int: from viam.utils import create_filter - my_filter = create_filter(component_name="left_motor") + my_filter = create_filter(component_name="left_motor", organization_ids=[""]) + res = await data_client.delete_binary_data_by_filter(my_filter) Args: filter (viam.proto.app.data.Filter): Optional `Filter` specifying binary data to delete. Passing an empty `Filter` will lead to - all data being deleted. Exercise caution when using this option. + all data being deleted. Exercise caution when using this option. You must specify any organization ID with + "organization_ids" when using this option. Returns: int: The number of items deleted. @@ -506,8 +515,12 @@ async def delete_binary_data_by_ids(self, binary_ids: List[BinaryID]) -> int: :: from viam.proto.app.data import BinaryID + from viam.utils import create_filter - binary_metadata, _, _ = await data_client.binary_data_by_filter( + my_filter = create_filter(component_name="camera-1", organization_ids=[""]) + binary_metadata, count, last = await data_client.binary_data_by_filter( + filter=my_filter, + limit=20, include_binary_data=False ) @@ -545,10 +558,14 @@ async def add_tags_to_binary_data_by_ids(self, tags: List[str], binary_ids: List :: from viam.proto.app.data import BinaryID + from viam.utils import create_filter tags = ["tag1", "tag2"] - binary_metadata, _, _ = await data_client.binary_data_by_filter( + my_filter = create_filter(component_name="camera-1", organization_ids=[""]) + binary_metadata, count, last = await data_client.binary_data_by_filter( + filter=my_filter, + limit=20, include_binary_data=False ) @@ -608,10 +625,15 @@ async def remove_tags_from_binary_data_by_ids(self, tags: List[str], binary_ids: :: from viam.proto.app.data import BinaryID + from viam.utils import create_filter tags = ["tag1", "tag2"] - binary_metadata, _, _ = await data_client.binary_data_by_filter( + my_filter = create_filter(component_name="camera-1") + + binary_metadata, count, last = await data_client.binary_data_by_filter( + filter=my_filter, + limit=50, include_binary_data=False ) @@ -718,12 +740,12 @@ async def add_bounding_box_to_image_by_id( from viam.proto.app.data import BinaryID MY_BINARY_ID = BinaryID( - file_id=your-file_id, - organization_id=your-org-id, - location_id=your-location-id + file_id="", + organization_id="", + location_id="" ) - bbox_label = await data_client.add_bounding_box_to_image_by_id( + bbox_id = await data_client.add_bounding_box_to_image_by_id( binary_id=MY_BINARY_ID, label="label", x_min_normalized=0, @@ -732,7 +754,7 @@ async def add_bounding_box_to_image_by_id( y_max_normalized=.3 ) - print(bbox_label) + print(bbox_id) Args: binary_id (viam.proto.app.data.BinaryID): The ID of the image to add the bounding box to. @@ -799,6 +821,8 @@ async def bounding_box_labels_by_filter(self, filter: Optional[Filter] = None) - bounding_box_labels = await data_client.bounding_box_labels_by_filter( my_filter) + print(bounding_box_labels) + Args: filter (viam.proto.app.data.Filter): `Filter` specifying data to retrieve from. If no `Filter` is provided, all labels will return. @@ -818,7 +842,7 @@ async def get_database_connection(self, organization_id: str) -> str: :: - data_client.get_database_connection(org_id="a12b3c4e-1234-1abc-ab1c-ab1c2d345abc") + hostname = await data_client.get_database_connection(organization_id="") Args: organization_id (str): Organization to retrieve the connection for. @@ -840,8 +864,8 @@ async def configure_database_user(self, organization_id: str, password: str) -> :: await data_client.configure_database_user( - organization_id="", - password="your_password" + organization_id="", + password="Your_Password@1234" ) Args: @@ -860,8 +884,8 @@ async def create_dataset(self, name: str, organization_id: str) -> str: :: dataset_id = await data_client.create_dataset( - name="", - organization_id="" + name="", + organization_id="" ) print(dataset_id) @@ -885,7 +909,7 @@ async def list_dataset_by_ids(self, ids: List[str]) -> Sequence[Dataset]: :: datasets = await data_client.list_dataset_by_ids( - ids=["abcd-1234xyz-8765z-123abc"] + ids=[", "] ) print(datasets) @@ -909,8 +933,8 @@ async def list_datasets_by_organization_id(self, organization_id: str) -> Sequen :: - datasets = await data_client.list_dataset_by_organization_id( - organization_id=[""a12b3c4e-1234-1abc-ab1c-ab1c2d345abc""] + datasets = await data_client.list_datasets_by_organization_id( + organization_id="YOUR-ORG-ID" ) print(datasets) @@ -936,12 +960,13 @@ async def rename_dataset(self, id: str, name: str) -> None: :: await data_client.rename_dataset( - id="abcd-1234xyz-8765z-123abc", - name="" + id="", + name="MyDataset" ) Args: - id (str): The ID of the dataset. + id (str): The ID of the dataset. You can retrieve this by navigating to the **DATASETS** sub-tab of the **DATA** tab, + clicking on the dataset, clicking the **...** menu and selecting **Copy dataset ID**. name (str): The new name of the dataset. For more information, see `Data Client API `_. @@ -959,7 +984,8 @@ async def delete_dataset(self, id: str) -> None: ) Args: - id (str): The ID of the dataset. + id (str): The ID of the dataset. You can retrieve this by navigating to the **DATASETS** sub-tab of the **DATA** tab, + clicking on the dataset, clicking the **...** menu and selecting **Copy dataset ID**. For more information, see `Data Client API `_. """ @@ -975,7 +1001,7 @@ async def add_binary_data_to_dataset_by_ids(self, binary_ids: List[BinaryID], da from viam.proto.app.data import BinaryID - binary_metadata, _, _ = await data_client.binary_data_by_filter( + binary_metadata, count, last = await data_client.binary_data_by_filter( include_binary_data=False ) @@ -1015,7 +1041,7 @@ async def remove_binary_data_from_dataset_by_ids(self, binary_ids: List[BinaryID from viam.proto.app.data import BinaryID - binary_metadata, _, _ = await data_client.binary_data_by_filter( + binary_metadata, count, last = await data_client.binary_data_by_filter( include_binary_data=False ) @@ -1247,7 +1273,7 @@ async def streaming_data_capture_upload( component_type='motor', component_name='left_motor', method_name='IsPowered', - data_request_times=[(time_requested, time_received)], + data_request_times=[time_requested, time_received], tags=["tag_1", "tag_2"] )