From 053bb4eeb0bc459af6c2f29b334c73376d44c1f3 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 25 Jun 2014 14:26:35 -0400 Subject: [PATCH] Add ordering and various filtering for job events as requested by AC-1349 --- awx/api/utils/decorators.py | 10 +++++++++- awx/api/views.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/awx/api/utils/decorators.py b/awx/api/utils/decorators.py index 79162d4b3e..612d562ee5 100644 --- a/awx/api/utils/decorators.py +++ b/awx/api/utils/decorators.py @@ -29,14 +29,21 @@ def paginated(method): limit = min(api_settings.MAX_PAGINATE_BY, limit) limit = int(limit) + # Get the order parameter if it's given + if request.QUERY_PARAMS.get("ordering", False): + ordering = request.QUERY_PARAMS[ordering] + else: + ordering = None + # What page are we on? page = int(request.QUERY_PARAMS.get('page', 1)) offset = (page - 1) * limit - # Add the limit, offset, and page variables to the keyword arguments + # Add the limit, offset, page, and order variables to the keyword arguments # being sent to the underlying method. kwargs['limit'] = limit kwargs['offset'] = offset + kwargs['ordering'] = ordering # Okay, call the underlying method. results, count = method(self, request, *args, **kwargs) @@ -64,3 +71,4 @@ def paginated(method): # Okay, we're done; return response data. return Response(answer) return func + diff --git a/awx/api/views.py b/awx/api/views.py index 36b6d213c1..ae02278861 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1486,12 +1486,26 @@ class JobJobPlaysList(BaseJobEventsList): new_in_150 = True @paginated - def get(self, request, limit, offset, *args, **kwargs): + def get(self, request, limit, offset, ordering, *args, **kwargs): all_plays = [] job = get_object_or_404(self.parent_model, pk=self.kwargs['pk']) # Put together a queryset for relevant job events. qs = job.job_events.filter(event='playbook_on_play_start') + if ordering is not None: + qs = qs.order_by(ordering) + + # This is a bit of a special case for id filtering requested by the UI + # doing this here for the moment until/unless we need to implement more + # complex filtering (since we aren't under a serializer) + + if "id__in" in request.QUERY_PARAMS: + qs = qs.filter(id__in=[int(filter_id) for filter_id in request.QUERY_PARAMS["id__in"].split(",")]) + elif "id__gt" in request.QUERY_PARAMS: + qs = qs.filter(id__gt=request.QUERY_PARAMS['id__gt']) + if "failed" in request.QUERY_PARAMS: + qs = qs.filter(failed=(request.QUERY_PARAMS['failed'].lower() == 'true')) + count = qs.count() # Iterate over the relevant play events and get the details. @@ -1540,7 +1554,7 @@ class JobJobTasksList(BaseJobEventsList): new_in_150 = True @paginated - def get(self, request, limit, offset, *args, **kwargs): + def get(self, request, limit, offset, ordering, *args, **kwargs): """Return aggregate data about each of the job tasks that is: - an immediate child of the job event - corresponding to the spinning up of a new task or playbook @@ -1570,6 +1584,18 @@ class JobJobTasksList(BaseJobEventsList): .values('parent__id', 'event', 'changed') .annotate(num=Count('event')) .order_by('parent__id')) + + # This is a bit of a special case for id filtering requested by the UI + # doing this here for the moment until/unless we need to implement more + # complex filtering (since we aren't under a serializer) + + if "id__in" in request.QUERY_PARAMS: + qs = qs.filter(id__in=[int(filter_id) for filter_id in request.QUERY_PARAMS["id__in"].split(",")]) + elif "id__gt" in request.QUERY_PARAMS: + qs = qs.filter(id__gt=request.QUERY_PARAMS['id__gt']) + if "failed" in request.QUERY_PARAMS: + qs = qs.filter(failed=(request.QUERY_PARAMS['failed'].lower() == 'true')) + count = queryset.count() # The data above will come back in a list, but we are going to