UI: Add pagination for playbook results

This will allow larger playbooks to load much faster without needing to
return and render all of a playbook's results on a single page.

Fixes: https://github.com/ansible-community/ara/issues/168
Change-Id: I5fcf8bc411bc6da946fdd5ce23325b2b11a7197a
This commit is contained in:
David Moreau Simard 2020-09-06 11:40:13 -04:00
parent 74defc2273
commit 91c190837c
2 changed files with 23 additions and 6 deletions

View File

@ -101,6 +101,9 @@
</details>
<details id="results" open="true">
<summary>Task results</summary>
{% if not static_generation %}
{% include "partials/pagination.html" with data=results %}
{% endif %}
<table class="pf-c-table pf-m-grid-md pf-m-compact" role="grid" id="result-table">
<thead>
<tr role="row">
@ -117,7 +120,7 @@
</tr>
</thead>
<tbody>
{% for result in results %}
{% for result in results.results %}
<tr role="row">
<td role="cell" data-label="Status" class="pf-c-table__icon pf-m-fit-content">
{% include "partials/result_status_icon.html" with status=result.status %}

View File

@ -65,6 +65,7 @@ class Playbook(generics.RetrieveAPIView):
queryset = models.Playbook.objects.all()
renderer_classes = [TemplateHTMLRenderer]
pagination_class = LimitOffsetPaginationWithLinks
template_name = "playbook.html"
def get(self, request, *args, **kwargs):
@ -78,15 +79,27 @@ class Playbook(generics.RetrieveAPIView):
records = serializers.ListRecordSerializer(
models.Record.objects.filter(playbook=playbook.data["id"]).all(), many=True
)
results = serializers.ListResultSerializer(
models.Result.objects.filter(playbook=playbook.data["id"]).all(), many=True
)
for result in results.data:
results = models.Result.objects.filter(playbook=playbook.data["id"])
page = self.paginate_queryset(results)
if page is not None:
serializer = serializers.ListResultSerializer(page, many=True)
else:
serializer = serializers.ListResultSerializer(results, many=True)
for result in serializer.data:
task_id = result["task"]
result["task"] = serializers.SimpleTaskSerializer(models.Task.objects.get(pk=task_id)).data
host_id = result["host"]
result["host"] = serializers.SimpleHostSerializer(models.Host.objects.get(pk=host_id)).data
paginated_results = self.get_paginated_response(serializer.data)
if self.paginator.count > (self.paginator.offset + self.paginator.limit):
max_current = self.paginator.offset + self.paginator.limit
else:
max_current = self.paginator.count
current_page_results = "%s-%s" % (self.paginator.offset + 1, max_current)
# fmt: off
return Response({
@ -94,7 +107,8 @@ class Playbook(generics.RetrieveAPIView):
"hosts": hosts.data,
"files": files.data,
"records": records.data,
"results": results.data
"results": paginated_results.data,
"current_page_results": current_page_results,
})
# fmt: on