class Counter(object): """A counter aggregates a series of values. The aggregation kind of the Counter is specified when the Counter is created. The values aggregated must be of an appropriate for the aggregation used. Aggregations supported are listed in the code. (The aggregated value will be reported to the Dataflow service.) Do not create directly; call CounterFactory.get_counter instead. Attributes: name: the name of the counter, a string combine_fn: the CombineFn to use for aggregation accumulator: the accumulator created for the combine_fn """ # Handy references to common counters. SUM = cy_combiners.SumInt64Fn() MEAN = cy_combiners.MeanInt64Fn() BEAM_DISTRIBUTION = cy_combiners.DistributionInt64Fn() # Dataflow Distribution Accumulator Fn. # TODO(BEAM-4045): Generalize distribution counter if necessary. DATAFLOW_DISTRIBUTION = cy_combiners.DataflowDistributionCounterFn() def __init__(self, name, combine_fn): # type: (CounterName, core.CombineFn) -> None """Creates a Counter object. Args: name: the name of this counter. It may be a string, or a CounterName object. combine_fn: the CombineFn to use for aggregation """ self.name = name self.combine_fn = combine_fn self.accumulator = combine_fn.create_accumulator() self._add_input = self.combine_fn.add_input def update(self, value): self.accumulator = self._add_input(self.accumulator, value) def reset(self, value): self.accumulator = self.combine_fn.create_accumulator() def value(self): return self.combine_fn.extract_output(self.accumulator) def __str__(self): return '<%s>' % self._str_internal() def __repr__(self): return '<%s at %s>' % (self._str_internal(), hex(id(self))) def _str_internal(self): return '%s %s %s' % ( self.name, self.combine_fn.__class__.__name__, self.value())
class Counter(object): """A counter aggregates a series of values. The aggregation kind of the Counter is specified when the Counter is created. The values aggregated must be of an appropriate for the aggregation used. Aggregations supported are listed in the code. (The aggregated value will be reported to the Dataflow service.) Do not create directly; call CounterFactory.get_counter instead. Attributes: name: the name of the counter, a string aggregation_kind: one of the aggregation kinds defined by this class. total: the total size of all the items passed to update() elements: the number of times update() was called """ # Handy references to common counters. SUM = cy_combiners.SumInt64Fn() MEAN = cy_combiners.MeanInt64Fn() def __init__(self, name, combine_fn): """Creates a Counter object. Args: name: the name of this counter. Typically has three parts: "step-output-counter". combine_fn: the CombineFn to use for aggregation """ self.name = name self.combine_fn = combine_fn self.accumulator = combine_fn.create_accumulator() self._add_input = self.combine_fn.add_input def update(self, value): self.accumulator = self._add_input(self.accumulator, value) def value(self): return self.combine_fn.extract_output(self.accumulator) def __str__(self): return '<%s>' % self._str_internal() def __repr__(self): return '<%s at %s>' % (self._str_internal(), hex(id(self))) def _str_internal(self): return '%s %s %s' % (self.name, self.combine_fn.__class__.__name__, self.value())