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

HUE-6077 [editor] Started refactoring of query hints in assistant

Romain Rigaux 8 жил өмнө
parent
commit
113a1ff

+ 12 - 12
desktop/core/src/desktop/templates/assist.mako

@@ -1746,28 +1746,25 @@ from notebook.conf import get_ordered_interpreters
 
       <form class="form-horizontal">
         <fieldset>
-          <div data-bind="visible: activeRisks().length > 0">${ _('Suggestions') }</div>
-          <ul data-bind="foreach: activeRisks">
+          <div>${ _('Suggestions') }</div>
+          <!-- ko if: hasActiveRisks -->
+          <ul data-bind="foreach: activeRisks()['hints']">
             <li>
               <span data-bind="text: risk"></span>
               <span data-bind="text: riskAnalysis"></span>
               <span data-bind="text: riskRecommendation"></span>
             </li>
           </ul>
+          <!-- /ko -->
         </fieldset>
       </form>
 
       <!-- ko if: HAS_OPTIMIZER -->
-        <a href="javascript:void(0)" data-bind="click: function() { huePubSub.publish('editor.workload.upload'); }" title="${ _('Load past query history in order to improve recommendations') }">
-          <i class="fa fa-fw fa-cloud-upload"></i> ${_('Upload workload')}
-        </a>
-        <a href="javascript:void(0)" data-bind="visible: activeTables().length > 0, click: function() { huePubSub.publish('editor.table.stats.upload', activeTables()); }" title="${ _('Load table and columns stats in order to improve recommendations') }">
-          <i class="fa fa-fw fa-cloud-upload"></i> ${_('Upload DDL')}
-        </a>
-        </br>
-        <a href="javascript:void(0)" data-bind="click: function() { huePubSub.publish('editor.workload.upload'); }" title="${ _('Load past query history in order to improve recommendations') }">
-          <i class="fa fa-fw fa-gears"></i> ${_('Analyse Query')}
+        <!-- ko if: hasActiveRisks() && activeRisks()['noDDL'].length > 0 -->
+        <a href="javascript:void(0)" data-bind="visible: activeTables().length > 0, click: function() { huePubSub.publish('editor.table.stats.upload', activeTables()); }" title="${ _('Load table and columns DDL/stats in order to improve recommendations') }">
+          <i class="fa fa-fw fa-gears"></i> ${_('Optimize Analysis')}
         </a>
+        <!-- /ko -->
       <!-- /ko -->
     </div>
   </script>
@@ -1785,7 +1782,10 @@ from notebook.conf import get_ordered_interpreters
         self.activeSourceType = ko.observable();
         self.activeTables = ko.observableArray();
         self.activeColumns = ko.observableArray();
-        self.activeRisks = ko.observableArray()
+        self.activeRisks = ko.observable({})
+        self.hasActiveRisks = ko.pureComputed(function () {
+           return Object.keys(self.activeRisks()).length > 0;
+        });
         self.statementCount = ko.observable(0);
         self.activeStatementIndex = ko.observable(0);
 

+ 2 - 1
desktop/libs/metadata/src/metadata/optimizer_api.py

@@ -174,10 +174,11 @@ def query_risk(request):
 
   query = json.loads(request.POST.get('query'))
   source_platform = request.POST.get('sourcePlatform')
+  db_name = request.POST.get('dbName')
 
   api = OptimizerApi()
 
-  data = api.query_risk(query=query, source_platform=source_platform)
+  data = api.query_risk(query=query, source_platform=source_platform, db_name=db_name)
 
   if data:
     response['status'] = 0

+ 20 - 6
desktop/libs/metadata/src/metadata/optimizer_client.py

@@ -73,6 +73,8 @@ class OptimizerApi(object):
 
     if data.get('code') == 'UNKNOWN':
       raise NavOptException(data.get('message'))
+    elif data.get('errorMsg'):
+      raise NavOptException(data.get('errorMsg'))
     else:
       return data
 
@@ -161,15 +163,27 @@ class OptimizerApi(object):
     return self._call('getQueryCompatible', {'tenant' : self._product_name, 'query': query, 'sourcePlatform': source_platform, 'targetPlatform': target_platform, })
 
 
-  def query_risk(self, query, source_platform, page_size=100, startingToken=None):
-    response = self._call('getQueryRisk', {'tenant' : self._product_name, 'query': query, 'sourcePlatform': source_platform, 'pageSize': page_size, startingToken: None})
-    data = response.get(source_platform + 'Risk', {})
+  def query_risk(self, query, source_platform, db_name, page_size=100, startingToken=None):
+    response = self._call('getQueryRisk', {
+      'tenant' : self._product_name,
+      'query': query,
+      'dbName': db_name,
+      'sourcePlatform': source_platform,
+      'pageSize': page_size,
+      startingToken: None
+    })
 
-    if data and data == [{u'riskAnalysis': u'', u'risk': u'low', u'riskRecommendation': u''}]:
-      data = []
+    hints = response.get(source_platform + 'Risk', {})
 
-    return data
+    if hints and hints == [{u'riskAnalysis': u'', u'risk': u'low', u'riskId': 0, u'riskRecommendation': u''}]:
+      hints = []
 
+    return {
+      'hints': hints,
+      'tables': response.get('tables', []),
+      'noStats': response.get('noStats', []),
+      'noDDL': response.get('noStats', []),
+    }
 
   def similar_queries(self, source_platform, query, page_size=100, startingToken=None):
     return self._call('getSimilarQueries', {'tenant' : self._product_name, 'sourcePlatform': source_platform, 'query': query, 'pageSize': page_size, startingToken: None})

+ 1 - 7
desktop/libs/notebook/src/notebook/connectors/hiveserver2.py

@@ -533,13 +533,7 @@ DROP TABLE IF EXISTS `%(table)s`;
 
     api = OptimizerApi()
 
-    data = api.query_risk(query=query, source_platform=snippet['type'])
-
-    return [{
-      'risk': risk.get('risk'),
-      'riskAnalysis': risk.get('riskAnalysis'),
-      'riskRecommendation': risk.get('riskRecommendation')
-    } for risk in data]
+    return api.query_risk(query=query, source_platform=snippet['type'], db_name=snippet.get('database') or 'default')
 
 
   def statement_compatibility(self, notebook, snippet, source_platform, target_platform):

+ 10 - 7
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -834,9 +834,12 @@ var EditorViewModel = (function() {
       };
     };
 
-    self.complexity = ko.observableArray();
-    self.hasComplexity = ko.computed(function () {
-      return self.complexity().length > 0;
+    self.complexity = ko.observable({});
+    self.hasComplexity = ko.pureComputed(function () {
+      return Object.keys(self.complexity()).length > 0;
+    });
+    self.hasRisks = ko.pureComputed(function () {
+      return self.hasComplexity() && self.complexity()['hints'].length > 0;
     });
 
     self.suggestion = ko.observable('');
@@ -899,12 +902,12 @@ var EditorViewModel = (function() {
 
         hueAnalytics.log('notebook', 'get_query_risk');
         self.complexityCheckRunning(true);
-        huePubSub.publish('editor.active.risks', []);
+        huePubSub.publish('editor.active.risks', {});
 
         lastComplexityRequest = $.ajax({
           type: 'POST',
           url: '/notebook/api/optimizer/statement/risk',
-          timeout: 10000, // 10 seconds
+          timeout: 15000, // 15 seconds
           data: {
             notebook: ko.mapping.toJSON(notebook.getContext()),
             snippet: ko.mapping.toJSON(self.getContext())
@@ -914,8 +917,8 @@ var EditorViewModel = (function() {
               self.complexity(data.query_complexity);
               self.hasSuggestion('');
             } else {
-              // TODO: Silence errors
-              $(document).trigger('error', data.message);
+              self.hasSuggestion(data.message); // TODO Properly inform user
+              self.complexity({});
             }
             huePubSub.publish('editor.active.risks', self.complexity());
             lastCheckedComplexityStatement = self.statement_raw();

+ 16 - 6
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1060,22 +1060,25 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
       <!-- /ko -->
       <!-- /ko -->
     <!-- /ko -->
-    <!-- ko if: ! hasSuggestion() && hasComplexity() -->
-      <!-- ko if: complexity()[0].risk.length === 0 || complexity()[0].risk === 'low' -->
+    <!-- ko if: ! hasSuggestion() && hasRisks() -->
+      <!-- ko if: complexity()['hints'][0].risk === 'low' -->
         <div class="round-icon success" data-bind="click: function(){ showOptimizer(! showOptimizer()) }, attr: { 'title': showOptimizer() ? '${ _ko('Close Validator') }' : '${ _ko('Open Validator') }'}">
           <i class="fa fa-check"></i>
         </div>
       <!-- /ko -->
-      <!-- ko if: complexity()[0].risk === 'high' -->
+      <!-- ko if: complexity()['hints'][0].risk != 'low' -->
         <div class="round-icon error" data-bind="click: function(){ showOptimizer(! showOptimizer()) }">
           <i class="fa fa-exclamation"></i>
         </div>
         <!-- ko if: showOptimizer -->
-        <span class="optimizer-explanation alert-error alert-neutral"><strong data-bind="text: complexity()[0].riskAnalysis"></strong> <span data-bind="text: complexity()[0].riskRecommendation"></span></span>
+        <span class="optimizer-explanation alert-error alert-neutral"><strong data-bind="text: complexity()['hints'][0].riskAnalysis"></strong> <span data-bind="text: complexity()['hints'][0].riskRecommendation"></span></span>
         <!-- /ko -->
       <!-- /ko -->
     <!-- /ko -->
-    <!-- ko if: ! hasSuggestion() && ! hasComplexity() -->
+    <!-- ko if: hasSuggestion() == '' && ! hasRisks() -->
+      <div class="round-icon success" data-bind="click: function(){ showOptimizer(! showOptimizer()) }, attr: { 'title': showOptimizer() ? '${ _ko('Close Validator') }' : '${ _ko('Open Validator') }'}">
+        <i class="fa fa-check"></i>
+      </div>
       <!-- ko if: showOptimizer -->
         <span class="optimizer-explanation alert-success alert-neutral">${ _('Query validated.') }</span>
       <!-- /ko -->
@@ -1703,6 +1706,13 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
             <i class="fa fa-fw fa-random"></i> ${_('Check compatibility')}
           </a>
         </li>
+        % if user.is_superuser:
+        <li>
+          <a href="javascript:void(0)" data-bind="click: function() { huePubSub.publish('editor.workload.upload'); }" title="${ _('Load past query history in order to improve recommendations') }">
+            <i class="fa fa-fw fa-cloud-upload"></i> ${_('Upload history')}
+          </a>
+        </li>
+        % endif
         <!-- /ko -->
       </ul>
     </div>
@@ -3074,7 +3084,7 @@ from notebook.conf import ENABLE_QUERY_BUILDER, ENABLE_QUERY_SCHEDULING, ENABLE_
       });
 
       huePubSub.subscribe("editor.workload.upload", function () {
-        viewModel.selectedNotebook().snippets()[0].loadQueryHistory(10);
+        viewModel.selectedNotebook().snippets()[0].loadQueryHistory(100);
       });