Browse Source

Add support of event list pagination

Add pagination support to event list api
and show event list by page in stack detail page.

Change-Id: Ie1699b267a4ca4c33669ea2fccfd170618fc2560
Story: #1343684
Task: #18885
Xinni Ge 10 months ago
parent
commit
360a7b7a86

+ 34
- 2
heat_dashboard/api/heat.py View File

@@ -206,8 +206,40 @@ def snapshot_delete(request, stack_id, snapshot_id):
206 206
 
207 207
 
208 208
 @profiler.trace
209
-def events_list(request, stack_name):
210
-    return heatclient(request).events.list(stack_name)
209
+def events_list(request, stack_name, marker=None, sort_dir='desc',
210
+                sort_key='event_time', paginate=False, filters=None):
211
+    limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
212
+    page_size = utils.get_page_size(request)
213
+
214
+    if paginate:
215
+        request_size = page_size + 1
216
+    else:
217
+        request_size = limit
218
+    kwargs = {'sort_dir': sort_dir, 'sort_key': sort_key}
219
+    if marker:
220
+        kwargs['marker'] = marker
221
+
222
+    if filters:
223
+        kwargs.update(filters)
224
+
225
+    events_iter = heatclient(request).events.list(stack_name,
226
+                                                  limit=request_size,
227
+                                                  **kwargs)
228
+
229
+    has_prev_data = False
230
+    has_more_data = False
231
+    events = list(events_iter)
232
+    if paginate:
233
+        if len(events) > page_size:
234
+            events.pop()
235
+            has_more_data = True
236
+            if marker is not None:
237
+                has_prev_data = True
238
+        elif sort_dir == 'asc' and marker is not None:
239
+            has_more_data = True
240
+        elif marker is not None:
241
+            has_prev_data = True
242
+    return events, has_more_data, has_prev_data
211 243
 
212 244
 
213 245
 @profiler.trace

+ 1
- 0
heat_dashboard/content/stacks/tables.py View File

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

+ 34
- 7
heat_dashboard/content/stacks/tabs.py View File

@@ -11,6 +11,7 @@
11 11
 # under the License.
12 12
 
13 13
 import logging
14
+from operator import attrgetter
14 15
 
15 16
 from django.utils.translation import ugettext_lazy as _
16 17
 
@@ -81,9 +82,10 @@ class ResourceOverviewTab(tabs.Tab):
81 82
             "metadata": self.tab_group.kwargs['metadata']}
82 83
 
83 84
 
84
-class StackEventsTab(tabs.Tab):
85
+class StackEventsTab(tabs.TableTab):
85 86
     name = _("Events")
86 87
     slug = "events"
88
+    table_classes = (project_tables.EventsTable, )
87 89
     template_name = "project/stacks/_detail_events.html"
88 90
     preload = False
89 91
 
@@ -95,21 +97,46 @@ class StackEventsTab(tabs.Tab):
95 97
              ("orchestration", "events:index"),),
96 98
             request)
97 99
 
98
-    def get_context_data(self, request):
100
+    def get_events_data(self):
99 101
         stack = self.tab_group.kwargs['stack']
102
+        stack_identifier = '%s/%s' % (stack.stack_name, stack.id)
103
+        prev_marker = self.request.GET.get(
104
+            project_tables.EventsTable._meta.prev_pagination_param)
105
+        if prev_marker is not None:
106
+            sort_dir = 'asc'
107
+            marker = prev_marker
108
+        else:
109
+            sort_dir = 'desc'
110
+            marker = self.request.GET.get(
111
+                project_tables.EventsTable._meta.pagination_param, None)
112
+
100 113
         try:
101
-            stack_identifier = '%s/%s' % (stack.stack_name, stack.id)
102
-            events = api.heat.events_list(self.request, stack_identifier)
114
+            events, self._more, self._prev = api.heat.events_list(
115
+                self.request,
116
+                stack_identifier,
117
+                marker=marker,
118
+                paginate=True,
119
+                sort_dir=sort_dir)
120
+            if prev_marker is not None:
121
+                events = sorted(events, key=attrgetter('event_time'),
122
+                                reverse=True)
103 123
             LOG.debug('got events %s', events)
104 124
             # The stack id is needed to generate the resource URL.
105 125
             for event in events:
106 126
                 event.stack_id = stack.id
107 127
         except Exception:
108 128
             events = []
109
-            messages.error(request, _(
129
+            self._prev = False
130
+            self._more = False
131
+            messages.error(self.request, _(
110 132
                 'Unable to get events for stack "%s".') % stack.stack_name)
111
-        return {"stack": stack,
112
-                "table": project_tables.EventsTable(request, data=events), }
133
+        return events
134
+
135
+    def has_prev_data(self, table):
136
+        return self._prev
137
+
138
+    def has_more_data(self, table):
139
+        return self._more
113 140
 
114 141
 
115 142
 class StackResourcesTab(tabs.Tab):

Loading…
Cancel
Save