Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 6ba99d5

Browse files
authored
Fix bugs in stats library (#246)
1 parent 2f50120 commit 6ba99d5

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

examples/stats/helloworld/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ def main():
6060

6161
# Get aggregated stats and print it to console.
6262
view_data = view_manager.get_view(VIDEO_SIZE_VIEW_NAME)
63-
pprint(vars(view_data)) # TODO: print more meaningful info for view_data
63+
pprint(vars(view_data))
64+
for k, v in view_data._tag_value_aggregation_map.items():
65+
pprint(vars(v))
66+
pprint(vars(v.aggregation_data))
6467

6568

6669
if __name__ == '__main__':

opencensus/stats/aggregation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, sum=None, aggregation_type=Type.SUM):
7575
super(SumAggregation, self).__init__(aggregation_type=aggregation_type)
7676
self._sum = aggregation_data.SumAggregationDataFloat(
7777
sum_data=float(sum or 0))
78+
self.aggregation_data = self._sum
7879

7980
@property
8081
def sum(self):
@@ -97,6 +98,7 @@ def __init__(self, count=0, aggregation_type=Type.COUNT):
9798
super(CountAggregation, self).__init__(
9899
aggregation_type=aggregation_type)
99100
self._count = aggregation_data.CountAggregationData(count)
101+
self.aggregation_data = self._count
100102

101103
@property
102104
def count(self):
@@ -128,6 +130,8 @@ def __init__(
128130
buckets=boundaries, aggregation_type=aggregation_type)
129131
self._boundaries = bucket_boundaries.BucketBoundaries(boundaries)
130132
self._distribution = distribution or {}
133+
self.aggregation_data = aggregation_data.DistributionAggregationData(
134+
0, 0, 0, 0, 0, None, boundaries)
131135

132136
@property
133137
def boundaries(self):

opencensus/stats/aggregation_data.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,22 @@ def __init__(self,
107107
min_,
108108
max_,
109109
sum_of_sqd_deviations,
110-
counts_per_bucket,
111-
bounds):
110+
counts_per_bucket=None,
111+
bounds=None):
112112
super(DistributionAggregationData, self).__init__(mean_data)
113113
self._mean_data = mean_data
114114
self._count_data = count_data
115115
self._min = min_
116116
self._max = max_
117117
self._sum_of_sqd_deviations = sum_of_sqd_deviations
118+
if bounds is None:
119+
bounds = []
120+
121+
if counts_per_bucket is None:
122+
counts_per_bucket = []
123+
bucket_size = len(bounds) + 1
124+
for i in range(bucket_size):
125+
counts_per_bucket.append(0)
118126
self._counts_per_bucket = counts_per_bucket
119127
self._bounds = bucket_boundaries.BucketBoundaries(
120128
boundaries=bounds).boundaries

opencensus/stats/view_data.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ def end(self):
7474
def get_tag_map(self, context):
7575
"""function to return the tag map based on the context"""
7676
if self.tag_map is not None:
77-
if context.items() <= self.tag_map.items():
77+
if context.map.items() <= self.tag_map.items():
7878
return self.tag_map
7979
else:
8080
tags = self.tag_map
81-
for tag_key, tag_value in context.items():
81+
for tag_key, tag_value in context.map.items():
8282
tags[tag_key] = tag_value
8383
return tags
8484
else: # pragma: NO COVER
@@ -105,4 +105,5 @@ def record(self, context, value, timestamp):
105105
for val in tuple_vals:
106106
if val not in self.tag_value_aggregation_map:
107107
self.tag_value_aggregation_map[val] = self.view.aggregation
108-
self.tag_value_aggregation_map.get(val).add(value)
108+
self.tag_value_aggregation_map.get(
109+
val).aggregation_data.add_sample(value)

tests/unit/stats/test_view_data.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,21 @@ def test_get_tag_map(self):
6060
view_data = view_data_module.ViewData(view=view,
6161
start_time=start_time,
6262
end_time=end_time)
63-
test_context_1 = {'key1': 'val1'}
63+
test_context_1 = mock.Mock()
64+
test_context_1.map = {'key1': 'val1'}
6465
context_map_1 = view_data.get_tag_map(context=test_context_1)
65-
self.assertEqual(test_context_1, view_data.tag_map)
66-
self.assertEqual(test_context_1, context_map_1)
66+
self.assertEqual(test_context_1.map, view_data.tag_map)
67+
self.assertEqual(test_context_1.map, context_map_1)
6768

68-
test_context_2 = {'key1': 'val2'}
69+
test_context_2 = mock.Mock()
70+
test_context_2.map = {'key1': 'val2'}
6971
context_map_2 = view_data.get_tag_map(context=test_context_2)
70-
self.assertEqual(test_context_2, context_map_2)
72+
self.assertEqual(test_context_2.map, context_map_2)
7173

72-
test_context_3 = {}
74+
test_context_3 = mock.Mock()
75+
test_context_3.map = {}
7376
context_map_3 = view_data.get_tag_map(context=test_context_3)
74-
self.assertEqual({'key1': 'val2'}, context_map_3)
77+
self.assertEqual(test_context_2.map, context_map_3)
7578

7679
def test_get_tag_values(self):
7780
view = mock.Mock()
@@ -101,7 +104,8 @@ def test_record(self):
101104
start_time=start_time,
102105
end_time=end_time)
103106

104-
context = {'key1': 'val1', 'key2': 'val2'}
107+
context = mock.Mock()
108+
context.map = {'key1': 'val1', 'key2': 'val2'}
105109
time = datetime.utcnow().isoformat() + 'Z'
106110
value = 1
107111
self.assertEqual({}, view_data.tag_value_aggregation_map)

0 commit comments

Comments
 (0)