Update metering unit selection to use smaller units for time
Update the unit selection to not convert to a higher unit until the max value is at least 2 units. Change-Id: I349b328b553ed634a70b70b695d4db92295c9b67 Closes-bug: #1384248
This commit is contained in:
parent
1b0a888390
commit
4f15492980
@ -440,18 +440,19 @@ class UnitsTests(test.TestCase):
|
|||||||
self.assertEqual(units.normalize(0.0001, 'MB'), (104.9, 'B'))
|
self.assertEqual(units.normalize(0.0001, 'MB'), (104.9, 'B'))
|
||||||
|
|
||||||
self.assertEqual(units.normalize(1, 's'), (1, 's'))
|
self.assertEqual(units.normalize(1, 's'), (1, 's'))
|
||||||
self.assertEqual(units.normalize(60, 's'), (1, 'min'))
|
self.assertEqual(units.normalize(120, 's'), (2, 'min'))
|
||||||
self.assertEqual(units.normalize(3600, 's'), (1, 'hr'))
|
self.assertEqual(units.normalize(3600, 's'), (60, 'min'))
|
||||||
self.assertEqual(units.normalize(3600 * 24, 's'), (1, 'day'))
|
self.assertEqual(units.normalize(3600 * 24, 's'), (24, 'hr'))
|
||||||
self.assertEqual(units.normalize(10 * 3600 * 24, 's'), (1.4, 'week'))
|
self.assertEqual(units.normalize(10 * 3600 * 24, 's'), (10, 'day'))
|
||||||
self.assertEqual(units.normalize(60, 'min'), (1, 'hr'))
|
self.assertEqual(units.normalize(90, 'min'), (90, 'min'))
|
||||||
self.assertEqual(units.normalize(90, 'min'), (1.5, 'hr'))
|
self.assertEqual(units.normalize(150, 'min'), (2.5, 'hr'))
|
||||||
self.assertEqual(units.normalize(60 * 24, 'min'), (1, 'day'))
|
self.assertEqual(units.normalize(60 * 24, 'min'), (24, 'hr'))
|
||||||
self.assertEqual(units.normalize(0.5, 'day'), (12, 'hr'))
|
self.assertEqual(units.normalize(0.5, 'day'), (12, 'hr'))
|
||||||
self.assertEqual(units.normalize(10800000000000, 'ns'), (3, 'hr'))
|
self.assertEqual(units.normalize(10800000000000, 'ns'), (3, 'hr'))
|
||||||
self.assertEqual(units.normalize(7, 'day'), (1, 'week'))
|
self.assertEqual(units.normalize(14, 'day'), (2, 'week'))
|
||||||
self.assertEqual(units.normalize(31, 'day'), (1, 'month'))
|
self.assertEqual(units.normalize(91, 'day'), (3, 'month'))
|
||||||
self.assertEqual(units.normalize(12, 'month'), (1, 'year'))
|
self.assertEqual(units.normalize(18, 'month'), (18, 'month'))
|
||||||
|
self.assertEqual(units.normalize(24, 'month'), (2, 'year'))
|
||||||
|
|
||||||
self.assertEqual(units.normalize(1, 'unknown_unit'),
|
self.assertEqual(units.normalize(1, 'unknown_unit'),
|
||||||
(1, 'unknown_unit'))
|
(1, 'unknown_unit'))
|
||||||
|
@ -124,22 +124,24 @@ def _normalize_information(value, unit):
|
|||||||
|
|
||||||
|
|
||||||
def _normalize_time(value, unit):
|
def _normalize_time(value, unit):
|
||||||
|
# Normalize time by converting to next higher unit when value is
|
||||||
|
# at least 2 units
|
||||||
value, unit = convert(value, unit, 's')
|
value, unit = convert(value, unit, 's')
|
||||||
|
|
||||||
if value >= 60:
|
if value >= 120:
|
||||||
value, unit = convert(value, 's', 'min')
|
value, unit = convert(value, 's', 'min')
|
||||||
|
|
||||||
if value >= 60:
|
if value >= 120:
|
||||||
value, unit = convert(value, 'min', 'hr')
|
value, unit = convert(value, 'min', 'hr')
|
||||||
|
|
||||||
if value >= 24:
|
if value >= 48:
|
||||||
value, unit = convert(value, 'hr', 'day')
|
value, unit = convert(value, 'hr', 'day')
|
||||||
|
|
||||||
if value >= 365:
|
if value >= 730:
|
||||||
value, unit = convert(value, 'day', 'year')
|
value, unit = convert(value, 'day', 'year')
|
||||||
elif value >= 31:
|
elif value >= 62:
|
||||||
value, unit = convert(value, 'day', 'month')
|
value, unit = convert(value, 'day', 'month')
|
||||||
elif value >= 7:
|
elif value >= 14:
|
||||||
value, unit = convert(value, 'day', 'week')
|
value, unit = convert(value, 'day', 'week')
|
||||||
|
|
||||||
return functions.format_value(value), unit
|
return functions.format_value(value), unit
|
||||||
|
Loading…
Reference in New Issue
Block a user