This is an automated email from the ASF dual-hosted git repository. chamikara pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/beam.git
commit f13afb400f0c13d616cd5b66ad2b10f05737be23 Merge: bb8c12c 7edbc73 Author: Chamikara Jayalath <[email protected]> AuthorDate: Fri Feb 16 09:56:58 2018 -0800 Merge pull request #4542: [BEAM-1618] Adding Gauge metric to Python SDK. sdks/python/apache_beam/metrics/cells.py | 149 +++++++++++++++++++-- sdks/python/apache_beam/metrics/cells_test.py | 29 ++++ sdks/python/apache_beam/metrics/execution.pxd | 1 + sdks/python/apache_beam/metrics/execution.py | 19 ++- sdks/python/apache_beam/metrics/execution_test.py | 15 +++ sdks/python/apache_beam/metrics/metric.py | 27 +++- sdks/python/apache_beam/metrics/metric_test.py | 8 ++ sdks/python/apache_beam/metrics/metricbase.py | 21 ++- .../runners/dataflow/dataflow_metrics.py | 2 +- .../apache_beam/runners/direct/direct_metrics.py | 14 +- sdks/python/apache_beam/runners/runner_test.py | 10 ++ 11 files changed, 278 insertions(+), 17 deletions(-) diff --cc sdks/python/apache_beam/metrics/cells.py index 7745667,f430f3a..04ace7d --- a/sdks/python/apache_beam/metrics/cells.py +++ b/sdks/python/apache_beam/metrics/cells.py @@@ -23,9 -23,9 +23,10 @@@ context Cells depend on a 'dirty-bit' in the CellCommitState class that tracks whether a cell's updates have been committed. """ +from __future__ import division import threading + import time from apache_beam.metrics.metricbase import Counter from apache_beam.metrics.metricbase import Distribution @@@ -236,9 -276,80 +277,80 @@@ class DistributionResult(object) """ if self.data.count == 0: return None - return float(self.data.sum)/self.data.count + return self.data.sum / self.data.count + class GaugeResult(object): + def __init__(self, data): + self.data = data + + def __eq__(self, other): + if isinstance(other, GaugeResult): + return self.data == other.data + else: + return False + + def __ne__(self, other): + return not self.__eq__(other) + + def __repr__(self): + return '<GaugeResult(value={}, timestamp={})>'.format( + self.value, + self.timestamp) + + @property + def value(self): + return self.data.value + + @property + def timestamp(self): + return self.data.timestamp + + + class GaugeData(object): + """For internal use only; no backwards-compatibility guarantees. + + The data structure that holds data about a gauge metric. + + Gauge metrics are restricted to integers only. + + This object is not thread safe, so it's not supposed to be modified + by other than the GaugeCell that contains it. + """ + def __init__(self, value, timestamp=None): + self.value = value + self.timestamp = timestamp if timestamp is not None else 0 + + def __eq__(self, other): + return self.value == other.value and self.timestamp == other.timestamp + + def __ne__(self, other): + return not self.__eq__(other) + + def __repr__(self): + return '<GaugeData(value={}, timestamp={})>'.format( + self.value, + self.timestamp) + + def get_cumulative(self): + return GaugeData(self.value, timestamp=self.timestamp) + + def combine(self, other): + if other is None: + return self + + if other.timestamp > self.timestamp: + return other + else: + return self + + @staticmethod + def singleton(value, timestamp=None): + return GaugeData(value, timestamp=timestamp) + + #TODO(pabloem) - Add to_runner_api, and from_runner_api + + class DistributionData(object): """For internal use only; no backwards-compatibility guarantees. -- To stop receiving notification emails like this one, please contact [email protected].
