From 913310e4f5e4339248360e85f912cb3393b4a81a Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Mon, 3 Mar 2014 14:50:30 -0500 Subject: [PATCH] Adding date range limit to event stats --- stacktach/dbapi.py | 18 ++++++++++++++---- tests/unit/test_dbapi.py | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/stacktach/dbapi.py b/stacktach/dbapi.py index 01e5242..91902fa 100644 --- a/stacktach/dbapi.py +++ b/stacktach/dbapi.py @@ -40,6 +40,7 @@ from stacktach import utils DEFAULT_LIMIT = 50 HARD_LIMIT = 1000 +HARD_WHEN_RANGE_LIMIT = 5 * 24 * 60 * 60 # 5 Days class APIException(Exception): @@ -462,13 +463,22 @@ def _rawdata_factory(service): def get_event_stats(request): try: filters = {} - if 'when_min' in request.GET: - when_min = utils.str_time_to_unix(request.GET['when_min']) - filters['when__gte'] = when_min - if 'when_max' in request.GET: + if 'when_min' in request.GET or 'when_max' in request.GET: + if not ('when_min' in request.GET and 'when_max' in request.GET): + msg = "When providing date range filters, " \ + "a min and max are required." + raise BadRequestException(message=msg) + + when_min = utils.str_time_to_unix(request.GET['when_min']) when_max = utils.str_time_to_unix(request.GET['when_max']) + + if when_max - when_min > HARD_WHEN_RANGE_LIMIT: + msg = "Date ranges may be no larger than %s seconds" + raise BadRequestException(message=msg % HARD_WHEN_RANGE_LIMIT) + filters['when__lte'] = when_max + filters['when__gte'] = when_min if 'event' in request.GET: filters['event'] = request.GET['event'] diff --git a/tests/unit/test_dbapi.py b/tests/unit/test_dbapi.py index fe8c7b3..0c40c58 100644 --- a/tests/unit/test_dbapi.py +++ b/tests/unit/test_dbapi.py @@ -1125,11 +1125,41 @@ class DBAPITestCase(StacktachBaseTestCase): {'stats': {'count': 100}}) self.mox.VerifyAll() + def test_get_verified_count_only_one_range_param_returns_400(self): + fake_request = self.mox.CreateMockAnything() + fake_request.method = 'GET' + fake_request.GET = {'when_min': "2014-020-26", + 'service': "nova"} + + self.mox.ReplayAll() + + response = dbapi.get_event_stats(fake_request) + self.assertEqual(response.status_code, 400) + self.assertEqual(json.loads(response.content)['message'], + "When providing date range filters, " + "a min and max are required.") + self.mox.VerifyAll() + + def test_get_verified_count_only_large_date_range_returns_400(self): + fake_request = self.mox.CreateMockAnything() + fake_request.method = 'GET' + fake_request.GET = {'when_min': "2014-2-26 00:00:00", + 'when_max': "2014-3-3 00:00:01", # > 5 days later + 'service': "nova"} + + self.mox.ReplayAll() + + response = dbapi.get_event_stats(fake_request) + self.assertEqual(response.status_code, 400) + self.assertEqual(json.loads(response.content)['message'], + "Date ranges may be no larger than 432000 seconds") + self.mox.VerifyAll() + def test_get_verified_count_wrong_date_format_returns_400(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' fake_request.GET = {'when_min': "2014-020-26", - + 'when_max': "2014-020-26", 'service': "nova"} self.mox.ReplayAll() @@ -1145,7 +1175,7 @@ class DBAPITestCase(StacktachBaseTestCase): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' fake_request.GET = {'when_min': "2014-02-26 00:00:00", - "when_min": "2014-02-27 00:00:00", + "when_max": "2014-02-27 00:00:00", 'service': "qonos"} self.mox.ReplayAll()