|
84 | 84 | StreamingDataCaptureUploadResponse, |
85 | 85 | UploadMetadata, |
86 | 86 | ) |
87 | | -from viam.utils import ValueTypes, create_filter, datetime_to_timestamp, struct_to_dict |
| 87 | +from viam.utils import ValueTypes, _alias_param, create_filter, datetime_to_timestamp, struct_to_dict |
88 | 88 |
|
89 | 89 | LOGGER = logging.getLogger(__name__) |
90 | 90 |
|
@@ -334,33 +334,36 @@ async def tabular_data_by_sql(self, organization_id: str, sql_query: str) -> Lis |
334 | 334 | response: TabularDataBySQLResponse = await self._data_client.TabularDataBySQL(request, metadata=self._metadata) |
335 | 335 | return [bson.decode(bson_bytes) for bson_bytes in response.raw_data] |
336 | 336 |
|
337 | | - async def tabular_data_by_mql(self, organization_id: str, mql_binary: List[bytes]) -> List[Dict[str, Union[ValueTypes, datetime]]]: |
| 337 | + @_alias_param("query", param_alias="mql_binary") |
| 338 | + async def tabular_data_by_mql( |
| 339 | + self, organization_id: str, query: Union[List[bytes], List[Dict[str, Any]]] |
| 340 | + ) -> List[Dict[str, Union[ValueTypes, datetime]]]: |
338 | 341 | """Obtain unified tabular data and metadata, queried with MQL. |
339 | 342 |
|
340 | 343 | :: |
341 | 344 |
|
342 | 345 | import bson |
343 | 346 |
|
344 | | - # using pymongo package (pip install pymongo) |
345 | | - tabular_data = await data_client.tabular_data_by_mql(organization_id="<YOUR-ORG-ID>", mql_binary=[ |
346 | | - bson.encode({ '$match': { 'location_id': '<YOUR-LOCATION-ID>' } }), |
347 | | - bson.encode({ "$limit": 5 }) |
| 347 | + tabular_data = await data_client.tabular_data_by_mql(organization_id="<YOUR-ORG-ID>", mql_query=[ |
| 348 | + { '$match': { 'location_id': '<YOUR-LOCATION-ID>' } }, |
| 349 | + { "$limit": 5 } |
348 | 350 | ]) |
349 | 351 |
|
350 | 352 | print(f"Tabular Data: {tabular_data}") |
351 | 353 |
|
352 | 354 | Args: |
353 | 355 | organization_id (str): The ID of the organization that owns the data. |
354 | 356 | You can obtain your organization ID from the Viam app's organization settings page. |
355 | | - mql_binary (List[bytes]): The MQL query to run as a list of BSON queries. You can encode your bson queries using a library like |
356 | | - `pymongo`. |
| 357 | + query (Union[List[bytes], List[Dict[str, Any]]]): The MQL query to run as a list of BSON queries. |
| 358 | + Note: Support for bytes will be removed in the future, so using a dictionary is preferred. |
357 | 359 |
|
358 | 360 | Returns: |
359 | 361 | List[Dict[str, Union[ValueTypes, datetime]]]: An array of decoded BSON data objects. |
360 | 362 |
|
361 | 363 | For more information, see `Data Client API <https://docs.viam.com/appendix/apis/data-client/>`_. |
362 | 364 | """ |
363 | | - request = TabularDataByMQLRequest(organization_id=organization_id, mql_binary=mql_binary) |
| 365 | + binary: List[bytes] = [bson.encode(query) for query in query] if isinstance(query[0], dict) else query # type: ignore |
| 366 | + request = TabularDataByMQLRequest(organization_id=organization_id, mql_binary=binary) |
364 | 367 | response: TabularDataByMQLResponse = await self._data_client.TabularDataByMQL(request, metadata=self._metadata) |
365 | 368 | return [bson.decode(bson_bytes) for bson_bytes in response.raw_data] |
366 | 369 |
|
|
0 commit comments