Merge "Add support of event list pagination"

This commit is contained in:
Zuul 2018-08-13 23:55:10 +00:00 committed by Gerrit Code Review
commit d3199d5bf8
3 changed files with 69 additions and 9 deletions

View File

@ -206,8 +206,40 @@ def snapshot_delete(request, stack_id, snapshot_id):
@profiler.trace @profiler.trace
def events_list(request, stack_name): def events_list(request, stack_name, marker=None, sort_dir='desc',
return heatclient(request).events.list(stack_name) sort_key='event_time', paginate=False, filters=None):
limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
page_size = utils.get_page_size(request)
if paginate:
request_size = page_size + 1
else:
request_size = limit
kwargs = {'sort_dir': sort_dir, 'sort_key': sort_key}
if marker:
kwargs['marker'] = marker
if filters:
kwargs.update(filters)
events_iter = heatclient(request).events.list(stack_name,
limit=request_size,
**kwargs)
has_prev_data = False
has_more_data = False
events = list(events_iter)
if paginate:
if len(events) > page_size:
events.pop()
has_more_data = True
if marker is not None:
has_prev_data = True
elif sort_dir == 'asc' and marker is not None:
has_more_data = True
elif marker is not None:
has_prev_data = True
return events, has_more_data, has_prev_data
@profiler.trace @profiler.trace

View File

@ -333,6 +333,7 @@ class EventsTable(tables.DataTable):
class Meta(object): class Meta(object):
name = "events" name = "events"
verbose_name = _("Stack Events") verbose_name = _("Stack Events")
pagination_param = 'event_marker'
class ResourcesUpdateRow(tables.Row): class ResourcesUpdateRow(tables.Row):

View File

@ -11,6 +11,7 @@
# under the License. # under the License.
import logging import logging
from operator import attrgetter
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -81,9 +82,10 @@ class ResourceOverviewTab(tabs.Tab):
"metadata": self.tab_group.kwargs['metadata']} "metadata": self.tab_group.kwargs['metadata']}
class StackEventsTab(tabs.Tab): class StackEventsTab(tabs.TableTab):
name = _("Events") name = _("Events")
slug = "events" slug = "events"
table_classes = (project_tables.EventsTable, )
template_name = "project/stacks/_detail_events.html" template_name = "project/stacks/_detail_events.html"
preload = False preload = False
@ -95,21 +97,46 @@ class StackEventsTab(tabs.Tab):
("orchestration", "events:index"),), ("orchestration", "events:index"),),
request) request)
def get_context_data(self, request): def get_events_data(self):
stack = self.tab_group.kwargs['stack'] stack = self.tab_group.kwargs['stack']
stack_identifier = '%s/%s' % (stack.stack_name, stack.id)
prev_marker = self.request.GET.get(
project_tables.EventsTable._meta.prev_pagination_param)
if prev_marker is not None:
sort_dir = 'asc'
marker = prev_marker
else:
sort_dir = 'desc'
marker = self.request.GET.get(
project_tables.EventsTable._meta.pagination_param, None)
try: try:
stack_identifier = '%s/%s' % (stack.stack_name, stack.id) events, self._more, self._prev = api.heat.events_list(
events = api.heat.events_list(self.request, stack_identifier) self.request,
stack_identifier,
marker=marker,
paginate=True,
sort_dir=sort_dir)
if prev_marker is not None:
events = sorted(events, key=attrgetter('event_time'),
reverse=True)
LOG.debug('got events %s', events) LOG.debug('got events %s', events)
# The stack id is needed to generate the resource URL. # The stack id is needed to generate the resource URL.
for event in events: for event in events:
event.stack_id = stack.id event.stack_id = stack.id
except Exception: except Exception:
events = [] events = []
messages.error(request, _( self._prev = False
self._more = False
messages.error(self.request, _(
'Unable to get events for stack "%s".') % stack.stack_name) 'Unable to get events for stack "%s".') % stack.stack_name)
return {"stack": stack, return events
"table": project_tables.EventsTable(request, data=events), }
def has_prev_data(self, table):
return self._prev
def has_more_data(self, table):
return self._more
class StackResourcesTab(tabs.Tab): class StackResourcesTab(tabs.Tab):