1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import copy
1516import logging
1617
18+ from opencensus .metrics .export import point
19+ from opencensus .metrics .export import value
1720from opencensus .stats import bucket_boundaries
1821
1922
@@ -36,6 +39,18 @@ def aggregation_data(self):
3639 """The current aggregation data"""
3740 return self ._aggregation_data
3841
42+ def to_point (self , timestamp ):
43+ """Get a Point conversion of this aggregation.
44+
45+ :type timestamp: :class: `datetime.datetime`
46+ :param timestamp: The time to report the point as having been recorded.
47+
48+ :rtype: :class: `opencensus.metrics.export.point.Point`
49+ :return: a Point with with this aggregation's value and appropriate
50+ value type.
51+ """
52+ raise NotImplementedError # pragma: NO COVER
53+
3954
4055class SumAggregationDataFloat (BaseAggregationData ):
4156 """Sum Aggregation Data is the aggregated data for the Sum aggregation
@@ -60,6 +75,18 @@ def sum_data(self):
6075 """The current sum data"""
6176 return self ._sum_data
6277
78+ def to_point (self , timestamp ):
79+ """Get a Point conversion of this aggregation.
80+
81+ :type timestamp: :class: `datetime.datetime`
82+ :param timestamp: The time to report the point as having been recorded.
83+
84+ :rtype: :class: `opencensus.metrics.export.point.Point`
85+ :return: a :class: `opencensus.metrics.export.value.ValueDouble`-valued
86+ Point with value equal to `sum_data`.
87+ """
88+ return point .Point (value .ValueDouble (self .sum_data ), timestamp )
89+
6390
6491class CountAggregationData (BaseAggregationData ):
6592 """Count Aggregation Data is the count value of aggregated data
@@ -83,6 +110,18 @@ def count_data(self):
83110 """The current count data"""
84111 return self ._count_data
85112
113+ def to_point (self , timestamp ):
114+ """Get a Point conversion of this aggregation.
115+
116+ :type timestamp: :class: `datetime.datetime`
117+ :param timestamp: The time to report the point as having been recorded.
118+
119+ :rtype: :class: `opencensus.metrics.export.point.Point`
120+ :return: a :class: `opencensus.metrics.export.value.ValueLong`-valued
121+ Point with value equal to `count_data`.
122+ """
123+ return point .Point (value .ValueLong (self .count_data ), timestamp )
124+
86125
87126class DistributionAggregationData (BaseAggregationData ):
88127 """Distribution Aggregation Data refers to the distribution stats of
@@ -123,34 +162,37 @@ def __init__(self,
123162 counts_per_bucket = None ,
124163 bounds = None ,
125164 exemplars = None ):
165+ if bounds is None and exemplars is not None :
166+ raise ValueError
167+ if exemplars is not None and len (exemplars ) != len (bounds ) + 1 :
168+ raise ValueError
169+
126170 super (DistributionAggregationData , self ).__init__ (mean_data )
127171 self ._mean_data = mean_data
128172 self ._count_data = count_data
129173 self ._min = min_
130174 self ._max = max_
131175 self ._sum_of_sqd_deviations = sum_of_sqd_deviations
176+
132177 if bounds is None :
133178 bounds = []
179+ self ._exemplars = None
134180 else :
135181 assert bounds == list (sorted (set (bounds )))
136182 assert all (bb > 0 for bb in bounds )
183+ if exemplars is None :
184+ self ._exemplars = {ii : None for ii in range (len (bounds ) + 1 )}
185+ else :
186+ self ._exemplars = {ii : ex for ii , ex in enumerate (exemplars )}
187+ self ._bounds = (bucket_boundaries .BucketBoundaries (boundaries = bounds )
188+ .boundaries )
137189
138190 if counts_per_bucket is None :
139191 counts_per_bucket = [0 for ii in range (len (bounds ) + 1 )]
140192 else :
141193 assert all (cc >= 0 for cc in counts_per_bucket )
142194 assert len (counts_per_bucket ) == len (bounds ) + 1
143-
144195 self ._counts_per_bucket = counts_per_bucket
145- self ._bounds = bucket_boundaries .BucketBoundaries (
146- boundaries = bounds ).boundaries
147- bucket = 0
148- for _ in self .bounds :
149- bucket = bucket + 1
150-
151- # If there is no histogram, do not record an exemplar
152- self ._exemplars = \
153- {bucket : exemplars } if len (self ._bounds ) > 0 else None
154196
155197 @property
156198 def mean_data (self ):
@@ -240,6 +282,43 @@ def increment_bucket_count(self, value):
240282 self ._counts_per_bucket [last_bucket_index ] += 1
241283 return last_bucket_index
242284
285+ def to_point (self , timestamp ):
286+ """Get a Point conversion of this aggregation.
287+
288+ This method creates a :class: `opencensus.metrics.export.point.Point`
289+ with a :class: `opencensus.metrics.export.value.ValueDistribution`
290+ value, and creates buckets and exemplars for that distribution from the
291+ appropriate classes in the `metrics` package.
292+
293+ :type timestamp: :class: `datetime.datetime`
294+ :param timestamp: The time to report the point as having been recorded.
295+
296+ :rtype: :class: `opencensus.metrics.export.point.Point`
297+ :return: a :class: `opencensus.metrics.export.value.ValueDistribution`
298+ -valued Point.
299+ """
300+ buckets = [None ] * len (self .counts_per_bucket )
301+ for ii , count in enumerate (self .counts_per_bucket ):
302+ stat_ex = self .exemplars .get (ii , None )
303+ if stat_ex is not None :
304+ metric_ex = value .Exemplar (stat_ex .value , stat_ex .timestamp ,
305+ copy .copy (stat_ex .attachments ))
306+ buckets [ii ] = value .Bucket (count , metric_ex )
307+ else :
308+ buckets [ii ] = value .Bucket (count )
309+
310+ bucket_options = value .BucketOptions (value .Explicit (self .bounds ))
311+ return point .Point (
312+ value .ValueDistribution (
313+ count = self .count_data ,
314+ sum_ = self .sum ,
315+ sum_of_squared_deviation = self .sum_of_sqd_deviations ,
316+ bucket_options = bucket_options ,
317+ buckets = buckets
318+ ),
319+ timestamp
320+ )
321+
243322
244323class LastValueAggregationData (BaseAggregationData ):
245324 """
@@ -265,6 +344,18 @@ def value(self):
265344 """The current value recorded"""
266345 return self ._value
267346
347+ def to_point (self , timestamp ):
348+ """Get a Point conversion of this aggregation.
349+
350+ :type timestamp: :class: `datetime.datetime`
351+ :param timestamp: The time to report the point as having been recorded.
352+
353+ :rtype: :class: `opencensus.metrics.export.point.Point`
354+ :return: a :class: `opencensus.metrics.export.value.ValueDouble`-valued
355+ Point.
356+ """
357+ return point .Point (value .ValueDouble (self .value ), timestamp )
358+
268359
269360class Exemplar (object ):
270361 """ Exemplar represents an example point that may be used to annotate
0 commit comments