Browse Source

HUE-6587 [optimizer] Query risk statement format is not always a single line

Romain Rigaux 8 years ago
parent
commit
4674262624

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

@@ -30,9 +30,10 @@ from libsentry.privilege_checker import MissingSentryPrivilegeException
 from notebook.api import _get_statement
 from notebook.api import _get_statement
 from notebook.models import Notebook
 from notebook.models import Notebook
 
 
-from metadata.optimizer_client import OptimizerApi, NavOptException, _get_table_name
+from metadata.optimizer_client import OptimizerApi, NavOptException, _get_table_name, _clean_query
 from metadata.conf import OPTIMIZER
 from metadata.conf import OPTIMIZER
 
 
+
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
 
 
 
 
@@ -309,7 +310,7 @@ def _convert_queries(queries_data):
       if 'guid' in snippet['result']['handle']: # Not failed query
       if 'guid' in snippet['result']['handle']: # Not failed query
         original_query_id = '%s:%s' % struct.unpack(b"QQ", base64.decodestring(snippet['result']['handle']['guid']))
         original_query_id = '%s:%s' % struct.unpack(b"QQ", base64.decodestring(snippet['result']['handle']['guid']))
         execution_time = snippet['result']['executionTime'] * 100 if snippet['status'] in ('available', 'expired') else -1
         execution_time = snippet['result']['executionTime'] * 100 if snippet['status'] in ('available', 'expired') else -1
-        statement = ' '.join([line for line in _get_statement(query_data).strip().splitlines() if not line.strip().startswith('--')])
+        statement = _clean_query(_get_statement(query_data))
         queries.append((original_query_id, execution_time, statement, snippet.get('database', 'default').strip()))
         queries.append((original_query_id, execution_time, statement, snippet.get('database', 'default').strip()))
     except Exception, e:
     except Exception, e:
       LOG.warning('Skipping upload of %s: %s' % (query_data['uuid'], e))
       LOG.warning('Skipping upload of %s: %s' % (query_data['uuid'], e))

+ 5 - 1
desktop/libs/metadata/src/metadata/optimizer_client.py

@@ -207,7 +207,7 @@ class OptimizerApi(object):
   def query_risk(self, query, source_platform, db_name, page_size=100, startingToken=None):
   def query_risk(self, query, source_platform, db_name, page_size=100, startingToken=None):
     response = self._call('getQueryRisk', {
     response = self._call('getQueryRisk', {
       'tenant' : self._tenant_id,
       'tenant' : self._tenant_id,
-      'query': query,
+      'query': _clean_query(query),
       'dbName': db_name,
       'dbName': db_name,
       'sourcePlatform': source_platform,
       'sourcePlatform': source_platform,
       'pageSize': page_size,
       'pageSize': page_size,
@@ -389,6 +389,10 @@ def _secure_results(results, user, action='SELECT'):
       return results
       return results
 
 
 
 
+def _clean_query(query):
+  return ' '.join([line for line in query.strip().splitlines() if not line.strip().startswith('--')])
+
+
 def _get_tenant_id(api):
 def _get_tenant_id(api):
   tenant_id = OPTIMIZER.TENANT_ID.get() or cache.get(OPTIMIZER_TENANT_ID_CACHE_KEY)
   tenant_id = OPTIMIZER.TENANT_ID.get() or cache.get(OPTIMIZER_TENANT_ID_CACHE_KEY)
   if not tenant_id:
   if not tenant_id: