From 913310e4f5e4339248360e85f912cb3393b4a81a Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Mon, 3 Mar 2014 14:50:30 -0500 Subject: [PATCH 1/2] 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() From 93d13262dbab444b2608bec9a6af87e76ba97ea1 Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Mon, 3 Mar 2014 15:37:34 -0500 Subject: [PATCH 2/2] Increasing event stats max range --- stacktach/dbapi.py | 2 +- tests/unit/test_dbapi.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stacktach/dbapi.py b/stacktach/dbapi.py index 91902fa..a263425 100644 --- a/stacktach/dbapi.py +++ b/stacktach/dbapi.py @@ -40,7 +40,7 @@ from stacktach import utils DEFAULT_LIMIT = 50 HARD_LIMIT = 1000 -HARD_WHEN_RANGE_LIMIT = 5 * 24 * 60 * 60 # 5 Days +HARD_WHEN_RANGE_LIMIT = 7 * 24 * 60 * 60 # 5 Days class APIException(Exception): diff --git a/tests/unit/test_dbapi.py b/tests/unit/test_dbapi.py index 0c40c58..3635458 100644 --- a/tests/unit/test_dbapi.py +++ b/tests/unit/test_dbapi.py @@ -1144,7 +1144,7 @@ class DBAPITestCase(StacktachBaseTestCase): 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 + 'when_max': "2014-3-5 00:00:01", # > 7 days later 'service': "nova"} self.mox.ReplayAll() @@ -1152,7 +1152,7 @@ class DBAPITestCase(StacktachBaseTestCase): 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") + "Date ranges may be no larger than 604800 seconds") self.mox.VerifyAll() def test_get_verified_count_wrong_date_format_returns_400(self):