Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@require_POST
@check_document_access_permission()
@api_error_handler
def autocomplete(request, server=None, database=None, table=None, column=None, nested=None):
response = {'status': -1}
# Passed by check_document_access_permission but unused by APIs
notebook = json.loads(request.POST.get('notebook', '{}'))
snippet = json.loads(request.POST.get('snippet', '{}'))
try:
autocomplete_data = get_api(request, snippet).autocomplete(snippet, database, table, column, nested)
response.update(autocomplete_data)
except QueryExpired:
pass
response['status'] = 0
return JsonResponse(response)
def download_to_file(notebook, snippet, file_format='csv', max_rows=-1, **kwargs):
download_to_file.update_state(task_id=notebook['uuid'], state='STARTED', meta={})
request = _get_request(**kwargs)
api = get_api(request, snippet)
meta = {'row_counter': 0, 'handle': {}, 'status': '', 'truncated': False}
with storage.open(_log_key(notebook), 'wb') as f_log: # TODO: use cache for editor 1000 rows and storage for result export
result_wrapper = ExecutionWrapper(api, notebook, snippet, ExecutionWrapperCallback(notebook['uuid'], meta, f_log))
content_generator = data_export.DataAdapter(result_wrapper, max_rows=max_rows, store_data_type_in_header=True) # TODO: Move FETCH_RESULT_LIMIT to front end
response = export_csvxls.create_generator(content_generator, file_format)
with storage.open(_result_key(notebook), 'wb') as f:
for chunk in response:
f.write(chunk)
meta['row_counter'] = content_generator.row_counter
meta['truncated'] = content_generator.is_truncated
download_to_file.update_state(task_id=notebook['uuid'], state='AVAILABLE', meta=meta)
if TASK_SERVER.RESULT_CACHE.get():
@require_POST
@check_document_access_permission()
@api_error_handler
def check_status(request):
response = {'status': -1}
notebook = json.loads(request.POST.get('notebook', '{}'))
snippet = json.loads(request.POST.get('snippet', '{}'))
if not snippet:
nb_doc = Document2.objects.get_by_uuid(user=request.user, uuid=notebook['id'])
notebook = Notebook(document=nb_doc).get_data()
snippet = notebook['snippets'][0]
try:
response['query_status'] = get_api(request, snippet).check_status(notebook, snippet)
response['status'] = 0
except SessionExpired:
response['status'] = 'expired'
raise
except QueryExpired:
response['status'] = 'expired'
raise
finally:
if response['status'] == 0 and snippet['status'] != response['query_status']:
status = response['query_status']['status']
elif response['status'] == 'expired':
status = 'expired'
else:
status = 'failed'
@require_POST
@check_document_access_permission()
@api_error_handler
def close_session(request):
response = {'status': -1}
session = json.loads(request.POST.get('session', '{}'))
response['session'] = get_api(request, {'type': session['type']}).close_session(session=session)
response['status'] = 0
return JsonResponse(response)
def __init__(self, request, snippet):
self.request = request
self.api = _get_api(request, snippet)
request.audit = {
'operation': 'EXPORT',
'operationText': 'User %s exported to HDFS directory: %s' % (request.user.username, destination),
'allowed': True
}
elif data_format in ('search-index', 'dashboard'):
# Open the result in the Dashboard via a SQL sub-query or the Import wizard (quick vs scalable)
if is_embedded:
notebook_id = notebook['id'] or request.GET.get('editor', request.GET.get('notebook'))
if data_format == 'dashboard':
engine = notebook['type'].replace('query-', '')
response['watch_url'] = reverse('dashboard:browse', kwargs={'name': notebook_id}) + '?source=query&engine=%(engine)s' % {'engine': engine}
response['status'] = 0
else:
sample = get_api(request, snippet).fetch_result(notebook, snippet, rows=4, start_over=True)
for col in sample['meta']:
col['type'] = HiveFormat.FIELD_TYPE_TRANSLATE.get(col['type'], 'string')
response['status'] = 0
response['id'] = notebook_id
response['name'] = _get_snippet_name(notebook)
response['source_type'] = 'query'
response['target_type'] = 'index'
response['target_path'] = destination
response['sample'] = list(sample['data'])
response['columns'] = [
Field(col['name'], col['type']).to_dict() for col in sample['meta']
]
else:
notebook_id = notebook['id'] or request.GET.get('editor', request.GET.get('notebook'))
response['watch_url'] = reverse('notebook:execute_and_watch') + '?action=index_query¬ebook=' + str(notebook_id) + '&snippet=0&destination=' + destination
snippet = {'type': self.engine}
response = editor.execute(request)
if 'handle' in response:
snippet['result'] = response
if response['handle'].get('sync'):
result = response['result']
else:
timeout_sec = 20 # To move to Notebook API
sleep_interval = 0.5
curr = time.time()
end = curr + timeout_sec
api = get_api(request, snippet)
while curr <= end:
status = api.check_status(mock_notebook, snippet)
if status['status'] == 'available':
result = api.fetch_result(mock_notebook, snippet, rows=10, start_over=True)
api.close_statement(mock_notebook, snippet)
break
time.sleep(sleep_interval)
curr = time.time()
if curr > end:
try:
api.cancel_operation(snippet)
except Exception as e:
LOG.warning("Failed to cancel query: %s" % e)
api.close_statement(mock_notebook, snippet)
def fields(self, name):
snippet = {'type': self.engine}
if self.source == 'query':
snippet['query'] = self._get_query(name)
database, table = '', ''
else:
database, table = self._get_database_table_names(name)
table_metadata = get_api(MockRequest(self.user, self.cluster), snippet).autocomplete(snippet, database, table)
return {
'schema': {
'fields':
dict([(col['name'], {
'name': str(escape(col['name'])),
'type': str(col['type']).lower(),
'uniqueKey': col.get('primary_key') == 'true',
# 'dynamicBase': False,
'indexed': False,
'stored': True,
'required': col.get('primary_key') == 'true'
})
for col in table_metadata['extended_columns']]
)
@require_POST
@check_document_access_permission()
@api_error_handler
def get_sample_data(request, server=None, database=None, table=None, column=None):
response = {'status': -1}
# Passed by check_document_access_permission but unused by APIs
notebook = json.loads(request.POST.get('notebook', '{}'))
snippet = json.loads(request.POST.get('snippet', '{}'))
async = json.loads(request.POST.get('async', 'false'))
sample_data = get_api(request, snippet).get_sample_data(snippet, database, table, column, async=async)
response.update(sample_data)
response['status'] = 0
return JsonResponse(response)
@require_POST
@check_document_access_permission()
@api_error_handler
def explain(request):
response = {'status': -1}
notebook = json.loads(request.POST.get('notebook', '{}'))
snippet = json.loads(request.POST.get('snippet', '{}'))
response = get_api(request, snippet).explain(notebook, snippet)
return JsonResponse(response)