Browse Source

HUE-5345 [editor] Add editor autocompletion of popular filters

Johan Ahlen 9 years ago
parent
commit
30e9161

+ 22 - 0
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1457,6 +1457,28 @@ var ApiHelper = (function () {
     });
   };
 
+  /**
+   * Fetches the popular filters for the given tables
+   *
+   * @param {Object} options
+   * @param {string} options.sourceType
+   * @param {Function} options.successCallback
+   * @param {Function} [options.errorCallback]
+   * @param {boolean} [options.silenceErrors]
+   *
+   * @param {Object[]} options.tables
+   * @param {Object[]} options.tables.identifierChain
+   * @param {string} options.tables.identifierChain.name
+   * @param {string} [options.defaultDatabase]
+   */
+  ApiHelper.prototype.fetchNavOptTopFilters = function (options) {
+    var self = this;
+
+    self.fetchNavCached('/metadata/api/optimizer/top_filters', options, function (data) {
+      return typeof data.values !== 'undefined' && data.values.length > 0;
+    });
+  };
+
   ApiHelper.prototype.fetchNavCached = function (url, options, cacheCondition) {
     var self = this;
 

+ 62 - 0
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter2.js

@@ -32,6 +32,7 @@ var SqlAutocompleter2 = (function () {
 
   // Keyword weights come from the parser
   var DEFAULT_WEIGHTS = {
+    FILTER: 1300,
     ACTIVE_JOIN: 1200,
     JOIN_CONDITION: 1100,
     COLUMN: 1000,
@@ -263,6 +264,60 @@ var SqlAutocompleter2 = (function () {
       deferrals.push(suggestColRefKeywordsDeferral);
     }
 
+    if (HAS_OPTIMIZER && typeof parseResult.suggestFilters !== 'undefined') {
+      var topFiltersDeferral = $.Deferred();
+      deferrals.push(topFiltersDeferral);
+      self.snippet.getApiHelper().fetchNavOptTopFilters({
+        sourceType: self.snippet.type(),
+        timeout: self.timeout,
+        defaultDatabase: database,
+        silenceErrors: true,
+        tables: parseResult.suggestFilters.tables,
+        successCallback: function (data) {
+          data.values.forEach(function (value) {
+            if (typeof value.popularValues !== 'undefined' && value.popularValues.length > 0) {
+              value.popularValues.forEach(function (popularValue) {
+                if (typeof popularValue.group !== 'undefined') {
+                  popularValue.group.forEach(function (grp) {
+                    var path = value.tableName.split('.').concat(grp.columnName.split('.').slice(1)).join('.');
+
+                    for (var i = 0; i < parseResult.suggestFilters.tables.length; i++) {
+                      var table = parseResult.suggestFilters.tables[i];
+                      var tablePath = '';
+                      if (table.identifierChain.length == 2) {
+                        tablePath = $.map(table.identifierChain, function (identifier) { return identifier.name }).join('.');
+                      } else if (table.identifierChain.length == 1) {
+                        tablePath = database + '.' + table.identifierChain[0].name;
+                      }
+                      if (path.indexOf(tablePath) === 0) {
+                        path = path.substring(tablePath.length + 1);
+                        if (table.alias) {
+                          path = table.alias + '.' + path;
+                        }
+                        break;
+                      }
+                    }
+                    var compVal = parseResult.suggestFilters.prefix ? (parseResult.lowerCase ? parseResult.suggestFilters.prefix.toLowerCase() : parseResult.suggestFilters.prefix) + ' ' : '';
+                    compVal += path + (parseResult.lowerCase ? grp.op.toLowerCase() : grp.op) + grp.literal;
+
+                    completions.push({
+                      value: compVal,
+                      meta: 'filter *',
+                      weight: DEFAULT_WEIGHTS.FILTER,
+                      docHTML: self.createFilterHtml()
+                    });
+                  });
+                }
+              });
+            }
+          });
+
+          topFiltersDeferral.resolve();
+        },
+        errorCallback: topFiltersDeferral.resolve
+      });
+    }
+
     if (parseResult.suggestColumns) {
       var topColumnsDeferral = $.Deferred();
       deferrals.push(topColumnsDeferral);
@@ -458,6 +513,13 @@ var SqlAutocompleter2 = (function () {
     }
   };
 
+  SqlAutocompleter2.prototype.createFilterHtml = function () {
+    // TODO: Show more relevant details here
+    var html = '<div style="max-width: 600px; white-space: normal; overflow-y: auto; height: 100%; padding: 8px;"><p><span style="white-space: pre; font-family: monospace;">Popular filter</span></p>';
+    html += '<div>';
+    return html;
+  };
+
   SqlAutocompleter2.prototype.createTopHtml = function (value) {
     // TODO: Show more relevant details here
     var html = '<div style="max-width: 600px; white-space: normal; overflow-y: auto; height: 100%; padding: 8px;"><p><span style="white-space: pre; font-family: monospace;">Popular column, count: ' + value.columnCount + '</span></p>';