Skip to content

Commit

Permalink
Use tag_value_aggregation_data_map instead of tag_value_aggregation_map.
Browse files Browse the repository at this point in the history
  • Loading branch information
songy23 committed Aug 9, 2018
1 parent b85e936 commit 80b7bc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/stats/helloworld/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def main():
# Get aggregated stats and print it to console.
view_data = view_manager.get_view(VIDEO_SIZE_VIEW_NAME)
pprint(vars(view_data))
for k, v in view_data._tag_value_aggregation_map.items():
for k, v in view_data._tag_value_aggregation_data_map.items():
pprint(k)
pprint(vars(v))
pprint(vars(v.aggregation_data))


if __name__ == '__main__':
Expand Down
15 changes: 7 additions & 8 deletions opencensus/stats/view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self,
self._view = view
self._start_time = start_time
self._end_time = end_time
self._tag_value_aggregation_map = {}
self._tag_value_aggregation_data_map = {}

@property
def view(self):
Expand All @@ -54,9 +54,9 @@ def end_time(self):
return self._end_time

@property
def tag_value_aggregation_map(self):
def tag_value_aggregation_data_map(self):
"""the current tag value aggregation map in the view data"""
return self._tag_value_aggregation_map
return self._tag_value_aggregation_data_map

def start(self):
"""sets the start time for the view data"""
Expand Down Expand Up @@ -84,8 +84,7 @@ def record(self, context, value, timestamp):
tag_values = self.get_tag_values(tags=context.map,
columns=self.view.columns)
tuple_vals = tuple(tag_values)
if tuple_vals not in self.tag_value_aggregation_map:
self.tag_value_aggregation_map[tuple_vals] = copy.deepcopy(
self.view.aggregation)
self.tag_value_aggregation_map.get(
tuple_vals).aggregation_data.add_sample(value)
if tuple_vals not in self.tag_value_aggregation_data_map:
self.tag_value_aggregation_data_map[tuple_vals] = copy.deepcopy(
self.view.aggregation.aggregation_data)
self.tag_value_aggregation_data_map.get(tuple_vals).add_sample(value)
41 changes: 19 additions & 22 deletions tests/unit/stats/test_view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_constructor(self):
self.assertEqual(view, view_data.view)
self.assertEqual(start_time, view_data.start_time)
self.assertEqual(end_time, view_data.end_time)
self.assertEqual({}, view_data.tag_value_aggregation_map)
self.assertEqual({}, view_data.tag_value_aggregation_data_map)

def test_start(self):
view = mock.Mock()
Expand Down Expand Up @@ -87,28 +87,28 @@ def test_record(self):
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
value = 1
self.assertEqual({}, view_data.tag_value_aggregation_map)
self.assertEqual({}, view_data.tag_value_aggregation_data_map)

view_data.record(context=context, value=value, timestamp=time)
tag_values = view_data.get_tag_values(
tags=context.map, columns=view.columns)
tuple_vals = tuple(tag_values)
self.assertEqual(['val1'], tag_values)
self.assertIsNotNone(view_data.tag_value_aggregation_map)
self.assertIsNotNone(view_data.tag_value_aggregation_data_map)

self.assertTrue(tuple_vals in view_data.tag_value_aggregation_map)
self.assertIsNotNone(view_data.tag_value_aggregation_map[tuple_vals])
self.assertIsNotNone(view_data.tag_value_aggregation_map.get(
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_data_map)
self.assertIsNotNone(view_data.tag_value_aggregation_data_map[tuple_vals])
self.assertIsNotNone(view_data.tag_value_aggregation_data_map.get(
tuple_vals).add(value))

view_data.record(context=context, value=value, timestamp=time)
tag_values.append('val2')
tuple_vals_2 = tuple(['val2'])
self.assertFalse(tuple_vals_2 in view_data.tag_value_aggregation_map)
view_data.tag_value_aggregation_map[tuple_vals_2] = view.aggregation
self.assertEqual(view_data.tag_value_aggregation_map.get(tuple_vals_2),
self.assertFalse(tuple_vals_2 in view_data.tag_value_aggregation_data_map)
view_data.tag_value_aggregation_data_map[tuple_vals_2] = view.aggregation
self.assertEqual(view_data.tag_value_aggregation_data_map.get(tuple_vals_2),
view_data.view.aggregation)
self.assertIsNotNone(view_data.tag_value_aggregation_map.get(
self.assertIsNotNone(view_data.tag_value_aggregation_data_map.get(
tuple_vals_2).add(value))


Expand All @@ -127,18 +127,17 @@ def test_record_with_multi_keys(self):
context.map = {'key1': 'val1', 'key2': 'val2'}
time = datetime.utcnow().isoformat() + 'Z'
value = 1
self.assertEqual({}, view_data.tag_value_aggregation_map)
self.assertEqual({}, view_data.tag_value_aggregation_data_map)

view_data.record(context=context, value=value, timestamp=time)
tag_values = view_data.get_tag_values(
tags=context.map, columns=view.columns)
tuple_vals = tuple(tag_values)
self.assertEqual(['val1', 'val2'], tag_values)
self.assertIsNotNone(view_data.tag_value_aggregation_map)
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_map)
self.assertIsNotNone(view_data.tag_value_aggregation_map[tuple_vals])
sum_data = view_data.tag_value_aggregation_map.get(
tuple_vals).aggregation_data
self.assertIsNotNone(view_data.tag_value_aggregation_data_map)
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_data_map)
self.assertIsNotNone(view_data.tag_value_aggregation_data_map[tuple_vals])
sum_data = view_data.tag_value_aggregation_data_map.get(tuple_vals)
self.assertEqual(1, sum_data.sum_data)

context_2 = mock.Mock()
Expand All @@ -150,9 +149,8 @@ def test_record_with_multi_keys(self):
tags=context_2.map, columns=view.columns)
tuple_vals_2 = tuple(tag_values_2)
self.assertEqual(['val3', 'val2'], tag_values_2)
self.assertTrue(tuple_vals_2 in view_data.tag_value_aggregation_map)
sum_data_2 = view_data.tag_value_aggregation_map.get(
tuple_vals_2).aggregation_data
self.assertTrue(tuple_vals_2 in view_data.tag_value_aggregation_data_map)
sum_data_2 = view_data.tag_value_aggregation_data_map.get(tuple_vals_2)
self.assertEqual(2, sum_data_2.sum_data)

time_3 = datetime.utcnow().isoformat() + 'Z'
Expand Down Expand Up @@ -184,7 +182,6 @@ def test_record_with_missing_key_in_context(self):
tags=context.map, columns=view.columns)
tuple_vals = tuple(tag_values)
self.assertEqual(['val1', None], tag_values)
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_map)
sum_data = view_data.tag_value_aggregation_map.get(
tuple_vals).aggregation_data
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_data_map)
sum_data = view_data.tag_value_aggregation_data_map.get(tuple_vals)
self.assertEqual(4, sum_data.sum_data)

0 comments on commit 80b7bc4

Please sign in to comment.