Browse Source

HUE-9351 [editor] Insert history records after execute instead of refetching all

Johan Ahlen 5 years ago
parent
commit
2a43dc2bb9

+ 21 - 6
desktop/core/src/desktop/js/apps/notebook2/components/ko.queryHistory.js

@@ -29,7 +29,7 @@ import { SHOW_EVENT } from 'ko/components/ko.importDocumentsModal';
 
 export const NAME = 'query-history';
 export const HISTORY_CLEARED_EVENT = 'query.history.cleared';
-export const UPDATE_HISTORY_EVENT = 'query.history.update';
+export const ADD_TO_HISTORY_EVENT = 'query.history.add';
 
 import { NAME as PAGINATOR_COMPONENT } from './ko.paginator';
 
@@ -179,7 +179,10 @@ class QueryHistory extends DisposableComponent {
     this.historyFilter = ko.observable('').extend({ rateLimit: 900 });
 
     this.historyCurrentPage = ko.observable(1);
-    this.historyTotalPages = ko.observable(1);
+    this.totalHistoryCount = ko.observable(0);
+    this.historyTotalPages = ko.pureComputed(() =>
+      Math.max(1, Math.ceil(this.totalHistoryCount() / QUERIES_PER_PAGE))
+    );
 
     this.refreshStatusFailed = false;
 
@@ -203,7 +206,7 @@ class QueryHistory extends DisposableComponent {
 
     this.onPageChange = throttledFetch;
 
-    this.subscribe(UPDATE_HISTORY_EVENT, throttledFetch);
+    this.subscribe(ADD_TO_HISTORY_EVENT, this.addHistoryRecord.bind(this));
 
     throttledFetch();
   }
@@ -218,7 +221,7 @@ class QueryHistory extends DisposableComponent {
       })
       .then(() => {
         this.history.removeAll();
-        this.historyTotalPages(1);
+        this.totalHistoryCount(0);
         this.historyFilter('');
         huePubSub.publish(HISTORY_CLEARED_EVENT);
       })
@@ -244,6 +247,18 @@ class QueryHistory extends DisposableComponent {
     }
   }
 
+  addHistoryRecord(historyRecord) {
+    this.history.unshift({
+      url: historyRecord.url,
+      query: trimEllipsis(historyRecord.statement),
+      lastExecuted: historyRecord.lastExecuted,
+      status: ko.observable(historyRecord.status),
+      name: historyRecord.name,
+      uuid: historyRecord.uuid
+    });
+    this.totalHistoryCount(this.totalHistoryCount() + 1);
+  }
+
   async fetchHistory() {
     this.loadingHistory(true);
 
@@ -270,10 +285,10 @@ class QueryHistory extends DisposableComponent {
         this.history([]);
       }
 
-      this.historyTotalPages(Math.ceil(historyData.count / QUERIES_PER_PAGE));
+      this.totalHistoryCount(historyData.count);
     } catch (err) {
       this.history([]);
-      this.historyTotalPages(1);
+      this.totalHistoryCount(0);
     }
 
     this.loadingHistory(false);

+ 14 - 5
desktop/core/src/desktop/js/apps/notebook2/snippet.js

@@ -45,7 +45,7 @@ import {
   REFRESH_STATEMENT_LOCATIONS_EVENT
 } from 'ko/bindings/ace/aceLocationHandler';
 import { EXECUTE_ACTIVE_EXECUTABLE_EVENT } from 'apps/notebook2/components/ko.executableActions';
-import { UPDATE_HISTORY_EVENT } from 'apps/notebook2/components/ko.queryHistory';
+import { ADD_TO_HISTORY_EVENT } from 'apps/notebook2/components/ko.queryHistory';
 import { findEditorConnector, getLastKnownConfig } from 'utils/hueConfig';
 import { cancelActiveRequest } from 'api/apiUtils';
 import { getOptimizer } from 'catalog/optimizer/optimizer';
@@ -911,11 +911,20 @@ export default class Snippet {
     huePubSub.subscribe(EXECUTABLE_STATUS_TRANSITION_EVENT, transitionDetails => {
       if (this.activeExecutable() === transitionDetails.executable) {
         if (
-          transitionDetails.newStatus === EXECUTION_STATUS.available ||
-          transitionDetails.newStatus === EXECUTION_STATUS.failed ||
-          transitionDetails.newStatus === EXECUTION_STATUS.success
+          (transitionDetails.newStatus === EXECUTION_STATUS.available ||
+            transitionDetails.newStatus === EXECUTION_STATUS.failed ||
+            transitionDetails.newStatus === EXECUTION_STATUS.success) &&
+          this.activeExecutable().history &&
+          this.activeExecutable().handle
         ) {
-          huePubSub.publish(UPDATE_HISTORY_EVENT);
+          huePubSub.publish(ADD_TO_HISTORY_EVENT, {
+            absoluteUrl: undefined,
+            statement: this.activeExecutable().handle.statement,
+            lastExecuted: this.activeExecutable().executeStarted,
+            status: this.activeExecutable().status,
+            name: this.parentNotebook.name(),
+            uuid: this.activeExecutable().history.uuid
+          });
         }
       }
     });