فهرست منبع

HUE-9117 [editor] Persist default limit when saving snippets in editor v2

Johan Ahlen 5 سال پیش
والد
کامیت
242d47e6df

+ 3 - 3
desktop/core/src/desktop/js/apps/notebook2/components/ko.executableActions.js

@@ -50,7 +50,7 @@ const TEMPLATE = `
       <!-- /ko -->
     <!-- /ko -->
   </div>
-  <form autocomplete="off" class="inline-block">
+  <form autocomplete="off" class="inline-block margin-left-10">
     <input class="input-small limit-input" type="text" ${ window.PREVENT_AUTOFILL_INPUT_ATTRS } placeholder="${ I18n('Limit') }" data-bind="textInput: limit">
   </form>
 </div>
@@ -71,7 +71,7 @@ class ExecutableActions extends DisposableComponent {
 
     this.subscribe(this.limit, newVal => {
       if (this.activeExecutable()) {
-        this.activeExecutable().executor.limit = newVal;
+        this.activeExecutable().executor.defaultLimit(newVal);
       }
     });
 
@@ -113,7 +113,7 @@ class ExecutableActions extends DisposableComponent {
   updateFromExecutable(executable) {
     this.status(executable.status);
     this.partOfRunningExecution(executable.isPartOfRunningExecution());
-    this.limit(executable.executor.limit);
+    this.limit(executable.executor.defaultLimit());
   }
 
   async stop() {

+ 6 - 2
desktop/core/src/desktop/js/apps/notebook2/components/ko.executableActions.test.js

@@ -16,7 +16,9 @@ describe('ko.executableActions.js', () => {
       isReady: () => true,
       reset: () => {},
       nextExecutable: {},
-      executor: {},
+      executor: {
+        defaultLimit: () => {}
+      },
       status: EXECUTION_STATUS.ready
     };
     const activeExecutable = () => mockExecutable;
@@ -45,7 +47,9 @@ describe('ko.executableActions.js', () => {
       isReady: () => true,
       reset: () => {},
       nextExecutable: {},
-      executor: {},
+      executor: {
+        defaultLimit: () => {}
+      },
       status: EXECUTION_STATUS.ready
     };
     const activeExecutable = () => mockExecutable;

+ 2 - 2
desktop/core/src/desktop/js/apps/notebook2/execution/executor.js

@@ -29,6 +29,7 @@ class Executor {
    * @param {ContextNamespace} options.namespace
    * @param {string} options.statement
    * @param {string} [options.database]
+   * @param {function} [options.defaultLimit]
    * @param {boolean} [options.isOptimizerEnabled] - Default false
    * @param {Snippet} [options.snippet] - Optional snippet for history
    */
@@ -40,8 +41,7 @@ class Executor {
     this.isSqlEngine = options.isSqlEngine;
     this.isOptimizerEnabled = options.isOptimizerEnabled;
     this.executables = [];
-
-    this.limit = 10;
+    this.defaultLimit = options.defaultLimit || (() => {});
 
     this.snippet = options.snippet;
   }

+ 4 - 2
desktop/core/src/desktop/js/apps/notebook2/execution/sqlExecutable.js

@@ -39,13 +39,15 @@ export default class SqlExecutable extends Executable {
     let statement = this.statement || this.parsedStatement.statement;
     if (
       this.parsedStatement &&
+      this.parsedStatement.firstToken &&
       this.parsedStatement.firstToken.toLowerCase() === 'select' &&
-      this.executor.limit &&
+      !isNaN(this.executor.defaultLimit()) &&
+      this.executor.defaultLimit() > 0 &&
       !/\slimit\s[0-9]/i.test(statement)
     ) {
       const endMatch = statement.match(SELECT_END_REGEX);
       if (endMatch) {
-        statement = endMatch[1] + ' LIMIT ' + this.executor.limit;
+        statement = endMatch[1] + ' LIMIT ' + this.executor.defaultLimit();
         if (endMatch[2]) {
           statement += endMatch[2];
         }

+ 2 - 2
desktop/core/src/desktop/js/apps/notebook2/execution/sqlExecutable.test.js

@@ -37,7 +37,7 @@ describe('sqlExecutable.js', () => {
         parsedStatement: { statement: statement, firstToken: 'select' },
         sourceType: 'impala',
         executor: {
-          limit: limit
+          defaultLimit: () => limit
         }
       });
     }
@@ -49,7 +49,7 @@ describe('sqlExecutable.js', () => {
       parsedStatement: statement,
       sourceType: 'impala',
       executor: {
-        limit: limit
+        defaultLimit: () => limit
       }
     });
   };

+ 15 - 3
desktop/core/src/desktop/js/apps/notebook2/snippet.js

@@ -195,16 +195,23 @@ export default class Snippet {
     this.dialect = ko.pureComputed(() => this.connector() && this.connector().dialect);
 
     this.isSqlDialect = ko.pureComputed(() => this.connector() && this.connector().is_sql);
+    this.defaultLimit = ko.observable(snippetRaw.defaultLimit);
 
-    this.connector.subscribe(newValue => {
-      sessionManager.getSession({ type: newValue.type }).then(() => {
+    this.connector.subscribe(connector => {
+      sessionManager.getSession({ type: connector.type }).then(() => {
         this.status(STATUS.ready);
       });
     });
 
+    this.editorConfig = undefined;
+
     huePubSub.publish(GET_KNOWN_CONFIG_EVENT, config => {
       if (config && config.app_config && config.app_config.editor) {
-        const connectors = config.app_config.editor.interpreters;
+        this.editorConfig = config.app_config.editor;
+        if (!this.defaultLimit() && this.editorConfig.default_limit) {
+          this.defaultLimit(this.editorConfig.default_limit);
+        }
+        const connectors = this.editorConfig.interpreters;
         if (snippetRaw.connector) {
           this.connector(
             connectors.find(connector => connector.type === snippetRaw.connector.type) ||
@@ -1004,6 +1011,7 @@ export default class Snippet {
       database: this.database,
       sourceType: this.dialect,
       namespace: this.namespace,
+      defaultLimit: this.defaultLimit,
       isOptimizerEnabled: this.parentVm.isOptimizerEnabled(),
       snippet: this,
       isSqlEngine: this.isSqlDialect
@@ -1464,6 +1472,10 @@ export default class Snippet {
       connector: this.connector(),
       currentQueryTab: this.currentQueryTab(),
       database: this.database(),
+      defaultLimit:
+        !this.editorConfig || this.defaultLimit() !== this.editorConfig.default_limit
+          ? this.defaultLimit()
+          : undefined,
       id: this.id(),
       is_redacted: this.is_redacted(),
       lastAceSelectionRowOffset: this.lastAceSelectionRowOffset(),