Created
March 3, 2014 18:03
-
-
Save ramielrowe/9330834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api_call | |
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: | |
when_max = utils.str_time_to_unix(request.GET['when_max']) | |
filters['when__lte'] = when_max | |
service = request.GET.get("service", "nova") | |
rawdata = _rawdata_factory(service) | |
if filters: | |
rawdata = rawdata.filter(**filters) | |
events = rawdata.values('event').annotate(event_count=Count('event')) | |
events = list(events) | |
if 'event' in request.GET: | |
event = request.GET['event'] | |
default = {'event': event, 'event_count': 0} | |
events = [x for x in events if x['event'] == event] or [default, ] | |
return {'stats': events} | |
except (KeyError, TypeError): | |
raise BadRequestException(message="Invalid/absent query parameter") | |
except (ValueError, AttributeError): | |
raise BadRequestException(message="Invalid format for date (Correct " | |
"format should be %YYYY-%mm-%dd)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment