Эх сурвалжийг харах

HUE-9000 [editor] Send the executable to the execute endpoint for proper handle placement

The idea is to send an executable instead of a snippet to execute, optionally with a notebook when a history entry is desired.
Johan Ahlen 6 жил өмнө
parent
commit
c0e096f9e4

+ 4 - 0
desktop/core/src/desktop/js/api/apiHelper.js

@@ -2063,6 +2063,8 @@ class ApiHelper {
     const promise = new Promise(async (resolve, reject) => {
       let data = {};
       if (executable.executor.snippet) {
+        // TODO: Refactor away the snippet, it currently works because snippet.statement is a computed from
+        // the active executable, but we n
         data = {
           notebook: await executable.executor.snippet.parentNotebook.toJson(),
           snippet: executable.executor.snippet.toContextJson()
@@ -2071,6 +2073,8 @@ class ApiHelper {
         data = await executable.toContext();
       }
 
+      data.executable = executable.toJson();
+
       this.simplePost(url, data, options)
         .done(response => {
           const executeResponse = {};

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

@@ -52,8 +52,8 @@ export default class Executable {
    * @param {Session[]} [options.sessions]
    */
   constructor(options) {
+    this.id = UUID();
     this.executor = options.executor;
-
     this.handle = {
       statement_id: 0 // TODO: Get rid of need for initial handle in the backend
     };
@@ -334,6 +334,7 @@ export default class Executable {
       executeStarted: this.executeStarted,
       handle: this.handle,
       history: this.history,
+      id: this.id,
       logs: this.logs.toJs(),
       lost: this.lost,
       observerState: state,

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

@@ -54,14 +54,15 @@ export default class SqlExecutable extends Executable {
 
   static fromJs(executor, executableRaw) {
     const executable = new SqlExecutable({
-      executor: executor,
       database: executableRaw.database,
+      executor: executor,
       parsedStatement: executableRaw.parsedStatement
     });
     executable.executeEnded = executableRaw.executeEnded;
     executable.executeStarted = executableRaw.executeStarted;
     executable.handle = executableRaw.handle;
     executable.history = executableRaw.history;
+    executable.id = executableRaw.id;
     executable.logs.errors = executableRaw.logs.errors;
     executable.logs.jobs = executableRaw.logs.jobs;
     executable.lost = executableRaw.lost;
@@ -74,9 +75,19 @@ export default class SqlExecutable extends Executable {
 
   toJs() {
     const executableJs = super.toJs();
-    executableJs.type = 'sqlExecutable';
     executableJs.database = this.database;
     executableJs.parsedStatement = this.parsedStatement;
+    executableJs.type = 'sqlExecutable';
     return executableJs;
   }
+
+  // TODO: Use this for execute instead of snippet
+  toJson() {
+    return JSON.stringify({
+      id: this.id,
+      statement: this.parsedStatement.statement,
+      database: this.database
+      // session:
+    });
+  }
 }

+ 17 - 0
desktop/libs/notebook/src/notebook/api.py

@@ -131,6 +131,11 @@ def _execute_notebook(request, notebook, snippet):
   try:
     try:
       session = notebook.get('sessions') and notebook['sessions'][0] # Session reference for snippet execution without persisting it
+
+      active_executable = json.loads(request.POST.get('executable', '{}')) # Editor v2
+
+      # TODO: Use statement, database etc. from active_executable
+
       if historify:
         history = _historify(notebook, request.user)
         notebook = Notebook(document=history).get_data()
@@ -148,6 +153,18 @@ def _execute_notebook(request, notebook, snippet):
     finally:
       if historify:
         _snippet = [s for s in notebook['snippets'] if s['id'] == snippet['id']][0]
+
+        if 'id' in active_executable: # Editor v2
+          # notebook_executable is the 1-to-1 match of active_executable in the notebook structure
+          notebook_executable = [e for e in _snippet['executor']['executables'] if e['id'] == active_executable['id']][0]
+          notebook_executable['handle'] = response['handle']
+          if history:
+            notebook_executable['history'] = {
+              'id': history.id,
+              'uuid': history.uuid
+            }
+            notebook_executable['operationId'] = history.uuid
+
         if 'handle' in response: # No failure
           if 'result' not in _snippet: # Editor v2
             _snippet['result'] = {}