Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8358cb

Browse files
committed
Bounded Trie translation.
1 parent f83c683 commit d8358cb

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

sdks/python/apache_beam/metrics/cells.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from typing import Optional
3333
from typing import Set
3434

35+
from apache_beam.portability.api import metrics_pb2
36+
3537
try:
3638
import cython
3739
except ImportError:
@@ -669,6 +671,29 @@ def __init__(self):
669671
self._children: Optional[dict[str, '_BoundedTrieNode']] = {}
670672
self._truncated = False
671673

674+
def to_proto(self) -> metrics_pb2.BoundedTrieNode:
675+
return metrics_pb2.BoundedTrieNode(
676+
truncated=self._truncated,
677+
children={
678+
name: child.to_proto()
679+
for name, child in self._children.items()
680+
} if self._children else None)
681+
682+
@staticmethod
683+
def from_proto(proto: metrics_pb2.BoundedTrieNode) -> '_BoundedTrieNode':
684+
node = _BoundedTrieNode()
685+
if proto.truncated:
686+
node._truncated = True
687+
node._children = None
688+
else:
689+
node._children = {
690+
name: _BoundedTrieNode.from_proto(child)
691+
for name,
692+
child in proto.children.items()
693+
}
694+
node._size = min(1, sum(child._size for child in node._children.values()))
695+
return node
696+
672697
def size(self):
673698
return self._size
674699

@@ -764,6 +789,19 @@ def __init__(self, *, root=None, singleton=None, bound=_DEFAULT_BOUND):
764789
self._root = root
765790
self._bound = bound
766791

792+
def to_proto(self) -> metrics_pb2.BoundedTrie:
793+
return metrics_pb2.BoundedTrie(
794+
bound=self._bound,
795+
singleton=self._singlton if self._singleton else None,
796+
root=self._root.to_proto() if self._root else None)
797+
798+
@staticmethod
799+
def from_proto(proto: metrics_pb2.BoundedTrie) -> 'BoundedTrieData':
800+
return BoundedTrieData(
801+
bound=proto.bound,
802+
singleton=tuple(proto.singleton) if proto.singleton else None,
803+
root=_BoundedTrieNode.from_proto(proto.root) if proto.root else None)
804+
767805
def as_trie(self):
768806
if self._root is not None:
769807
return self._root

sdks/python/apache_beam/metrics/monitoring_infos.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@
5050
common_urns.monitoring_info_specs.USER_DISTRIBUTION_INT64.spec.urn)
5151
USER_GAUGE_URN = common_urns.monitoring_info_specs.USER_LATEST_INT64.spec.urn
5252
USER_STRING_SET_URN = common_urns.monitoring_info_specs.USER_SET_STRING.spec.urn
53+
USER_BOUNDED_TRIE_URN = (
54+
common_urns.monitoring_info_specs.USER_BOUNDED_TRIE.spec.urn)
5355
USER_METRIC_URNS = set([
5456
USER_COUNTER_URN,
5557
USER_DISTRIBUTION_URN,
5658
USER_GAUGE_URN,
57-
USER_STRING_SET_URN
59+
USER_STRING_SET_URN,
60+
USER_BOUNDED_TRIE_URN,
5861
])
5962
WORK_REMAINING_URN = common_urns.monitoring_info_specs.WORK_REMAINING.spec.urn
6063
WORK_COMPLETED_URN = common_urns.monitoring_info_specs.WORK_COMPLETED.spec.urn
@@ -72,11 +75,13 @@
7275
LATEST_INT64_TYPE = common_urns.monitoring_info_types.LATEST_INT64_TYPE.urn
7376
PROGRESS_TYPE = common_urns.monitoring_info_types.PROGRESS_TYPE.urn
7477
STRING_SET_TYPE = common_urns.monitoring_info_types.SET_STRING_TYPE.urn
78+
BOUNDED_TRIE_TYPE = common_urns.monitoring_info_types.BOUNDED_TRIE_TYPE.urn
7579

7680
COUNTER_TYPES = set([SUM_INT64_TYPE])
7781
DISTRIBUTION_TYPES = set([DISTRIBUTION_INT64_TYPE])
7882
GAUGE_TYPES = set([LATEST_INT64_TYPE])
7983
STRING_SET_TYPES = set([STRING_SET_TYPE])
84+
BOUNDED_TRIE_TYPES = set([BOUNDED_TRIE_TYPE])
8085

8186
# TODO(migryz) extract values from beam_fn_api.proto::MonitoringInfoLabels
8287
PCOLLECTION_LABEL = (
@@ -320,6 +325,23 @@ def user_set_string(namespace, name, metric, ptransform=None):
320325
USER_STRING_SET_URN, STRING_SET_TYPE, metric, labels)
321326

322327

328+
def user_bounded_trie(namespace, name, metric, ptransform=None):
329+
"""Return the string set monitoring info for the URN, metric and labels.
330+
331+
Args:
332+
namespace: User-defined namespace of BoundedTrie.
333+
name: Name of BoundedTrie.
334+
metric: The BoundedTrieData representing the metrics.
335+
ptransform: The ptransform id used as a label.
336+
"""
337+
labels = create_labels(ptransform=ptransform, namespace=namespace, name=name)
338+
return create_monitoring_info(
339+
USER_BOUNDED_TRIE_URN,
340+
BOUNDED_TRIE_TYPE,
341+
metric.to_proto().SerializeToString(),
342+
labels)
343+
344+
323345
def create_monitoring_info(
324346
urn, type_urn, payload, labels=None) -> metrics_pb2.MonitoringInfo:
325347
"""Return the gauge monitoring info for the URN, type, metric and labels.

0 commit comments

Comments
 (0)