Skip to content

Commit 9ac48ad

Browse files
authored
RSDK-12467: Add and test historical module data (#1031)
1 parent fd35dd4 commit 9ac48ad

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import datetime
2+
import os
3+
from typing import Any, Dict, List
4+
5+
from viam.app.viam_client import ViamClient
6+
7+
8+
class ResourceDataConsumer:
9+
"""Client for retrieving historical module data from app.
10+
11+
Inherit from this class in a module to get access to historical module data.
12+
"""
13+
14+
@classmethod
15+
def construct_query(cls, part_id: str, resource_name: str, time_back: datetime.timedelta):
16+
return [
17+
{
18+
"$match": {
19+
"part_id": part_id,
20+
"component_name": resource_name,
21+
"time_received": {"$gte": datetime.datetime.now() - time_back},
22+
}
23+
}
24+
]
25+
26+
@classmethod
27+
async def query_tabular_data(cls, resource_name: str, time_back: datetime.timedelta, **kwargs) -> List[Dict[str, Any]]:
28+
"""Return historical data for this module, queried with MQL."""
29+
viam_client = await ViamClient.create_from_env_vars()
30+
31+
org_id = os.environ["VIAM_PRIMARY_ORG_ID"]
32+
part_id = os.environ["VIAM_MACHINE_PART_ID"]
33+
34+
query = cls.construct_query(part_id=part_id, resource_name=resource_name, time_back=time_back)
35+
return await viam_client.data_client.tabular_data_by_mql(org_id, query)

tests/test_module.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import datetime
2+
import os
3+
import uuid
14
from unittest import mock
25

36
import pytest
47
from grpclib.testing import ChannelFor
58

69
from viam.errors import GRPCError
710
from viam.module import Module
11+
from viam.module.resource_data_consumer import ResourceDataConsumer
812
from viam.module.service import ModuleRPCService
913
from viam.proto.app.robot import ComponentConfig
1014
from viam.proto.module import (
@@ -44,6 +48,33 @@ def service(module: Module) -> ModuleRPCService:
4448
return ModuleRPCService(module)
4549

4650

51+
class TestResourceDataConsumer:
52+
async def test_historical_data(self):
53+
with mock.patch("viam.app.data_client.DataClient.tabular_data_by_mql", new=mock.AsyncMock()) as mocked:
54+
with mock.patch("viam.app.viam_client._get_access_token") as patched_auth:
55+
ACCESS_TOKEN = "MY_ACCESS_TOKEN"
56+
patched_auth.return_value = ACCESS_TOKEN
57+
58+
os.environ["VIAM_API_KEY"] = "MY_API_KEY"
59+
os.environ["VIAM_API_KEY_ID"] = str(uuid.uuid4())
60+
os.environ["VIAM_PRIMARY_ORG_ID"] = "my_org"
61+
os.environ["VIAM_MACHINE_PART_ID"] = "my_part"
62+
63+
delta = datetime.timedelta(hours=2)
64+
65+
# Define a helper approx matcher because the time received fields will vary slightly
66+
class DeltaApprox:
67+
def __eq__(self, other):
68+
gte = datetime.datetime.now() - delta
69+
return other - gte < datetime.timedelta(seconds=1)
70+
71+
query = ResourceDataConsumer.construct_query("my_part", "resource", delta)
72+
query[0]["$match"]["time_received"]["$gte"] = DeltaApprox()
73+
74+
await ResourceDataConsumer.query_tabular_data("resource", delta)
75+
mocked.assert_called_once_with("my_org", query)
76+
77+
4778
class TestModule:
4879
async def test_add_resource(self, module: Module):
4980
req = AddResourceRequest(

0 commit comments

Comments
 (0)