def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id): for item in links: path = urlsplit(item['url'])[2] id_type, qid = urlsplit(item['url'])[3].split('=') query_id = {'id': qid} if qid.isdigit() else {'uuid': qid} try: if path == '/hue/editor' and id_type == 'editor': doc = Document2.objects.get(**query_id) doc_type = 'query' elif path == '/hue/gist' and id_type == 'uuid': doc = _get_gist_document(**query_id) doc_type = 'gist' else: raise PopupException(_("Cannot unfurl link")) except Document2.DoesNotExist: msg = "Document with {key} does not exist".format(key=query_id) raise PopupException(_(msg)) # Permission check for Slack user to be Hue user slack_user = check_slack_user_permission(host_domain, user_id) user = get_user(channel_id, slack_user) if not slack_user['is_bot'] else doc.owner doc.can_read_or_exception(user) request = MockRequest(user=rewrite_user(user)) payload = _make_unfurl_payload(request, item['url'], id_type, doc, doc_type) try: slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload']) except Exception as e: raise PopupException(_("Cannot unfurl link"), detail=e) # Generate and upload result xlsx file only if result available if payload['file_status']: send_result_file(request, channel_id, message_ts, doc, 'xls')
def list_executed_tasks(self, app_id): sql_query = """ SELECT scheduled_executions.* FROM information_schema.scheduled_executions JOIN information_schema.scheduled_queries ON scheduled_queries.schedule_name = scheduled_executions.schedule_name where scheduled_query_id = %(scheduled_query_id)s LIMIT 100""" % { 'scheduled_query_id': app_id } job = make_notebook( name='List Hive scheduled execution', editor_type='hive', statement=sql_query, status='ready', database='default', is_task=False, ) request = MockRequest(self.user) handle = job.execute_and_wait(request, include_results=True) return [{ 'scheduled_execution_id': row[0], 'schedule_name': row[1], 'executor_query_id': row[2], 'state': row[3], 'start_time': row[4], 'end_time': row[5], 'elapsed': row[6], 'error_message': row[7], 'last_update_time': row[8], } for row in handle['result']['data']]
def handle_on_link_shared(channel_id, message_ts, links): for item in links: path = urlsplit(item['url'])[2] id_type, qid = urlsplit(item['url'])[3].split('=') try: if path == '/hue/editor' and id_type == 'editor': doc = Document2.objects.get(id=qid) doc_type = 'Query' elif path == '/hue/gist' and id_type == 'uuid' and ENABLE_GIST_PREVIEW.get(): doc = _get_gist_document(uuid=qid) doc_type = 'Gist' else: raise PopupException(_("Cannot unfurl link")) except Document2.DoesNotExist: msg = "Document with {key}={value} does not exist".format(key='uuid' if id_type == 'uuid' else 'id', value=qid) raise PopupException(_(msg)) # Mock request for query execution and fetch result user = rewrite_user(User.objects.get(username=doc.owner.username)) request = MockRequest(user=user) payload = _make_unfurl_payload(request, item['url'], id_type, doc, doc_type) try: slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload']) except Exception as e: raise PopupException(_("Cannot unfurl link"), detail=e) # Generate and upload result xlsx file only if result available if payload['file_status']: send_result_file(request, channel_id, message_ts, doc, 'xls')
def kill_query(self, query_id): kill_sql = 'KILL QUERY "%s";' % query_id job = make_notebook( name=_('Kill query %s') % query_id, editor_type='hive', statement=kill_sql, status='ready', on_success_url='assist.db.refresh', is_task=False, ) job.execute_and_wait(MockRequest(user=self.user))
def handle(self, *args, **options): if args: dialect = args[0] db_name = args[1] if len(args) > 1 else 'default' user = User.objects.get(username=pwd.getpwuid(os.getuid()).pw_name) request = None self.queries = None self.tables = None interpreter = None else: dialect = options['dialect'] db_name = options.get('db_name', 'default') interpreter = options.get('interpreter') # Only when connectors are enabled. Later will deprecate `dialect`. user = options['user'] request = options.get('request', MockRequest(user=user)) self.queries = options.get('queries') self.tables = options.get('tables') # Optional whitelist of table names tables = \ 'tables.json' if dialect not in ('hive', 'impala') else ( 'tables_transactional.json' if has_concurrency_support() else 'tables_standard.json' ) exception = None LOG.info('Installing %s samples %s in DB %s owned by user %s' % (dialect, tables, db_name, user)) self.successes = [] self.errors = [] try: sample_user = install_sample_user(user) # Documents will belong to the sample user but we run the SQL as the current user self.install_queries(sample_user, dialect, interpreter=interpreter) self.install_tables(user, dialect, db_name, tables, interpreter=interpreter, request=request) except Exception as ex: LOG.exception('Dialect %s sample install' % dialect) exception = ex if exception is not None: pretty_msg = None if "Permission denied" in str(exception): pretty_msg = _("Permission denied. Please check with your system administrator.") if pretty_msg is not None: raise PopupException(pretty_msg) else: raise exception return self.successes, self.errors
def list_tasks(self, user): sql_query = "SELECT * FROM information_schema.scheduled_queries" job = make_notebook( name='List Hive schedules', editor_type='hive', statement=sql_query, status='ready', database='default', is_task=False, ) request = MockRequest(user) handle = job.execute_and_wait(request, include_results=True) return [self._get_task(row) for row in handle['result']['data']]
def action(self, query_ids, action): message = {'actions': {}, 'status': 0} if action.get('action') == 'kill': for query_id in query_ids: action_details = {} try: request = MockRequest(user=self.user) self.kill_query(query_id, request) action_details['status'] = 0 action_details['message'] = _('kill action performed') except Exception as ex: LOG.error(ex) message['status'] = -1 action_details['status'] = -1 action_details['message'] = _('kill action failed : %s' % str(ex)) message['actions'][query_id] = action_details return message
def list_task(self, task_id): task_id = task_id.replace('schedule-hive-', '') sql_query = """ SELECT * FROM information_schema.scheduled_queries WHERE scheduled_query_id = %(scheduled_query_id)s """ % { 'scheduled_query_id': task_id } job = make_notebook( name='List Hive schedule id', editor_type='hive', statement=sql_query, status='ready', database='default', is_task=False, ) request = MockRequest(self.user) handle = job.execute_and_wait(request, include_results=True) return self._get_task(handle['result']['data'][0])
def _fetch_result_data(user, notebook=None, snippet=None, operation_id=None, rows=100, start_over=False, nulls_only=False): snippet = _get_snippet(user, notebook, snippet, operation_id) request = MockRequest(user) response = { 'result': get_api(request, snippet).fetch_result(notebook, snippet, rows, start_over) } # Materialize and HTML escape results if response['result'].get('data') and response['result'].get( 'type') == 'table' and not response['result'].get('isEscaped'): response['result']['data'] = escape_rows(response['result']['data'], nulls_only=nulls_only) response['result']['isEscaped'] = True return response
def _make_unfurl_payload(url, id_type, doc, doc_type): doc_data = json.loads(doc.data) statement = doc_data['snippets'][0][ 'statement_raw'] if id_type == 'editor' else doc_data['statement_raw'] dialect = doc_data['dialect'] if id_type == 'editor' else doc.extra created_by = doc.owner.get_full_name() or doc.owner.username # Mock request for query execution and fetch result user = rewrite_user(User.objects.get(username=doc.owner.username)) request = MockRequest(user=user) if id_type == 'editor': try: status = _check_status(request, operation_id=doc_data['uuid']) if status['query_status']['status'] == 'available': fetch_result = query_result(request, json.loads(doc.data)) unfurl_result = _make_result_table(fetch_result) if isinstance( fetch_result, dict) else fetch_result except: unfurl_result = 'Query result has expired or could not be found' else: unfurl_result = 'Result is not available for Gist' payload = { url: { "color": "#025BA6", "blocks": [{ "type": "section", "text": { "type": "mrkdwn", "text": "\n*<{}|Hue - SQL {}>*".format(url, doc_type) } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Statement:*\n```{}```".format( statement if len(statement) < 150 else ( statement[:150] + '...')) } }, { "type": "section", "fields": [{ "type": "mrkdwn", "text": "*Dialect:*\n{}".format(dialect) }, { "type": "mrkdwn", "text": "*Created by:*\n{}".format(created_by) }] }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Query result:*\n```{}```".format(unfurl_result), } }] } } return payload