فهرست منبع

HUE-8758 [editor] Supporting installing SQL query samples based on the dialect

Romain 5 سال پیش
والد
کامیت
ee29f226e3

+ 1 - 1
apps/beeswax/data/queries.json

@@ -2,7 +2,7 @@
   {
     "name": "Sample: Salary Analysis",
     "desc": "Top salary 2007 above $100k, Salary growth (sorted) from 2007-08",
-    "type": "0",
+    "type": "2",
     "dialects": ["postgresql", "mysql", "presto"],
     "data": {
       "query": {

+ 9 - 8
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples.py

@@ -36,7 +36,7 @@ from useradmin.models import get_default_user_group, install_sample_user, User
 from beeswax.design import hql_query
 from beeswax.conf import LOCAL_EXAMPLES_DATA_DIR
 from beeswax.hive_site import has_concurrency_support
-from beeswax.models import SavedQuery, HQL, IMPALA
+from beeswax.models import SavedQuery, HQL, IMPALA, RDBMS
 from beeswax.server import dbms
 from beeswax.server.dbms import get_query_server_config, QueryServerException
 
@@ -64,19 +64,15 @@ class Command(BaseCommand):
       user = options['user']
 
     tables = options['tables'] if 'tables' in options else ('tables_transactional.json' if has_concurrency_support() else 'tables.json')
-
     exception = None
 
-    # Documents will belong to this user but we run the install as the current user
     try:
-      sample_user = install_sample_user(user)
+      sample_user = install_sample_user(user)  # Documents will belong to this user but we run the install as the current user
       self._install_queries(sample_user, app_name, interpreter=interpreter)
       self._install_tables(user, app_name, db_name, tables, interpreter=interpreter)
     except Exception as ex:
       exception = ex
 
-    Document.objects.sync()
-
     if exception is not None:
       pretty_msg = None
 
@@ -109,8 +105,13 @@ class Command(BaseCommand):
     design_file.close()
 
     # Filter design list to app-specific designs
-    app_type = HQL if app_name == 'beeswax' else IMPALA if app_name == 'impala' else 'sql'
+    app_type = HQL if app_name == 'beeswax' else IMPALA if app_name == 'impala' else RDBMS
     design_list = [d for d in design_list if int(d['type']) == app_type]
+    if app_type == RDBMS:
+      design_list = [d for d in design_list if app_name in d['dialects']]
+
+    if not design_list:
+      raise InstallException(_('No %s queries are available as samples') % app_name)
 
     for design_dict in design_list:
       design = SampleQuery(design_dict)
@@ -406,6 +407,6 @@ class SampleQuery(object):
     elif type == IMPALA:
       return 'query-impala'
     elif interpreter:
-      return 'query-%(dialect)s' % interpreter
+      return 'query-%(type)s' % interpreter
     else:
       return None

+ 42 - 3
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples_tests.py

@@ -43,11 +43,11 @@ class TestStandardTables():
     self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
     self.user = User.objects.get(username="test")
 
-  def test_install_queries(self):
+  def test_install_queries_mysql(self):
       design_dict = {
         "name": "TestStandardTables Query",
         "desc": "Small query",
-        "type": "0",
+        "type": "2",
         "dialects": ["postgresql", "mysql", "presto"],
         "data": {
           "query": {
@@ -73,7 +73,7 @@ class TestStandardTables():
 
         assert_true(Document2.objects.filter(name='TestStandardTables Query').exists())
         query = Document2.objects.filter(name='TestStandardTables Query').get()
-        assert_equal('query-hive', query.type)
+        assert_equal('query-mysql', query.type)
 
   # def test_install_queries(self):
   #     sample_user = install_sample_user()
@@ -83,6 +83,45 @@ class TestStandardTables():
   #     cmd._install_queries(sample_user, app_name, interpreter=interpreter)
 
 
+
+class TestBeswaxHiveTables():
+
+  def setUp(self):
+    self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
+    self.user = User.objects.get(username="test")
+
+  def test_install_queries(self):
+      design_dict = {
+        "name": "TestBeswaxHiveTables Query",
+        "desc": "Small query",
+        "type": "0",
+        "data": {
+          "query": {
+            "query": "SELECT 1",
+            "type": 0,
+            "email_notify": False,
+            "is_parameterized": False,
+            "database": "default"
+          },
+          "functions": [],
+          "VERSION": "0.4.1",
+          "file_resources": [],
+          "settings": []
+        }
+      }
+      interpreter = {'type': 'hive'}
+
+      design = SampleQuery(design_dict)
+      assert_false(Document2.objects.filter(name='TestBeswaxHiveTables Query').exists())
+
+      with patch('notebook.models.get_interpreter') as get_interpreter:
+        design.install(django_user=self.user, interpreter=interpreter)
+
+        assert_true(Document2.objects.filter(name='TestBeswaxHiveTables Query').exists())
+        query = Document2.objects.filter(name='TestBeswaxHiveTables Query').get()
+        assert_equal('query-hive', query.type)
+
+
 class TestTransactionalTables():
 
   def setUp(self):

+ 16 - 17
desktop/libs/notebook/src/notebook/views.py

@@ -24,6 +24,7 @@ from django.db.models import Q
 from django.shortcuts import redirect
 from django.utils.translation import ugettext as _
 from django.views.decorators.clickjacking import xframe_options_exempt
+from django.views.decorators.http import require_POST
 
 from beeswax.data_export import DOWNLOAD_COOKIE_AGE
 from beeswax.management.commands import beeswax_install_examples
@@ -374,26 +375,24 @@ def download(request):
   return response
 
 
+@require_POST
 @admin_required
 def install_examples(request):
   response = {'status': -1, 'message': ''}
 
-  if request.method == 'POST':
-    try:
-      connector = Connector.objects.get(id=request.POST.get('connector'))
-      if connector:
-        app_name = 'beeswax' if connector.dialect == 'hive' else connector.dialect
-        db_name = request.POST.get('db_name', 'default')
-        interpreter = get_interpreter(connector_type=connector.to_dict()['type'], user=request.user)
-
-        beeswax_install_examples.Command().handle(app_name=app_name, db_name=db_name, user=request.user, interpreter=interpreter)
-      else:
-        Command().handle(user=request.user)
-        response['status'] = 0
-    except Exception as err:
-      LOG.exception(err)
-      response['message'] = str(err)
-  else:
-    response['message'] = _('A POST request is required.')
+  try:
+    connector = Connector.objects.get(id=request.POST.get('connector'))
+    if connector:
+      app_name = 'beeswax' if connector.dialect == 'hive' else connector.dialect
+      db_name = request.POST.get('db_name', 'default')
+      interpreter = get_interpreter(connector_type=connector.to_dict()['type'], user=request.user)
+
+      beeswax_install_examples.Command().handle(app_name=app_name, db_name=db_name, user=request.user, interpreter=interpreter)
+    else:
+      Command().handle(user=request.user)
+    response['status'] = 0
+  except Exception as e:
+    LOG.exception('Error during Editor samples installation.')
+    response['message'] = str(e)
 
   return JsonResponse(response)