diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index aba336bf..1eb17b3b 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -324,12 +324,8 @@ class Statistics(Base): if (self.duration_start and self.duration_end and self.duration_start <= self.duration_end): - # Can't use timedelta.total_seconds() because - # it is not available in Python 2.6. - diff = self.duration_end - self.duration_start - self.duration = (diff.seconds + (diff.days * 24 * 60 ** 2)) / 60 - # FIXME(dhellmann): Shouldn't this value be returned in - # seconds, or something even smaller? + self.duration = timeutils.delta_seconds(self.duration_start, + self.duration_end) else: self.duration_start = self.duration_end = self.duration = None diff --git a/ceilometer/api/v1/blueprint.py b/ceilometer/api/v1/blueprint.py index 2d424b4e..a4c20d28 100644 --- a/ceilometer/api/v1/blueprint.py +++ b/ceilometer/api/v1/blueprint.py @@ -570,10 +570,7 @@ def compute_duration_by_resource(resource, meter): # sentinal indicating that there is something "funny" # about the range. if min_ts and max_ts and (min_ts <= max_ts): - # Can't use timedelta.total_seconds() because - # it is not available in Python 2.6. - diff = max_ts - min_ts - duration = (diff.seconds + (diff.days * 24 * 60 ** 2)) / 60 + duration = timeutils.delta_seconds(min_ts, max_ts) else: min_ts = max_ts = duration = None diff --git a/tests/api/v1/test_compute_duration_by_resource.py b/tests/api/v1/test_compute_duration_by_resource.py index 020bb338..3dd1826c 100644 --- a/tests/api/v1/test_compute_duration_by_resource.py +++ b/tests/api/v1/test_compute_duration_by_resource.py @@ -80,14 +80,14 @@ class TestComputeDurationByResource(tests_api.TestBase): data = self._invoke_api() self._assert_times_match(data['start_timestamp'], self.start) self._assert_times_match(data['end_timestamp'], self.middle1) - assert data['duration'] == 8 * 60 + self.assertEqual(data['duration'], 8 * 60 * 60) def test_within_range(self): self._set_interval(self.middle1, self.middle2) data = self._invoke_api() self._assert_times_match(data['start_timestamp'], self.middle1) self._assert_times_match(data['end_timestamp'], self.middle2) - assert data['duration'] == 10 * 60 + self.assertEqual(data['duration'], 10 * 60 * 60) def test_within_range_zero_duration(self): self._set_interval(self.middle1, self.middle1) @@ -101,7 +101,7 @@ class TestComputeDurationByResource(tests_api.TestBase): data = self._invoke_api() self._assert_times_match(data['start_timestamp'], self.middle2) self._assert_times_match(data['end_timestamp'], self.end) - assert data['duration'] == (6 * 60) - 1 + self.assertEqual(data['duration'], ((6 * 60) - 1) * 60) def test_after_range(self): self._set_interval(self.late1, self.late2) diff --git a/tests/api/v2/test_compute_duration_by_resource.py b/tests/api/v2/test_compute_duration_by_resource.py index d8125779..f9b8eb25 100644 --- a/tests/api/v2/test_compute_duration_by_resource.py +++ b/tests/api/v2/test_compute_duration_by_resource.py @@ -99,14 +99,14 @@ class TestComputeDurationByResource(FunctionalTest): data = self._invoke_api() self._assert_times_match(data['duration_start'], self.start) self._assert_times_match(data['duration_end'], self.middle1) - assert data['duration'] == 8 * 60 + self.assertEqual(data['duration'], 8 * 60 * 60) def test_within_range(self): self._set_interval(self.middle1, self.middle2) data = self._invoke_api() self._assert_times_match(data['duration_start'], self.middle1) self._assert_times_match(data['duration_end'], self.middle2) - assert data['duration'] == 10 * 60 + self.assertEqual(data['duration'], 10 * 60 * 60) def test_within_range_zero_duration(self): self._set_interval(self.middle1, self.middle1) @@ -120,7 +120,7 @@ class TestComputeDurationByResource(FunctionalTest): data = self._invoke_api() self._assert_times_match(data['duration_start'], self.middle2) self._assert_times_match(data['duration_end'], self.end) - assert data['duration'] == (6 * 60) - 1 + self.assertEqual(data['duration'], ((6 * 60) - 1) * 60) def test_after_range(self): self._set_interval(self.late1, self.late2) diff --git a/tests/api/v2/test_statistics.py b/tests/api/v2/test_statistics.py index 2be17091..9ac8ec43 100644 --- a/tests/api/v2/test_statistics.py +++ b/tests/api/v2/test_statistics.py @@ -66,7 +66,7 @@ class TestStatisticsDuration(unittest.TestCase): ) assert s.duration_start == self.start assert s.duration_end == self.middle1 - assert s.duration == 8 * 60 + self.assertEqual(s.duration, 8 * 60 * 60) def test_within_range(self): s = v2.Statistics(duration_start=self.middle1, @@ -76,7 +76,7 @@ class TestStatisticsDuration(unittest.TestCase): ) assert s.duration_start == self.middle1 assert s.duration_end == self.middle2 - assert s.duration == 10 * 60 + self.assertEqual(s.duration, 10 * 60 * 60) def test_within_range_zero_duration(self): s = v2.Statistics(duration_start=self.middle1, @@ -96,7 +96,7 @@ class TestStatisticsDuration(unittest.TestCase): ) assert s.duration_start == self.middle2 assert s.duration_end == self.end - assert s.duration == (6 * 60) - 1 + self.assertEqual(s.duration, ((6 * 60) - 1) * 60) def test_after_range(self): s = v2.Statistics(duration_start=self.late1,