Skip to content

Instantly share code, notes, and snippets.

@ramielrowe
Created March 3, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramielrowe/9330834 to your computer and use it in GitHub Desktop.
Save ramielrowe/9330834 to your computer and use it in GitHub Desktop.
@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