Adding date range limit to event stats

This commit is contained in:
Andrew Melton 2014-03-03 14:50:30 -05:00
parent 475ef4777d
commit 913310e4f5
2 changed files with 46 additions and 6 deletions
stacktach
tests/unit

@ -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']

@ -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()