Просмотр исходного кода

HUE-3215 [editor] Add live moment for query history and hook up update status check

Enrico Berti 9 лет назад
Родитель
Сommit
3828ef31c7

+ 26 - 0
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -3926,4 +3926,30 @@
     }
   };
 
+
+  ko.bindingHandlers.momentFromNow = {
+    update: function (element, valueAccessor) {
+      var options = ko.unwrap(valueAccessor());
+      var $element = $(element);
+
+      var value = typeof options.data === 'function' ? options.data() : options.data;
+
+      function render() {
+        $element.text(moment(value).fromNow());
+        if (options.titleFormat) {
+          $element.attr('title', moment(value).format(options.titleFormat));
+        }
+      }
+
+      render();
+
+      if (options.interval) {
+        window.clearInterval($element.data('momentInterval'));
+        $element.data('momentInterval', window.setInterval(render, options.interval));
+      }
+    }
+  };
+
+
+
 }));

+ 23 - 6
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -1342,12 +1342,14 @@
       });
     };
 
+    self.updateHistoryRunning = false;
     self.updateHistory = function () {
-      // TODO fetch list
+      if (!self.updateHistoryRunning) {
+        var items = $.grep(self.history(), function (item) {
+          return item.status() == 'running' || item.status() == 'starting';
+        });
 
-      // Update statuses
-      $.each(
-        $.grep(self.history(), function(item) { return item.status() == 'available' || item.status() == 'running' || item.status() == 'starting'; }), function(index, item) {
+        function updateHistoryCall(item) {
           $.post("/notebook/api/check_status", {
             notebook: ko.mapping.toJSON({id: item.uuid()}),
           }, function (data) {
@@ -1355,8 +1357,20 @@
             if (item.status() != status) {
               item.status(status);
             }
-        });
-      });
+          }).always(function () {
+            if (items.length > 0) {
+              window.setTimeout(function () {
+                updateHistoryCall(items.pop());
+              }, 1000);
+            }
+            self.updateHistoryRunning = (items.length > 0);
+          });
+        }
+
+        if (items.length > 0) {
+          updateHistoryCall(items.pop());
+        }
+      }
     };
 
     self._makeHistoryRecord = function(url, statement, lastExecuted, status, name, uuid) {
@@ -1452,6 +1466,8 @@
       });
       if (vm.editorMode && self.history().length == 0) {
         self.fetchHistory();
+        window.clearInterval(vm.updateHistoryInterval);
+        vm.updateHistoryInterval = window.setInterval(self.updateHistory, 20000);
       }
     }
   };
@@ -1554,6 +1570,7 @@
     self.availableSnippets = ko.mapping.fromJS(options.languages);
 
     self.editorMode = options.mode == 'editor';
+    self.updateHistoryInterval = null;
 
     self.getSnippetViewSettings = function (snippetType) {
       if (options.snippetViewSettings[snippetType]) {

+ 1 - 1
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1064,7 +1064,7 @@ ${ hueIcons.symbols() }
               <tbody data-bind="foreach: $parent.history">
                 <tr class="pointer" data-bind="click: function() { if (getSelection().toString().length == 0) { $root.openNotebook(uuid()) } }">
                   <td style="width: 100px" class="muted" data-bind="style: {'border-top-width': $index() == 0 ? '0' : ''}">
-                    <span data-bind="text: moment(lastExecuted()).fromNow(), attr: {title: moment(lastExecuted()).format('LLL')}"></span>
+                    <span data-bind="momentFromNow: {data: lastExecuted, interval: 10000, titleFormat: 'LLL'}"></span>
                   </td>
                   <td style="width: 25px" class="muted" data-bind="style: {'border-top-width': $index() == 0 ? '0' : ''}">
                     <!-- ko switch: status -->