Browse Source

HUE-6143 [editor] Add pagination to the query history

Paginate in bottom of page.
Page size is 50.
Can only go 1 page back or forth if possible.
Re-init to page 1 when executing the query (and not when open a query history).
Romain Rigaux 8 years ago
parent
commit
76cc838

+ 9 - 2
desktop/libs/notebook/src/notebook/api.py

@@ -25,6 +25,7 @@ from django.db.models import Q
 from django.utils.translation import ugettext as _
 from django.views.decorators.http import require_GET, require_POST
 
+from desktop.api2 import __paginate
 from desktop.lib.i18n import smart_str
 from desktop.lib.django_util import JsonResponse
 from desktop.models import Document2, Document
@@ -426,7 +427,8 @@ def get_history(request):
 
   doc_type = request.GET.get('doc_type')
   doc_text = request.GET.get('doc_text')
-  limit = min(request.GET.get('len', 50), 100)
+  page = min(int(request.GET.get('page', 1)), 100)
+  limit = min(int(request.GET.get('limit', 50)), 100)
   is_notification_manager = request.GET.get('is_notification_manager', 'false') == 'true'
 
   if is_notification_manager:
@@ -437,8 +439,13 @@ def get_history(request):
   if doc_text:
     docs = docs.filter(Q(name__icontains=doc_text) | Q(description__icontains=doc_text) | Q(search__icontains=doc_text))
 
+  # Paginate
+  docs = docs.order_by('-last_modified')
+  response['count'] = docs.count()
+  docs = __paginate(page, limit, queryset=docs)['documents']
+
   history = []
-  for doc in docs.order_by('-last_modified')[:limit]:
+  for doc in docs:
     notebook = Notebook(document=doc).get_data()
     if 'snippets' in notebook:
       statement = notebook['description'] if is_notification_manager else _get_statement(notebook)

+ 26 - 2
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1061,6 +1061,7 @@ var EditorViewModel = (function() {
           column: 0
         }
       });
+      notebook.historyCurrentPage(1);
 
       // TODO: rename startLongOperationTimeout to startBlockingOperationTimeout
       // TODO: stop blocking operation UI if there is one
@@ -1757,6 +1758,11 @@ var EditorViewModel = (function() {
       self.fetchHistory();
     });
     self.loadingHistory = ko.observable(self.history().length == 0);
+    self.historyCurrentPage = ko.observable(vm.selectedNotebook() ? vm.selectedNotebook().historyCurrentPage() : 1);
+    self.historyCurrentPage.subscribe(function(val) {
+      self.fetchHistory();
+    });
+    self.historyTotalPages = ko.observable(vm.selectedNotebook() ? vm.selectedNotebook().historyTotalPages() : 1);
 
     self.schedulerViewModel = null;
     self.schedulerViewModelIsLoaded = ko.observable(false);
@@ -1773,6 +1779,7 @@ var EditorViewModel = (function() {
 
     self.avoidClosing = false;
 
+
     self.getSession = function (session_type) {
       var _s = null;
       $.each(self.sessions(), function (index, s) {
@@ -2106,10 +2113,13 @@ var EditorViewModel = (function() {
     };
 
     self.fetchHistory = function (callback) {
+      var QUERIES_PER_PAGE = 50;
       self.loadingHistory(true);
+
       $.get("/notebook/api/get_history", {
         doc_type: self.selectedSnippet(),
-        limit: 50,
+        limit: QUERIES_PER_PAGE,
+        page: self.historyCurrentPage(),
         doc_text: self.historyFilter(),
         is_notification_manager: vm.isNotificationManager()
       }, function(data) {
@@ -2129,6 +2139,7 @@ var EditorViewModel = (function() {
           });
         }
         self.history(parsedHistory);
+        self.historyTotalPages(Math.ceil(data.count / QUERIES_PER_PAGE));
       }).always(function(){
         self.loadingHistory(false);
         if (callback) {
@@ -2137,6 +2148,19 @@ var EditorViewModel = (function() {
       });
     };
 
+    self.prevHistoryPage = function () {
+      if (self.historyCurrentPage() !== 1) {
+        self.historyCurrentPage(self.historyCurrentPage() - 1);
+      }
+    };
+
+    self.nextHistoryPage = function () {
+      if (self.historyCurrentPage() < self.historyTotalPages()) {
+        self.historyCurrentPage(self.historyCurrentPage() + 1);
+      }
+    };
+
+
     self.updateHistoryFailed = false;
     self.updateHistory = function (statuses, interval) {
       var items = $.grep(self.history(), function (item) {
@@ -2154,7 +2178,7 @@ var EditorViewModel = (function() {
           }).fail(function (xhr) {
             items = [];
             self.updateHistoryFailed = true;
-            console.warn('Lost connectivity to the Hue backend.');
+            console.warn('Lost connectivity to the Hue history refresh backend.');
           }).always(function () {
             if (items.length > 0) {
               window.setTimeout(function () {

+ 9 - 0
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -796,6 +796,15 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
               </tbody>
             </table>
             <!-- /ko -->
+            <!-- ko with: $parent -->
+            <div class="pagination" data-bind="visible: historyTotalPages() > 1">
+            <ul>
+              <li data-bind="css: { 'disabled' : historyCurrentPage() === 1 }"><a href="javascript: void(0);" data-bind="click: function() { prevHistoryPage(); }">${ _("Prev") }</a></li>
+              <li class="active"><span data-bind="text: historyCurrentPage() + '/' + historyTotalPages()"></span></li>
+              <li data-bind="css: { 'disabled' : historyCurrentPage() === historyTotalPages() }"><a href="javascript: void(0);" data-bind="click: function() { nextHistoryPage(); }">${ _("Next") }</a></li>
+            </ul>
+            </div>
+            <!-- /ko -->
           <!-- /ko -->
         </div>