Răsfoiți Sursa

HUE-9143 [editor] Move common meta adaptions into executionResult

Johan Ahlen 5 ani în urmă
părinte
comite
d56c13123e

+ 13 - 47
desktop/core/src/desktop/js/apps/notebook2/components/ko.snippetResults.js

@@ -33,30 +33,6 @@ import { CURRENT_QUERY_TAB_SWITCHED_EVENT } from 'apps/notebook2/snippet';
 
 export const NAME = 'snippet-results';
 
-const META_TYPE_TO_CSS = {
-  TINYINT_TYPE: 'sort-numeric',
-  SMALLINT_TYPE: 'sort-numeric',
-  INT_TYPE: 'sort-numeric',
-  BIGINT_TYPE: 'sort-numeric',
-  FLOAT_TYPE: 'sort-numeric',
-  DOUBLE_TYPE: 'sort-numeric',
-  DECIMAL_TYPE: 'sort-numeric',
-  TIMESTAMP_TYPE: 'sort-date',
-  DATE_TYPE: 'sort-date',
-  DATETIME_TYPE: 'sort-date'
-};
-
-const isNumericColumn = type =>
-  ['tinyint', 'smallint', 'int', 'bigint', 'float', 'double', 'decimal', 'real'].indexOf(type) !==
-  -1;
-
-const isDateTimeColumn = type => ['timestamp', 'date', 'datetime'].indexOf(type) !== -1;
-
-const isComplexColumn = type => ['array', 'map', 'struct'].indexOf(type) !== -1;
-
-const isStringColumn = type =>
-  !isNumericColumn(type) && !isDateTimeColumn(type) && !isComplexColumn(type);
-
 // prettier-ignore
 const TEMPLATE = `
 <div class="snippet-row">
@@ -190,19 +166,10 @@ class SnippetResults extends DisposableComponent {
 
     attachTracker(this.activeExecutable, NAME, this, trackedObservables);
 
-    this.cleanedMeta = ko.pureComputed(() => this.meta().filter(item => item.name !== ''));
-
-    this.cleanedDateTimeMeta = ko.pureComputed(() =>
-      this.meta().filter(item => item.name !== '' && isDateTimeColumn(item.type))
-    );
-
-    self.cleanedStringMeta = ko.pureComputed(() =>
-      this.meta().filter(item => item.name !== '' && isStringColumn(item.type))
-    );
-
-    this.cleanedNumericMeta = ko.pureComputed(() =>
-      this.meta().filter(item => item.name !== '' && isNumericColumn(item.type))
-    );
+    this.cleanedMeta = ko.observableArray();
+    this.cleanedDateTimeMeta = ko.observableArray();
+    this.cleanedStringMeta = ko.observableArray();
+    this.cleanedNumericMeta = ko.observableArray();
 
     this.subscribe(this.showChart, val => {
       if (val) {
@@ -253,6 +220,10 @@ class SnippetResults extends DisposableComponent {
     this.lastFetchedRows([]);
     this.data([]);
     this.meta([]);
+    this.cleanedMeta([]);
+    this.cleanedDateTimeMeta([]);
+    this.cleanedNumericMeta([]);
+    this.cleanedStringMeta([]);
     this.hasMore(false);
     this.type(RESULT_TYPE.TABLE);
   }
@@ -268,16 +239,11 @@ class SnippetResults extends DisposableComponent {
       this.type(executionResult.type);
 
       if (!this.meta().length && executionResult.meta.length) {
-        this.meta(
-          executionResult.meta.map((item, index) => ({
-            name: item.name,
-            type: item.type.replace(/_type/i, '').toLowerCase(),
-            comment: item.comment,
-            cssClass: META_TYPE_TO_CSS[item.type] || 'sort-string',
-            checked: ko.observable(true),
-            originalIndex: index
-          }))
-        );
+        this.meta(executionResult.koEnrichedMeta);
+        this.cleanedMeta(executionResult.cleanedMeta);
+        this.cleanedDateTimeMeta(executionResult.cleanedDateTimeMeta);
+        this.cleanedStringMeta(executionResult.cleanedStringMeta);
+        this.cleanedNumericMeta(executionResult.cleanedNumericMeta);
       }
 
       if (executionResult.lastRows.length) {

+ 68 - 0
desktop/core/src/desktop/js/apps/notebook2/execution/executionResult.js

@@ -14,6 +14,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import * as ko from 'knockout';
+
 import apiHelper from 'api/apiHelper';
 import huePubSub from 'utils/huePubSub';
 import { sleep } from 'utils/hueUtils';
@@ -25,6 +27,43 @@ export const RESULT_TYPE = {
   TABLE: 'table'
 };
 
+const META_TYPE_TO_CSS = {
+  bigint: 'sort-numeric',
+  date: 'sort-date',
+  datetime: 'sort-date',
+  decimal: 'sort-numeric',
+  double: 'sort-numeric',
+  float: 'sort-numeric',
+  int: 'sort-numeric',
+  real: 'sort-numeric',
+  smallint: 'sort-numeric',
+  timestamp: 'sort-date',
+  tinyint: 'sort-numeric'
+};
+
+const NUMERIC_TYPES = {
+  bigint: true,
+  decimal: true,
+  double: true,
+  float: true,
+  int: true,
+  real: true,
+  smallint: true,
+  tinyint: true
+};
+
+const DATE_TIME_TYPES = {
+  date: true,
+  datetime: true,
+  timestamp: true
+};
+
+const COMPLEX_TYPES = {
+  array: true,
+  map: true,
+  struct: true
+};
+
 export default class ExecutionResult {
   /**
    *
@@ -36,6 +75,13 @@ export default class ExecutionResult {
     this.type = RESULT_TYPE.TABLE;
     this.rows = [];
     this.meta = [];
+
+    this.cleanedMeta = [];
+    this.cleanedDateTimeMeta = [];
+    this.cleanedStringMeta = [];
+    this.cleanedNumericMeta = [];
+    this.koEnrichedMeta = [];
+
     this.lastRows = [];
     this.images = [];
     this.type = undefined;
@@ -106,6 +152,28 @@ export default class ExecutionResult {
     if (!this.meta.length) {
       this.meta = resultResponse.meta;
       this.meta.unshift({ type: 'INT_TYPE', name: '', comment: null });
+
+      this.meta.forEach((item, index) => {
+        const cleanedType = item.type.replace(/_type/i, '').toLowerCase();
+        if (index) {
+          this.cleanedMeta.push(item);
+          if (NUMERIC_TYPES[cleanedType]) {
+            this.cleanedNumericMeta.push(item);
+          } else if (DATE_TIME_TYPES[cleanedType]) {
+            this.cleanedDateTimeMeta.push(item);
+          } else if (!COMPLEX_TYPES[cleanedType]) {
+            this.cleanedStringMeta.push(item);
+          }
+        }
+        this.koEnrichedMeta.push({
+          name: item.name,
+          type: cleanedType,
+          comment: item.comment,
+          cssClass: META_TYPE_TO_CSS[cleanedType] || 'sort-string',
+          checked: ko.observable(true),
+          originalIndex: index
+        });
+      });
     }
     this.hasMore = resultResponse.has_more;
     this.isEscaped = resultResponse.isEscaped;