Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions docs/how-tos/sensor-data-query-sdk.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Query sensor data with the Python SDK"
linkTitle: "Query sensor data with SDK"
linkTitle: "Query sensor data with an SDK"
weight: 31
type: "docs"
images: ["/services/icons/data-query.svg"]
Expand Down Expand Up @@ -64,6 +64,17 @@ source .venv/bin/activate
pip install viam-sdk
```

{{% /tablestep %}}
{{% tablestep%}}
**2. Install requirements**

To query data with the Python SDK, you will the `bson` package or the `pymongo` package.
To install `bson`, run the following command:

```sh {class="command-line" data-prompt="$"}
pip install bson
```

{{% /tablestep %}}
{{< /table >}}

Expand All @@ -76,16 +87,23 @@ pip install viam-sdk
To access your machines using the Python SDK, you must use an API key:

```sh {class="command-line" data-prompt="$"}
viam organizations api-key create --org-id <org-id> --name my-api-key
viam organizations api-key create --org-id=<org-id> --name=my-api-key
```

This command uses the Viam CLI.
You can use [`viam organizations list`](/cli/#organizations) to retrieve your organization's ID.
Comment on lines 87 to +94
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we can now get API keys from the organization settings page, we could change this. Then there's no need to install the CLI anymore. Fewer clicks away from this page

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? IMO its a bit simpler than the manual instructions for retrieving api key and organization ids and that would require them to click around in the app anyways. I will change if you think 100%


{{% /tablestep %}}
{{% tablestep link="/appendix/apis/data-client/"%}}
**2. Use the API key with the `data_client`**

Use the API key and [`TabularDataByFilter()`](/appendix/apis/data-client/#tabulardatabyfilter), [`TabularDataBySQL()`](/appendix/apis/data-client/#tabulardatabysql), [`TabularDataByMQL()`](/appendix/apis/data-client/#tabulardatabymql), and[`DeleteTabularData()`](/appendix/apis/data-client/#deletetabulardata) to query data:
Use the API key and [`TabularDataByFilter()`](/appendix/apis/data-client/#tabulardatabyfilter), [`TabularDataBySQL()`](/appendix/apis/data-client/#tabulardatabysql), [`TabularDataByMQL()`](/appendix/apis/data-client/#tabulardatabymql), and[`DeleteTabularData()`](/appendix/apis/data-client/#deletetabulardata) to query data by creating and running the following Python script:

{{% alert title="Note" color="note" %}}
Make sure to replace the value in line 30 with your correct sensor name, line 35 with your organization ID which you can get by running `viam organizations list`, and line 37 with your location ID which you can get by running `viam locations list`.
{{% /alert %}}

```python {class="line-numbers linkable-line-numbers" data-line="28-50"}
```python {class="line-numbers linkable-line-numbers" data-line="29-54, 30, 37, 40"}
import asyncio
import bson

Expand Down Expand Up @@ -114,15 +132,18 @@ async def main():
# Instantiate a DataClient to run data client API methods on
data_client = viam_client.data_client

# TODO: replace "my-sensor" with your correct sensor name
my_filter = Filter(component_name="my-sensor")
data, count, id = await data_client.tabular_data_by_filter(
filter=my_filter, limit=5)
# This query requests all stored data grouped by hour and calculates the
# average, minimum, and maximum of the memory usage
data = await data_client.tabular_data_by_mql(
organization_id='<organization-id>',
# TODO: Replace <ORGANIZATION-ID> with your organization ID
organization_id='<ORGANIZATION-ID>',
mql_binary=[
bson.dumps({'$match': {'location_id': '<location-id>'}}),
# TODO: Replace <LOCATION-ID> with your location ID
bson.dumps({'$match': {'location_id': '<LOCATION-ID>'}}),
bson.dumps({
"$group": {
"_id": {
Expand All @@ -148,7 +169,7 @@ if __name__ == '__main__':
{{% /tablestep %}}
{{< /table >}}

Adjust the Python script to query your data further.
Adjust the Python script to query your data further with the [`data_client` API](/appendix/apis/data-client/#api).

## Next steps

Expand Down
Loading