Skip to content

Commit c2df09f

Browse files
committed
cloudvision/Connector: Add GetAndSubscribe method to connector
Change-Id: Ia14e830d5f1bd77a22c6c71eab3893cf349b3866
1 parent 262f560 commit c2df09f

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

cloudvision/Connector/grpc_client/grpcClient.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def subscribe(self, queries, sharding=None, timeout: Optional[float] = None):
290290
"""
291291
Subscribe creates and executes a Subscribe protobuf message,
292292
returning a stream of notificationBatch.
293-
queries must be a list of querry protobuf messages.
293+
queries must be a list of query protobuf messages.
294294
sharding, if present must be a protobuf sharding message.
295295
timeout: if present, sets the GRPC timeout in seconds. Default is None (no timeout).
296296
"""
@@ -302,6 +302,42 @@ def subscribe(self, queries, sharding=None, timeout: Optional[float] = None):
302302
stream = self.__client.Subscribe(req, metadata=self.metadata, timeout=timeout)
303303
return (self.decode_batch(nb) for nb in stream)
304304

305+
def getAndSubscribe(
306+
self,
307+
queries: List[rtr.Query],
308+
start: Optional[TIME_TYPE] = None,
309+
versions=0,
310+
sharding=None,
311+
exact_range=False,
312+
timeout: Optional[float] = None,
313+
):
314+
"""
315+
GetAndSubscribe creates and executes a GetAndSubscribe protobuf message,
316+
returning a stream of notificationBatch. This will initially consist of notifications
317+
of the current state in CloudVision (i.e. a Get), after which it will transition to a
318+
subscription method, where update notifications will be received for changes occurring to
319+
the queried paths.
320+
queries must be a list of query protobuf messages.
321+
start, if present, must be a google.protobuf.timestamp_pb2.Timestamp or a datetime object.
322+
versions, if present, specifies the maximum number of versions to retrieve.
323+
sharding, if present must be a protobuf sharding message.
324+
exact_range, if present, specifies whether to return the initial state at time `start`.
325+
timeout: if present, sets the GRPC timeout in seconds. Default is None (no timeout).
326+
"""
327+
start_ts = 0
328+
if start:
329+
start_ts = to_pbts(start).ToNanoseconds()
330+
331+
request = rtr.GetAndSubscribeRequest(
332+
query=queries,
333+
start=start_ts,
334+
versions=versions,
335+
sharded_sub=sharding,
336+
exact_range=exact_range,
337+
)
338+
stream = self.__client.GetAndSubscribe(request, metadata=self.metadata, timeout=timeout)
339+
return (self.decode_batch(nb) for nb in stream)
340+
305341
def publish(
306342
self,
307343
dId,
@@ -359,6 +395,7 @@ def decode_batch(self, batch):
359395
"name": batch.dataset.name,
360396
"type": batch.dataset.type,
361397
},
398+
"metadata": batch.metadata,
362399
"notifications": [self.decode_notification(n) for n in batch.notifications],
363400
}
364401
return res

cloudvision/Connector/grpc_client/grpcConnectionPool.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,47 @@ def wrapped_stream():
187187

188188
return wrapped_stream()
189189

190+
def getAndSubscribe(
191+
self,
192+
queries: List[rtr.Query],
193+
start: Optional[TIME_TYPE] = None,
194+
versions=0,
195+
sharding=None,
196+
exact_range=False,
197+
timeout: Optional[float] = None,
198+
):
199+
"""
200+
GetAndSubscribe creates and executes a GetAndSubscribe protobuf message,
201+
returning a stream of notificationBatch. This will initially consist of notifications
202+
of the current state in CloudVision (i.e. a Get), after which it will transition to a
203+
subscription method, where update notifications will be received for changes occurring to
204+
the queried paths.
205+
queries must be a list of query protobuf messages.
206+
start, if present, must be a google.protobuf.timestamp_pb2.Timestamp or a datetime object.
207+
versions, if present, specifies the maximum number of versions to retrieve.
208+
sharding, if present, must be a protobuf sharding message.
209+
exact_range, if present, specifies whether to return the initial state at time `start`.
210+
timeout: if present, sets the GRPC timeout in seconds. Default is None (no timeout).
211+
"""
212+
client = self._get_or_create_client()
213+
stream = client.getAndSubscribe(
214+
queries=queries,
215+
start=start,
216+
versions=versions,
217+
sharding=sharding,
218+
exact_range=exact_range,
219+
timeout=timeout,
220+
)
221+
222+
def wrapped_stream():
223+
try:
224+
for item in stream:
225+
yield item
226+
finally:
227+
client.release_stream()
228+
229+
return wrapped_stream()
230+
190231
def get(
191232
self,
192233
queries: List[rtr.Query],
@@ -201,7 +242,8 @@ def get(
201242
notificationBatch.
202243
queries must be a list of querry protobuf messages.
203244
start and end, if present, must be nanoseconds timestamps (uint64).
204-
sharding, if present must be a protobuf sharding message.
245+
versions, if present, specifies the maximum number of versions to retrieve.
246+
sharding, if present, must be a protobuf sharding message.
205247
Unary get request — uses any client, does not reserve a stream.
206248
"""
207249
client = self._get_any_client()

0 commit comments

Comments
 (0)