Skip to content

Commit da8675b

Browse files
authored
DOCS-2795: QA query sensor data sdk (#3362)
1 parent 55293fb commit da8675b

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

docs/how-tos/sensor-data-query-sdk.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Query sensor data with the Python SDK"
3-
linkTitle: "Query sensor data with SDK"
3+
linkTitle: "Query sensor data with an SDK"
44
weight: 31
55
type: "docs"
66
images: ["/services/icons/data-query.svg"]
@@ -64,6 +64,17 @@ source .venv/bin/activate
6464
pip install viam-sdk
6565
```
6666

67+
{{% /tablestep %}}
68+
{{% tablestep%}}
69+
**2. Install requirements**
70+
71+
To query data with the Python SDK, you will the `bson` package or the `pymongo` package.
72+
To install `bson`, run the following command:
73+
74+
```sh {class="command-line" data-prompt="$"}
75+
pip install bson
76+
```
77+
6778
{{% /tablestep %}}
6879
{{< /table >}}
6980

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

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

93+
This command uses the Viam CLI.
94+
You can use [`viam organizations list`](/cli/#organizations) to retrieve your organization's ID.
95+
8296
{{% /tablestep %}}
8397
{{% tablestep link="/appendix/apis/data-client/"%}}
8498
**2. Use the API key with the `data_client`**
8599

86-
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:
100+
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:
101+
102+
{{% alert title="Note" color="note" %}}
103+
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`.
104+
{{% /alert %}}
87105

88-
```python {class="line-numbers linkable-line-numbers" data-line="28-50"}
106+
```python {class="line-numbers linkable-line-numbers" data-line="29-54, 30, 37, 40"}
89107
import asyncio
90108
import bson
91109

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

135+
# TODO: replace "my-sensor" with your correct sensor name
117136
my_filter = Filter(component_name="my-sensor")
118137
data, count, id = await data_client.tabular_data_by_filter(
119138
filter=my_filter, limit=5)
120139
# This query requests all stored data grouped by hour and calculates the
121140
# average, minimum, and maximum of the memory usage
122141
data = await data_client.tabular_data_by_mql(
123-
organization_id='<organization-id>',
142+
# TODO: Replace <ORGANIZATION-ID> with your organization ID
143+
organization_id='<ORGANIZATION-ID>',
124144
mql_binary=[
125-
bson.dumps({'$match': {'location_id': '<location-id>'}}),
145+
# TODO: Replace <LOCATION-ID> with your location ID
146+
bson.dumps({'$match': {'location_id': '<LOCATION-ID>'}}),
126147
bson.dumps({
127148
"$group": {
128149
"_id": {
@@ -148,7 +169,7 @@ if __name__ == '__main__':
148169
{{% /tablestep %}}
149170
{{< /table >}}
150171

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

153174
## Next steps
154175

0 commit comments

Comments
 (0)