14
14
BinaryDataByIDsRequest ,
15
15
BinaryDataByIDsResponse ,
16
16
BinaryID ,
17
+ BinaryMetadata ,
17
18
BoundingBoxLabelsByFilterRequest ,
18
19
BoundingBoxLabelsByFilterResponse ,
19
20
CaptureInterval ,
21
+ CaptureMetadata ,
20
22
DataRequest ,
21
23
DataServiceStub ,
22
24
DeleteBinaryDataByFilterRequest ,
@@ -60,6 +62,54 @@ class DataClient:
60
62
`ViamClient`.
61
63
"""
62
64
65
+ class TabularData :
66
+ """Class representing a piece of tabular data and associated metadata.
67
+
68
+ Args:
69
+ data (Mapping[str, Any]): the requested data.
70
+ metadata (viam.proto.app.data.CaptureMetadata): the metadata from the request.
71
+ time_requested (datetime): the time the data request was sent.
72
+ time_received (datetime): the time the requested data was received.
73
+ """
74
+
75
+ def __init__ (self , data : Mapping [str , Any ], metadata : CaptureMetadata , time_requested : datetime , time_received : datetime ) -> None :
76
+ self .data = data
77
+ self .metadata = metadata
78
+ self .time_requested = time_requested
79
+ self .time_received = time_received
80
+
81
+ data : Mapping [str , Any ]
82
+ metadata : CaptureMetadata
83
+ time_requested : datetime
84
+ time_received : datetime
85
+
86
+ def __str__ (self ) -> str :
87
+ return f"{ self .data } \n { self .metadata } Time requested: { self .time_requested } \n Time received: { self .time_received } \n "
88
+
89
+ def __eq__ (self , other : "DataClient.TabularData" ) -> bool :
90
+ return str (self ) == str (other )
91
+
92
+ class BinaryData :
93
+ """Class representing a piece of binary data and associated metadata.
94
+
95
+ Args:
96
+ data (bytes): the requested data.
97
+ metadata (viam.proto.app.data.BinaryMetadata): the metadata from the request.
98
+ """
99
+
100
+ def __init__ (self , data : bytes , metadata : BinaryMetadata ) -> None :
101
+ self .data = data
102
+ self .metadata = metadata
103
+
104
+ data : bytes
105
+ metadata : BinaryMetadata
106
+
107
+ def __str__ (self ) -> str :
108
+ return f"{ self .data } \n { self .metadata } "
109
+
110
+ def __eq__ (self , other : "DataClient.BinaryData" ) -> bool :
111
+ return str (self ) == str (other )
112
+
63
113
def __init__ (self , channel : Channel , metadata : Mapping [str , str ]):
64
114
"""Create a `DataClient` that maintains a connection to app.
65
115
@@ -79,7 +129,7 @@ async def tabular_data_by_filter(
79
129
self ,
80
130
filter : Optional [Filter ] = None ,
81
131
dest : Optional [str ] = None ,
82
- ) -> List [Mapping [ str , Any ] ]:
132
+ ) -> List [TabularData ]:
83
133
"""Filter and download tabular data.
84
134
85
135
Args:
@@ -102,13 +152,18 @@ async def tabular_data_by_filter(
102
152
response : TabularDataByFilterResponse = await self ._data_client .TabularDataByFilter (request , metadata = self ._metadata )
103
153
if not response .data or len (response .data ) == 0 :
104
154
break
105
- data += [struct_to_dict (struct .data ) for struct in response .data ]
155
+ data += [DataClient .TabularData (
156
+ struct_to_dict (struct .data ),
157
+ response .metadata [struct .metadata_index ],
158
+ struct .time_requested .ToDatetime (),
159
+ struct .time_received .ToDatetime (),
160
+ ) for struct in response .data ]
106
161
last = response .last
107
162
108
163
if dest :
109
164
try :
110
165
file = open (dest , "w" )
111
- file .write (f"{ data } " )
166
+ file .write (f"{ [ str ( d ) for d in data ] } " )
112
167
except Exception as e :
113
168
LOGGER .error (f"Failed to write tabular data to file { dest } " , exc_info = e )
114
169
return data
@@ -117,7 +172,7 @@ async def binary_data_by_filter(
117
172
self ,
118
173
filter : Optional [Filter ] = None ,
119
174
dest : Optional [str ] = None ,
120
- ) -> List [bytes ]:
175
+ ) -> List [BinaryData ]:
121
176
"""Filter and download binary data.
122
177
123
178
Args:
@@ -139,13 +194,13 @@ async def binary_data_by_filter(
139
194
response : BinaryDataByFilterResponse = await self ._data_client .BinaryDataByFilter (request , metadata = self ._metadata )
140
195
if not response .data or len (response .data ) == 0 :
141
196
break
142
- data += [data .binary for data in response .data ]
197
+ data += [DataClient . BinaryData ( data .binary , data . metadata ) for data in response .data ]
143
198
last = response .last
144
199
145
200
if dest :
146
201
try :
147
202
file = open (dest , "w" )
148
- file .write (f"{ data } " )
203
+ file .write (f"{ [ str ( d ) for d in data ] } " )
149
204
except Exception as e :
150
205
LOGGER .error (f"Failed to write binary data to file { dest } " , exc_info = e )
151
206
@@ -155,7 +210,7 @@ async def binary_data_by_ids(
155
210
self ,
156
211
binary_ids : List [BinaryID ],
157
212
dest : Optional [str ] = None ,
158
- ) -> List [bytes ]:
213
+ ) -> List [BinaryData ]:
159
214
"""Filter and download binary data.
160
215
161
216
Args:
@@ -176,7 +231,7 @@ async def binary_data_by_ids(
176
231
file .write (f"{ response .data } " )
177
232
except Exception as e :
178
233
LOGGER .error (f"Failed to write binary data to file { dest } " , exc_info = e )
179
- return [binary_data . binary for binary_data in response .data ]
234
+ return [DataClient . BinaryData ( data . binary , data . metadata ) for data in response .data ]
180
235
181
236
async def delete_tabular_data_by_filter (self , filter : Optional [Filter ]) -> int :
182
237
"""Filter and delete tabular data.
0 commit comments