From c6b0411ccd05090bf3927249ddfd0c327d109128 Mon Sep 17 00:00:00 2001 From: Luka Peschke Date: Thu, 5 Sep 2019 11:22:03 +0200 Subject: [PATCH] Use tzutils functions in gnocchi collector Work items: * Introduce a "substract_delta()" function in "cloudkitty.tzutils". It is the opposite of the "add_delta()" function. * Use tzutils functions to add/substract a timedelta in the gnocchi collector instead of directly doing the calculation. * Add test cases for the new "substract_delta()" function Change-Id: Ic54d443b18b67e96a5de4cfb46a90f7393196376 --- cloudkitty/collector/gnocchi.py | 6 ++++-- cloudkitty/tests/test_tzutils.py | 14 +++++++++----- cloudkitty/tzutils.py | 5 +++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cloudkitty/collector/gnocchi.py b/cloudkitty/collector/gnocchi.py index a2bdf2e8..2ec19c36 100644 --- a/cloudkitty/collector/gnocchi.py +++ b/cloudkitty/collector/gnocchi.py @@ -31,6 +31,7 @@ from voluptuous import Schema from cloudkitty import collector from cloudkitty import dataframe +from cloudkitty import tzutils from cloudkitty import utils as ck_utils @@ -251,8 +252,9 @@ class GnocchiCollector(collector.BaseCollector): # FIXME(peschk_l): In order not to miss any resource whose metrics may # contain measures after its destruction, we scan resources over three # collect periods. - start -= timedelta(seconds=CONF.collect.period) - end += timedelta(seconds=CONF.collect.period) + delta = timedelta(seconds=CONF.collect.period) + start = tzutils.substract_delta(start, delta) + end = tzutils.add_delta(end, delta) query_parameters = self._generate_time_filter(start, end) if project_id: diff --git a/cloudkitty/tests/test_tzutils.py b/cloudkitty/tests/test_tzutils.py index 89bc6d56..2ebc1362 100644 --- a/cloudkitty/tests/test_tzutils.py +++ b/cloudkitty/tests/test_tzutils.py @@ -60,7 +60,7 @@ class TestTZUtils(unittest.TestCase): self.assertEqual(tzutils.dt_from_iso(tester, as_utc=True).isoformat(), tester_utc) - def _test_add_delta(self, obj, tzone): + def _test_add_substract_delta(self, obj, tzone): delta = datetime.timedelta(seconds=3600) naive = obj.astimezone(tz.UTC).replace(tzinfo=None) @@ -68,16 +68,20 @@ class TestTZUtils(unittest.TestCase): tzutils.add_delta(obj, delta).astimezone(tzone), (naive + delta).replace(tzinfo=tz.UTC).astimezone(tzone), ) + self.assertEqual( + tzutils.substract_delta(obj, delta).astimezone(tzone), + (naive - delta).replace(tzinfo=tz.UTC).astimezone(tzone), + ) - def test_add_delta_summertime(self): + def test_add_substract_delta_summertime(self): tzone = tz.gettz('Europe/Paris') obj = datetime.datetime(2019, 3, 31, 1, tzinfo=tzone) - self._test_add_delta(obj, tzone) + self._test_add_substract_delta(obj, tzone) - def test_add_delta(self): + def test_add_substract_delta(self): tzone = tz.gettz('Europe/Paris') obj = datetime.datetime(2019, 1, 1, tzinfo=tzone) - self._test_add_delta(obj, tzone) + self._test_add_substract_delta(obj, tzone) def test_get_month_start_no_arg(self): naive_utc_now = timeutils.utcnow() diff --git a/cloudkitty/tzutils.py b/cloudkitty/tzutils.py index bd55e2d6..6f143e64 100644 --- a/cloudkitty/tzutils.py +++ b/cloudkitty/tzutils.py @@ -104,6 +104,11 @@ def add_delta(dt, delta): return utc_to_local(local_to_utc(dt, naive=True) + delta) +def substract_delta(dt, delta): + """Substracts a timedelta from a datetime object.""" + return utc_to_local(local_to_utc(dt, naive=True) - delta) + + def get_month_start(dt=None, naive=False): """Returns the start of the month in the local timezone.