diff --git a/os_performance_tools/collectors/_delta.py b/os_performance_tools/collectors/_delta.py index e824d65..0f61ee8 100644 --- a/os_performance_tools/collectors/_delta.py +++ b/os_performance_tools/collectors/_delta.py @@ -13,7 +13,7 @@ import json -def delta(previous, current): +def delta(previous, current, meta=False): product = {} seen = set() @@ -27,10 +27,17 @@ def delta(previous, current): 'Type of key %s changed from %s to %s' % (k, type(v), type(newv))) - if isinstance(v, int) or isinstance(v, float): + if k == '__meta__': + meta = True + if meta and k == 'delta_seconds': + continue + elif meta and k == 'unixtime': + product['delta_seconds'] = newv - v + product[k] = newv + elif isinstance(v, int) or isinstance(v, float): product[k] = newv - v elif isinstance(v, dict): - product[k] = delta(v, newv) + product[k] = delta(v, newv, meta) else: raise ValueError('Only mappings of numbers are understood') seen.add(k) diff --git a/os_performance_tools/tests/test_collectors_delta.py b/os_performance_tools/tests/test_collectors_delta.py index 39db9bc..89da02a 100644 --- a/os_performance_tools/tests/test_collectors_delta.py +++ b/os_performance_tools/tests/test_collectors_delta.py @@ -60,6 +60,18 @@ class TestOSQATDelta(testscenarios.WithScenarios, base.TestCase): previous={'zoo': {'horse': 7}}, current={'zoo': 15}, expected=ValueError)), + ('meta_unixtime', dict( + previous={'__meta__': {'unixtime': 1.0}}, + current={'__meta__': {'unixtime': 10.5}}, + expected={'__meta__': {'unixtime': 10.5, 'delta_seconds': 9.5}})), + ('meta_unixtime_gone', dict( + previous={'__meta__': {'unixtime': 1.0}}, + current={}, + expected={})), + ('meta_unixtime_new', dict( + previous={}, + current={'__meta__': {'unixtime': 1.0}}, + expected={'__meta__': {'unixtime': 1.0}})), ] def test_delta(self):