|
@@ -27,14 +27,12 @@ from desktop.lib.exceptions import StructuredException
|
|
|
|
|
|
|
|
from beeswax import models as beeswax_models
|
|
from beeswax import models as beeswax_models
|
|
|
from beeswax.forms import SaveForm
|
|
from beeswax.forms import SaveForm
|
|
|
-from beeswax.views import authorized_get_history, safe_get_design
|
|
|
|
|
|
|
+from beeswax.views import authorized_get_history, safe_get_design, save_design
|
|
|
|
|
|
|
|
-from spark import conf
|
|
|
|
|
-from spark.design import SparkDesign
|
|
|
|
|
-from spark.views import save_design
|
|
|
|
|
from spark.job_server_api import get_api
|
|
from spark.job_server_api import get_api
|
|
|
-from spark.forms import SparkForm, UploadApp
|
|
|
|
|
-import urllib
|
|
|
|
|
|
|
+from spark.forms import SparkForm, QueryForm
|
|
|
|
|
+from desktop.lib.i18n import smart_str
|
|
|
|
|
+from spark.design import SparkDesign
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
LOG = logging.getLogger(__name__)
|
|
@@ -111,11 +109,12 @@ def delete_context(request):
|
|
|
api = get_api(request.user)
|
|
api = get_api(request.user)
|
|
|
try:
|
|
try:
|
|
|
response = api.delete_context(name)
|
|
response = api.delete_context(name)
|
|
|
|
|
+ except ValueError:
|
|
|
|
|
+ # No json is returned
|
|
|
|
|
+ response = {'status': 'OK'}
|
|
|
except Exception, e:
|
|
except Exception, e:
|
|
|
- if e.message != 'No JSON object could be decoded':
|
|
|
|
|
- response = json.loads(e.message)
|
|
|
|
|
- else:
|
|
|
|
|
- response = {'status': 'OK'}
|
|
|
|
|
|
|
+ response = json.loads(e.message)
|
|
|
|
|
+
|
|
|
response['name'] = name
|
|
response['name'] = name
|
|
|
|
|
|
|
|
return HttpResponse(json.dumps(response), mimetype="application/json")
|
|
return HttpResponse(json.dumps(response), mimetype="application/json")
|
|
@@ -182,51 +181,6 @@ def execute(request, design_id=None):
|
|
|
return HttpResponse(json.dumps(response, cls=ResultEncoder), mimetype="application/json")
|
|
return HttpResponse(json.dumps(response, cls=ResultEncoder), mimetype="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
-@error_handler
|
|
|
|
|
-def fetch_results(request, id, first_row=0):
|
|
|
|
|
- """
|
|
|
|
|
- Returns the results of the QueryHistory with the given id.
|
|
|
|
|
-
|
|
|
|
|
- The query results MUST be ready.
|
|
|
|
|
-
|
|
|
|
|
- If ``first_row`` is 0, restarts (if necessary) the query read. Otherwise, just
|
|
|
|
|
- spits out a warning if first_row doesn't match the servers conception.
|
|
|
|
|
- Multiple readers will produce a confusing interaction here, and that's known.
|
|
|
|
|
- """
|
|
|
|
|
- first_row = long(first_row)
|
|
|
|
|
- results = type('Result', (object,), {
|
|
|
|
|
- 'rows': 0,
|
|
|
|
|
- 'columns': [],
|
|
|
|
|
- 'has_more': False,
|
|
|
|
|
- 'start_row': 0,
|
|
|
|
|
- })
|
|
|
|
|
- fetch_error = False
|
|
|
|
|
- error_message = ''
|
|
|
|
|
-
|
|
|
|
|
- query_history = authorized_get_history(request, id, must_exist=True)
|
|
|
|
|
- query_server = query_history.get_query_server_config()
|
|
|
|
|
- design = SQLdesign.loads(query_history.design.data)
|
|
|
|
|
- db = dbms.get(request.user, query_server)
|
|
|
|
|
-
|
|
|
|
|
- try:
|
|
|
|
|
- database = design.query.get('database', 'default')
|
|
|
|
|
- db.use(database)
|
|
|
|
|
- datatable = db.execute_and_wait(design)
|
|
|
|
|
- results = db.client.create_result(datatable)
|
|
|
|
|
- status = 0
|
|
|
|
|
- except Exception, e:
|
|
|
|
|
- fetch_error = True
|
|
|
|
|
- error_message = str(e)
|
|
|
|
|
- status = -1
|
|
|
|
|
-
|
|
|
|
|
- response = {
|
|
|
|
|
- 'status': status,
|
|
|
|
|
- 'message': fetch_error and error_message or '',
|
|
|
|
|
- 'results': results_to_dict(results)
|
|
|
|
|
- }
|
|
|
|
|
- return HttpResponse(json.dumps(response), mimetype="application/json")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
@error_handler
|
|
@error_handler
|
|
|
def save_query(request, design_id=None):
|
|
def save_query(request, design_id=None):
|
|
|
response = {'status': -1, 'message': ''}
|
|
response = {'status': -1, 'message': ''}
|
|
@@ -237,17 +191,20 @@ def save_query(request, design_id=None):
|
|
|
app_name = get_app_name(request)
|
|
app_name = get_app_name(request)
|
|
|
query_type = beeswax_models.SavedQuery.TYPES_MAPPING[app_name]
|
|
query_type = beeswax_models.SavedQuery.TYPES_MAPPING[app_name]
|
|
|
design = safe_get_design(request, query_type, design_id)
|
|
design = safe_get_design(request, query_type, design_id)
|
|
|
|
|
+ form = QueryForm()
|
|
|
|
|
+ api = get_api(request.user)
|
|
|
|
|
+ app_names = api.jars()
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
- save_form = SaveForm(request.POST.copy())
|
|
|
|
|
- query_form = get_query_form(request, design_id)
|
|
|
|
|
|
|
+ form.bind(request.POST)
|
|
|
|
|
+ form.query.fields['appName'].choices = ((key, key) for key in app_names)
|
|
|
|
|
|
|
|
- if query_form.is_valid() and save_form.is_valid():
|
|
|
|
|
- design = save_design(request, save_form, query_form, query_type, design, True)
|
|
|
|
|
|
|
+ if form.is_valid():
|
|
|
|
|
+ design = save_design(request, form, query_type, design, True)
|
|
|
response['design_id'] = design.id
|
|
response['design_id'] = design.id
|
|
|
response['status'] = 0
|
|
response['status'] = 0
|
|
|
else:
|
|
else:
|
|
|
- response['errors'] = query_form.errors
|
|
|
|
|
|
|
+ response['message'] = smart_str(form.query.errors) + smart_str(form.saveform.errors)
|
|
|
except RuntimeError, e:
|
|
except RuntimeError, e:
|
|
|
response['message'] = str(e)
|
|
response['message'] = str(e)
|
|
|
|
|
|
|
@@ -269,37 +226,26 @@ def fetch_saved_query(request, design_id):
|
|
|
return HttpResponse(json.dumps(response), mimetype="application/json")
|
|
return HttpResponse(json.dumps(response), mimetype="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
-def results_to_dict(results):
|
|
|
|
|
- data = {}
|
|
|
|
|
- rows = []
|
|
|
|
|
- for row in results.rows():
|
|
|
|
|
- rows.append(dict(zip(results.columns, row)))
|
|
|
|
|
- data['rows'] = rows
|
|
|
|
|
- data['start_row'] = results.start_row
|
|
|
|
|
- data['has_more'] = results.has_more
|
|
|
|
|
- data['columns'] = results.columns
|
|
|
|
|
- return data
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
def design_to_dict(design):
|
|
def design_to_dict(design):
|
|
|
- sql_design = SQLdesign.loads(design.data)
|
|
|
|
|
|
|
+ spark_design = SparkDesign.loads(design.data)
|
|
|
return {
|
|
return {
|
|
|
'id': design.id,
|
|
'id': design.id,
|
|
|
- 'query': sql_design.sql_query,
|
|
|
|
|
'name': design.name,
|
|
'name': design.name,
|
|
|
'desc': design.desc,
|
|
'desc': design.desc,
|
|
|
- 'server': sql_design.server,
|
|
|
|
|
- 'database': sql_design.database
|
|
|
|
|
|
|
+ 'appName': spark_design.appName,
|
|
|
|
|
+ 'classPath': spark_design.classPath,
|
|
|
|
|
+ 'autoContext': spark_design.autoContext,
|
|
|
|
|
+ 'context': spark_design.context,
|
|
|
|
|
+ 'params': spark_design.params,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
def get_query_form(request):
|
|
def get_query_form(request):
|
|
|
api = get_api(request.user)
|
|
api = get_api(request.user)
|
|
|
|
|
|
|
|
app_names = api.jars()
|
|
app_names = api.jars()
|
|
|
|
|
|
|
|
if not app_names:
|
|
if not app_names:
|
|
|
- raise RuntimeError(_("Server specified a jar name."))
|
|
|
|
|
|
|
+ raise RuntimeError(_("Missing application jar list."))
|
|
|
|
|
|
|
|
form = SparkForm(request.POST, app_names=app_names)
|
|
form = SparkForm(request.POST, app_names=app_names)
|
|
|
|
|
|