repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
apache/incubator-superset
superset/utils/cache.py
memoized_func
def memoized_func(key=view_cache_key, attribute_in_key=None): """Use this decorator to cache functions that have predefined first arg. enable_cache is treated as True by default, except enable_cache = False is passed to the decorated function. force means whether to force refresh the cache and is treated as False by default, except force = True is passed to the decorated function. timeout of cache is set to 600 seconds by default, except cache_timeout = {timeout in seconds} is passed to the decorated function. memoized_func uses simple_cache and stored the data in memory. Key is a callable function that takes function arguments and returns the caching key. """ def wrap(f): if tables_cache: def wrapped_f(self, *args, **kwargs): if not kwargs.get('cache', True): return f(self, *args, **kwargs) if attribute_in_key: cache_key = key(*args, **kwargs).format( getattr(self, attribute_in_key)) else: cache_key = key(*args, **kwargs) o = tables_cache.get(cache_key) if not kwargs.get('force') and o is not None: return o o = f(self, *args, **kwargs) tables_cache.set(cache_key, o, timeout=kwargs.get('cache_timeout')) return o else: # noop def wrapped_f(self, *args, **kwargs): return f(self, *args, **kwargs) return wrapped_f return wrap
python
def memoized_func(key=view_cache_key, attribute_in_key=None): """Use this decorator to cache functions that have predefined first arg. enable_cache is treated as True by default, except enable_cache = False is passed to the decorated function. force means whether to force refresh the cache and is treated as False by default, except force = True is passed to the decorated function. timeout of cache is set to 600 seconds by default, except cache_timeout = {timeout in seconds} is passed to the decorated function. memoized_func uses simple_cache and stored the data in memory. Key is a callable function that takes function arguments and returns the caching key. """ def wrap(f): if tables_cache: def wrapped_f(self, *args, **kwargs): if not kwargs.get('cache', True): return f(self, *args, **kwargs) if attribute_in_key: cache_key = key(*args, **kwargs).format( getattr(self, attribute_in_key)) else: cache_key = key(*args, **kwargs) o = tables_cache.get(cache_key) if not kwargs.get('force') and o is not None: return o o = f(self, *args, **kwargs) tables_cache.set(cache_key, o, timeout=kwargs.get('cache_timeout')) return o else: # noop def wrapped_f(self, *args, **kwargs): return f(self, *args, **kwargs) return wrapped_f return wrap
[ "def", "memoized_func", "(", "key", "=", "view_cache_key", ",", "attribute_in_key", "=", "None", ")", ":", "def", "wrap", "(", "f", ")", ":", "if", "tables_cache", ":", "def", "wrapped_f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "not", "kwargs", ".", "get", "(", "'cache'", ",", "True", ")", ":", "return", "f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "if", "attribute_in_key", ":", "cache_key", "=", "key", "(", "*", "args", ",", "*", "*", "kwargs", ")", ".", "format", "(", "getattr", "(", "self", ",", "attribute_in_key", ")", ")", "else", ":", "cache_key", "=", "key", "(", "*", "args", ",", "*", "*", "kwargs", ")", "o", "=", "tables_cache", ".", "get", "(", "cache_key", ")", "if", "not", "kwargs", ".", "get", "(", "'force'", ")", "and", "o", "is", "not", "None", ":", "return", "o", "o", "=", "f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "tables_cache", ".", "set", "(", "cache_key", ",", "o", ",", "timeout", "=", "kwargs", ".", "get", "(", "'cache_timeout'", ")", ")", "return", "o", "else", ":", "# noop", "def", "wrapped_f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "return", "f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "wrapped_f", "return", "wrap" ]
Use this decorator to cache functions that have predefined first arg. enable_cache is treated as True by default, except enable_cache = False is passed to the decorated function. force means whether to force refresh the cache and is treated as False by default, except force = True is passed to the decorated function. timeout of cache is set to 600 seconds by default, except cache_timeout = {timeout in seconds} is passed to the decorated function. memoized_func uses simple_cache and stored the data in memory. Key is a callable function that takes function arguments and returns the caching key.
[ "Use", "this", "decorator", "to", "cache", "functions", "that", "have", "predefined", "first", "arg", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/cache.py#L28-L67
train
apache/incubator-superset
superset/models/sql_lab.py
Query.name
def name(self): """Name property""" ts = datetime.now().isoformat() ts = ts.replace('-', '').replace(':', '').split('.')[0] tab = (self.tab_name.replace(' ', '_').lower() if self.tab_name else 'notab') tab = re.sub(r'\W+', '', tab) return f'sqllab_{tab}_{ts}'
python
def name(self): """Name property""" ts = datetime.now().isoformat() ts = ts.replace('-', '').replace(':', '').split('.')[0] tab = (self.tab_name.replace(' ', '_').lower() if self.tab_name else 'notab') tab = re.sub(r'\W+', '', tab) return f'sqllab_{tab}_{ts}'
[ "def", "name", "(", "self", ")", ":", "ts", "=", "datetime", ".", "now", "(", ")", ".", "isoformat", "(", ")", "ts", "=", "ts", ".", "replace", "(", "'-'", ",", "''", ")", ".", "replace", "(", "':'", ",", "''", ")", ".", "split", "(", "'.'", ")", "[", "0", "]", "tab", "=", "(", "self", ".", "tab_name", ".", "replace", "(", "' '", ",", "'_'", ")", ".", "lower", "(", ")", "if", "self", ".", "tab_name", "else", "'notab'", ")", "tab", "=", "re", ".", "sub", "(", "r'\\W+'", ",", "''", ",", "tab", ")", "return", "f'sqllab_{tab}_{ts}'" ]
Name property
[ "Name", "property" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/models/sql_lab.py#L132-L139
train
apache/incubator-superset
superset/views/core.py
check_datasource_perms
def check_datasource_perms(self, datasource_type=None, datasource_id=None): """ Check if user can access a cached response from explore_json. This function takes `self` since it must have the same signature as the the decorated method. """ form_data = get_form_data()[0] datasource_id, datasource_type = get_datasource_info( datasource_id, datasource_type, form_data) viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=False, ) security_manager.assert_datasource_permission(viz_obj.datasource)
python
def check_datasource_perms(self, datasource_type=None, datasource_id=None): """ Check if user can access a cached response from explore_json. This function takes `self` since it must have the same signature as the the decorated method. """ form_data = get_form_data()[0] datasource_id, datasource_type = get_datasource_info( datasource_id, datasource_type, form_data) viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=False, ) security_manager.assert_datasource_permission(viz_obj.datasource)
[ "def", "check_datasource_perms", "(", "self", ",", "datasource_type", "=", "None", ",", "datasource_id", "=", "None", ")", ":", "form_data", "=", "get_form_data", "(", ")", "[", "0", "]", "datasource_id", ",", "datasource_type", "=", "get_datasource_info", "(", "datasource_id", ",", "datasource_type", ",", "form_data", ")", "viz_obj", "=", "get_viz", "(", "datasource_type", "=", "datasource_type", ",", "datasource_id", "=", "datasource_id", ",", "form_data", "=", "form_data", ",", "force", "=", "False", ",", ")", "security_manager", ".", "assert_datasource_permission", "(", "viz_obj", ".", "datasource", ")" ]
Check if user can access a cached response from explore_json. This function takes `self` since it must have the same signature as the the decorated method.
[ "Check", "if", "user", "can", "access", "a", "cached", "response", "from", "explore_json", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L106-L123
train
apache/incubator-superset
superset/views/core.py
check_slice_perms
def check_slice_perms(self, slice_id): """ Check if user can access a cached response from slice_json. This function takes `self` since it must have the same signature as the the decorated method. """ form_data, slc = get_form_data(slice_id, use_slice_data=True) datasource_type = slc.datasource.type datasource_id = slc.datasource.id viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=False, ) security_manager.assert_datasource_permission(viz_obj.datasource)
python
def check_slice_perms(self, slice_id): """ Check if user can access a cached response from slice_json. This function takes `self` since it must have the same signature as the the decorated method. """ form_data, slc = get_form_data(slice_id, use_slice_data=True) datasource_type = slc.datasource.type datasource_id = slc.datasource.id viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=False, ) security_manager.assert_datasource_permission(viz_obj.datasource)
[ "def", "check_slice_perms", "(", "self", ",", "slice_id", ")", ":", "form_data", ",", "slc", "=", "get_form_data", "(", "slice_id", ",", "use_slice_data", "=", "True", ")", "datasource_type", "=", "slc", ".", "datasource", ".", "type", "datasource_id", "=", "slc", ".", "datasource", ".", "id", "viz_obj", "=", "get_viz", "(", "datasource_type", "=", "datasource_type", ",", "datasource_id", "=", "datasource_id", ",", "form_data", "=", "form_data", ",", "force", "=", "False", ",", ")", "security_manager", ".", "assert_datasource_permission", "(", "viz_obj", ".", "datasource", ")" ]
Check if user can access a cached response from slice_json. This function takes `self` since it must have the same signature as the the decorated method.
[ "Check", "if", "user", "can", "access", "a", "cached", "response", "from", "slice_json", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L126-L143
train
apache/incubator-superset
superset/views/core.py
apply_caching
def apply_caching(response): """Applies the configuration's http headers to all responses""" for k, v in config.get('HTTP_HEADERS').items(): response.headers[k] = v return response
python
def apply_caching(response): """Applies the configuration's http headers to all responses""" for k, v in config.get('HTTP_HEADERS').items(): response.headers[k] = v return response
[ "def", "apply_caching", "(", "response", ")", ":", "for", "k", ",", "v", "in", "config", ".", "get", "(", "'HTTP_HEADERS'", ")", ".", "items", "(", ")", ":", "response", ".", "headers", "[", "k", "]", "=", "v", "return", "response" ]
Applies the configuration's http headers to all responses
[ "Applies", "the", "configuration", "s", "http", "headers", "to", "all", "responses" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L3017-L3021
train
apache/incubator-superset
superset/views/core.py
Superset.override_role_permissions
def override_role_permissions(self): """Updates the role with the give datasource permissions. Permissions not in the request will be revoked. This endpoint should be available to admins only. Expects JSON in the format: { 'role_name': '{role_name}', 'database': [{ 'datasource_type': '{table|druid}', 'name': '{database_name}', 'schema': [{ 'name': '{schema_name}', 'datasources': ['{datasource name}, {datasource name}'] }] }] } """ data = request.get_json(force=True) role_name = data['role_name'] databases = data['database'] db_ds_names = set() for dbs in databases: for schema in dbs['schema']: for ds_name in schema['datasources']: fullname = utils.get_datasource_full_name( dbs['name'], ds_name, schema=schema['name']) db_ds_names.add(fullname) existing_datasources = ConnectorRegistry.get_all_datasources(db.session) datasources = [ d for d in existing_datasources if d.full_name in db_ds_names] role = security_manager.find_role(role_name) # remove all permissions role.permissions = [] # grant permissions to the list of datasources granted_perms = [] for datasource in datasources: view_menu_perm = security_manager.find_permission_view_menu( view_menu_name=datasource.perm, permission_name='datasource_access') # prevent creating empty permissions if view_menu_perm and view_menu_perm.view_menu: role.permissions.append(view_menu_perm) granted_perms.append(view_menu_perm.view_menu.name) db.session.commit() return self.json_response({ 'granted': granted_perms, 'requested': list(db_ds_names), }, status=201)
python
def override_role_permissions(self): """Updates the role with the give datasource permissions. Permissions not in the request will be revoked. This endpoint should be available to admins only. Expects JSON in the format: { 'role_name': '{role_name}', 'database': [{ 'datasource_type': '{table|druid}', 'name': '{database_name}', 'schema': [{ 'name': '{schema_name}', 'datasources': ['{datasource name}, {datasource name}'] }] }] } """ data = request.get_json(force=True) role_name = data['role_name'] databases = data['database'] db_ds_names = set() for dbs in databases: for schema in dbs['schema']: for ds_name in schema['datasources']: fullname = utils.get_datasource_full_name( dbs['name'], ds_name, schema=schema['name']) db_ds_names.add(fullname) existing_datasources = ConnectorRegistry.get_all_datasources(db.session) datasources = [ d for d in existing_datasources if d.full_name in db_ds_names] role = security_manager.find_role(role_name) # remove all permissions role.permissions = [] # grant permissions to the list of datasources granted_perms = [] for datasource in datasources: view_menu_perm = security_manager.find_permission_view_menu( view_menu_name=datasource.perm, permission_name='datasource_access') # prevent creating empty permissions if view_menu_perm and view_menu_perm.view_menu: role.permissions.append(view_menu_perm) granted_perms.append(view_menu_perm.view_menu.name) db.session.commit() return self.json_response({ 'granted': granted_perms, 'requested': list(db_ds_names), }, status=201)
[ "def", "override_role_permissions", "(", "self", ")", ":", "data", "=", "request", ".", "get_json", "(", "force", "=", "True", ")", "role_name", "=", "data", "[", "'role_name'", "]", "databases", "=", "data", "[", "'database'", "]", "db_ds_names", "=", "set", "(", ")", "for", "dbs", "in", "databases", ":", "for", "schema", "in", "dbs", "[", "'schema'", "]", ":", "for", "ds_name", "in", "schema", "[", "'datasources'", "]", ":", "fullname", "=", "utils", ".", "get_datasource_full_name", "(", "dbs", "[", "'name'", "]", ",", "ds_name", ",", "schema", "=", "schema", "[", "'name'", "]", ")", "db_ds_names", ".", "add", "(", "fullname", ")", "existing_datasources", "=", "ConnectorRegistry", ".", "get_all_datasources", "(", "db", ".", "session", ")", "datasources", "=", "[", "d", "for", "d", "in", "existing_datasources", "if", "d", ".", "full_name", "in", "db_ds_names", "]", "role", "=", "security_manager", ".", "find_role", "(", "role_name", ")", "# remove all permissions", "role", ".", "permissions", "=", "[", "]", "# grant permissions to the list of datasources", "granted_perms", "=", "[", "]", "for", "datasource", "in", "datasources", ":", "view_menu_perm", "=", "security_manager", ".", "find_permission_view_menu", "(", "view_menu_name", "=", "datasource", ".", "perm", ",", "permission_name", "=", "'datasource_access'", ")", "# prevent creating empty permissions", "if", "view_menu_perm", "and", "view_menu_perm", ".", "view_menu", ":", "role", ".", "permissions", ".", "append", "(", "view_menu_perm", ")", "granted_perms", ".", "append", "(", "view_menu_perm", ".", "view_menu", ".", "name", ")", "db", ".", "session", ".", "commit", "(", ")", "return", "self", ".", "json_response", "(", "{", "'granted'", ":", "granted_perms", ",", "'requested'", ":", "list", "(", "db_ds_names", ")", ",", "}", ",", "status", "=", "201", ")" ]
Updates the role with the give datasource permissions. Permissions not in the request will be revoked. This endpoint should be available to admins only. Expects JSON in the format: { 'role_name': '{role_name}', 'database': [{ 'datasource_type': '{table|druid}', 'name': '{database_name}', 'schema': [{ 'name': '{schema_name}', 'datasources': ['{datasource name}, {datasource name}'] }] }] }
[ "Updates", "the", "role", "with", "the", "give", "datasource", "permissions", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L862-L911
train
apache/incubator-superset
superset/views/core.py
Superset.explore_json
def explore_json(self, datasource_type=None, datasource_id=None): """Serves all request that GET or POST form_data This endpoint evolved to be the entry point of many different requests that GETs or POSTs a form_data. `self.generate_json` receives this input and returns different payloads based on the request args in the first block TODO: break into one endpoint for each return shape""" csv = request.args.get('csv') == 'true' query = request.args.get('query') == 'true' results = request.args.get('results') == 'true' samples = request.args.get('samples') == 'true' force = request.args.get('force') == 'true' form_data = get_form_data()[0] datasource_id, datasource_type = get_datasource_info( datasource_id, datasource_type, form_data) viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=force, ) return self.generate_json( viz_obj, csv=csv, query=query, results=results, samples=samples, )
python
def explore_json(self, datasource_type=None, datasource_id=None): """Serves all request that GET or POST form_data This endpoint evolved to be the entry point of many different requests that GETs or POSTs a form_data. `self.generate_json` receives this input and returns different payloads based on the request args in the first block TODO: break into one endpoint for each return shape""" csv = request.args.get('csv') == 'true' query = request.args.get('query') == 'true' results = request.args.get('results') == 'true' samples = request.args.get('samples') == 'true' force = request.args.get('force') == 'true' form_data = get_form_data()[0] datasource_id, datasource_type = get_datasource_info( datasource_id, datasource_type, form_data) viz_obj = get_viz( datasource_type=datasource_type, datasource_id=datasource_id, form_data=form_data, force=force, ) return self.generate_json( viz_obj, csv=csv, query=query, results=results, samples=samples, )
[ "def", "explore_json", "(", "self", ",", "datasource_type", "=", "None", ",", "datasource_id", "=", "None", ")", ":", "csv", "=", "request", ".", "args", ".", "get", "(", "'csv'", ")", "==", "'true'", "query", "=", "request", ".", "args", ".", "get", "(", "'query'", ")", "==", "'true'", "results", "=", "request", ".", "args", ".", "get", "(", "'results'", ")", "==", "'true'", "samples", "=", "request", ".", "args", ".", "get", "(", "'samples'", ")", "==", "'true'", "force", "=", "request", ".", "args", ".", "get", "(", "'force'", ")", "==", "'true'", "form_data", "=", "get_form_data", "(", ")", "[", "0", "]", "datasource_id", ",", "datasource_type", "=", "get_datasource_info", "(", "datasource_id", ",", "datasource_type", ",", "form_data", ")", "viz_obj", "=", "get_viz", "(", "datasource_type", "=", "datasource_type", ",", "datasource_id", "=", "datasource_id", ",", "form_data", "=", "form_data", ",", "force", "=", "force", ",", ")", "return", "self", ".", "generate_json", "(", "viz_obj", ",", "csv", "=", "csv", ",", "query", "=", "query", ",", "results", "=", "results", ",", "samples", "=", "samples", ",", ")" ]
Serves all request that GET or POST form_data This endpoint evolved to be the entry point of many different requests that GETs or POSTs a form_data. `self.generate_json` receives this input and returns different payloads based on the request args in the first block TODO: break into one endpoint for each return shape
[ "Serves", "all", "request", "that", "GET", "or", "POST", "form_data" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1233-L1265
train
apache/incubator-superset
superset/views/core.py
Superset.import_dashboards
def import_dashboards(self): """Overrides the dashboards using json instances from the file.""" f = request.files.get('file') if request.method == 'POST' and f: dashboard_import_export.import_dashboards(db.session, f.stream) return redirect('/dashboard/list/') return self.render_template('superset/import_dashboards.html')
python
def import_dashboards(self): """Overrides the dashboards using json instances from the file.""" f = request.files.get('file') if request.method == 'POST' and f: dashboard_import_export.import_dashboards(db.session, f.stream) return redirect('/dashboard/list/') return self.render_template('superset/import_dashboards.html')
[ "def", "import_dashboards", "(", "self", ")", ":", "f", "=", "request", ".", "files", ".", "get", "(", "'file'", ")", "if", "request", ".", "method", "==", "'POST'", "and", "f", ":", "dashboard_import_export", ".", "import_dashboards", "(", "db", ".", "session", ",", "f", ".", "stream", ")", "return", "redirect", "(", "'/dashboard/list/'", ")", "return", "self", ".", "render_template", "(", "'superset/import_dashboards.html'", ")" ]
Overrides the dashboards using json instances from the file.
[ "Overrides", "the", "dashboards", "using", "json", "instances", "from", "the", "file", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1270-L1276
train
apache/incubator-superset
superset/views/core.py
Superset.explorev2
def explorev2(self, datasource_type, datasource_id): """Deprecated endpoint, here for backward compatibility of urls""" return redirect(url_for( 'Superset.explore', datasource_type=datasource_type, datasource_id=datasource_id, **request.args))
python
def explorev2(self, datasource_type, datasource_id): """Deprecated endpoint, here for backward compatibility of urls""" return redirect(url_for( 'Superset.explore', datasource_type=datasource_type, datasource_id=datasource_id, **request.args))
[ "def", "explorev2", "(", "self", ",", "datasource_type", ",", "datasource_id", ")", ":", "return", "redirect", "(", "url_for", "(", "'Superset.explore'", ",", "datasource_type", "=", "datasource_type", ",", "datasource_id", "=", "datasource_id", ",", "*", "*", "request", ".", "args", ")", ")" ]
Deprecated endpoint, here for backward compatibility of urls
[ "Deprecated", "endpoint", "here", "for", "backward", "compatibility", "of", "urls" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1281-L1287
train
apache/incubator-superset
superset/views/core.py
Superset.filter
def filter(self, datasource_type, datasource_id, column): """ Endpoint to retrieve values for specified column. :param datasource_type: Type of datasource e.g. table :param datasource_id: Datasource id :param column: Column name to retrieve values for :return: """ # TODO: Cache endpoint by user, datasource and column datasource = ConnectorRegistry.get_datasource( datasource_type, datasource_id, db.session) if not datasource: return json_error_response(DATASOURCE_MISSING_ERR) security_manager.assert_datasource_permission(datasource) payload = json.dumps( datasource.values_for_column( column, config.get('FILTER_SELECT_ROW_LIMIT', 10000), ), default=utils.json_int_dttm_ser) return json_success(payload)
python
def filter(self, datasource_type, datasource_id, column): """ Endpoint to retrieve values for specified column. :param datasource_type: Type of datasource e.g. table :param datasource_id: Datasource id :param column: Column name to retrieve values for :return: """ # TODO: Cache endpoint by user, datasource and column datasource = ConnectorRegistry.get_datasource( datasource_type, datasource_id, db.session) if not datasource: return json_error_response(DATASOURCE_MISSING_ERR) security_manager.assert_datasource_permission(datasource) payload = json.dumps( datasource.values_for_column( column, config.get('FILTER_SELECT_ROW_LIMIT', 10000), ), default=utils.json_int_dttm_ser) return json_success(payload)
[ "def", "filter", "(", "self", ",", "datasource_type", ",", "datasource_id", ",", "column", ")", ":", "# TODO: Cache endpoint by user, datasource and column", "datasource", "=", "ConnectorRegistry", ".", "get_datasource", "(", "datasource_type", ",", "datasource_id", ",", "db", ".", "session", ")", "if", "not", "datasource", ":", "return", "json_error_response", "(", "DATASOURCE_MISSING_ERR", ")", "security_manager", ".", "assert_datasource_permission", "(", "datasource", ")", "payload", "=", "json", ".", "dumps", "(", "datasource", ".", "values_for_column", "(", "column", ",", "config", ".", "get", "(", "'FILTER_SELECT_ROW_LIMIT'", ",", "10000", ")", ",", ")", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", "return", "json_success", "(", "payload", ")" ]
Endpoint to retrieve values for specified column. :param datasource_type: Type of datasource e.g. table :param datasource_id: Datasource id :param column: Column name to retrieve values for :return:
[ "Endpoint", "to", "retrieve", "values", "for", "specified", "column", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1394-L1415
train
apache/incubator-superset
superset/views/core.py
Superset.save_or_overwrite_slice
def save_or_overwrite_slice( self, args, slc, slice_add_perm, slice_overwrite_perm, slice_download_perm, datasource_id, datasource_type, datasource_name): """Save or overwrite a slice""" slice_name = args.get('slice_name') action = args.get('action') form_data = get_form_data()[0] if action in ('saveas'): if 'slice_id' in form_data: form_data.pop('slice_id') # don't save old slice_id slc = models.Slice(owners=[g.user] if g.user else []) slc.params = json.dumps(form_data, indent=2, sort_keys=True) slc.datasource_name = datasource_name slc.viz_type = form_data['viz_type'] slc.datasource_type = datasource_type slc.datasource_id = datasource_id slc.slice_name = slice_name if action in ('saveas') and slice_add_perm: self.save_slice(slc) elif action == 'overwrite' and slice_overwrite_perm: self.overwrite_slice(slc) # Adding slice to a dashboard if requested dash = None if request.args.get('add_to_dash') == 'existing': dash = ( db.session.query(models.Dashboard) .filter_by(id=int(request.args.get('save_to_dashboard_id'))) .one() ) # check edit dashboard permissions dash_overwrite_perm = check_ownership(dash, raise_if_false=False) if not dash_overwrite_perm: return json_error_response( _('You don\'t have the rights to ') + _('alter this ') + _('dashboard'), status=400) flash( _('Chart [{}] was added to dashboard [{}]').format( slc.slice_name, dash.dashboard_title), 'info') elif request.args.get('add_to_dash') == 'new': # check create dashboard permissions dash_add_perm = security_manager.can_access('can_add', 'DashboardModelView') if not dash_add_perm: return json_error_response( _('You don\'t have the rights to ') + _('create a ') + _('dashboard'), status=400) dash = models.Dashboard( dashboard_title=request.args.get('new_dashboard_name'), owners=[g.user] if g.user else []) flash( _('Dashboard [{}] just got created and chart [{}] was added ' 'to it').format( dash.dashboard_title, slc.slice_name), 'info') if dash and slc not in dash.slices: dash.slices.append(slc) db.session.commit() response = { 'can_add': slice_add_perm, 'can_download': slice_download_perm, 'can_overwrite': is_owner(slc, g.user), 'form_data': slc.form_data, 'slice': slc.data, 'dashboard_id': dash.id if dash else None, } if request.args.get('goto_dash') == 'true': response.update({'dashboard': dash.url}) return json_success(json.dumps(response))
python
def save_or_overwrite_slice( self, args, slc, slice_add_perm, slice_overwrite_perm, slice_download_perm, datasource_id, datasource_type, datasource_name): """Save or overwrite a slice""" slice_name = args.get('slice_name') action = args.get('action') form_data = get_form_data()[0] if action in ('saveas'): if 'slice_id' in form_data: form_data.pop('slice_id') # don't save old slice_id slc = models.Slice(owners=[g.user] if g.user else []) slc.params = json.dumps(form_data, indent=2, sort_keys=True) slc.datasource_name = datasource_name slc.viz_type = form_data['viz_type'] slc.datasource_type = datasource_type slc.datasource_id = datasource_id slc.slice_name = slice_name if action in ('saveas') and slice_add_perm: self.save_slice(slc) elif action == 'overwrite' and slice_overwrite_perm: self.overwrite_slice(slc) # Adding slice to a dashboard if requested dash = None if request.args.get('add_to_dash') == 'existing': dash = ( db.session.query(models.Dashboard) .filter_by(id=int(request.args.get('save_to_dashboard_id'))) .one() ) # check edit dashboard permissions dash_overwrite_perm = check_ownership(dash, raise_if_false=False) if not dash_overwrite_perm: return json_error_response( _('You don\'t have the rights to ') + _('alter this ') + _('dashboard'), status=400) flash( _('Chart [{}] was added to dashboard [{}]').format( slc.slice_name, dash.dashboard_title), 'info') elif request.args.get('add_to_dash') == 'new': # check create dashboard permissions dash_add_perm = security_manager.can_access('can_add', 'DashboardModelView') if not dash_add_perm: return json_error_response( _('You don\'t have the rights to ') + _('create a ') + _('dashboard'), status=400) dash = models.Dashboard( dashboard_title=request.args.get('new_dashboard_name'), owners=[g.user] if g.user else []) flash( _('Dashboard [{}] just got created and chart [{}] was added ' 'to it').format( dash.dashboard_title, slc.slice_name), 'info') if dash and slc not in dash.slices: dash.slices.append(slc) db.session.commit() response = { 'can_add': slice_add_perm, 'can_download': slice_download_perm, 'can_overwrite': is_owner(slc, g.user), 'form_data': slc.form_data, 'slice': slc.data, 'dashboard_id': dash.id if dash else None, } if request.args.get('goto_dash') == 'true': response.update({'dashboard': dash.url}) return json_success(json.dumps(response))
[ "def", "save_or_overwrite_slice", "(", "self", ",", "args", ",", "slc", ",", "slice_add_perm", ",", "slice_overwrite_perm", ",", "slice_download_perm", ",", "datasource_id", ",", "datasource_type", ",", "datasource_name", ")", ":", "slice_name", "=", "args", ".", "get", "(", "'slice_name'", ")", "action", "=", "args", ".", "get", "(", "'action'", ")", "form_data", "=", "get_form_data", "(", ")", "[", "0", "]", "if", "action", "in", "(", "'saveas'", ")", ":", "if", "'slice_id'", "in", "form_data", ":", "form_data", ".", "pop", "(", "'slice_id'", ")", "# don't save old slice_id", "slc", "=", "models", ".", "Slice", "(", "owners", "=", "[", "g", ".", "user", "]", "if", "g", ".", "user", "else", "[", "]", ")", "slc", ".", "params", "=", "json", ".", "dumps", "(", "form_data", ",", "indent", "=", "2", ",", "sort_keys", "=", "True", ")", "slc", ".", "datasource_name", "=", "datasource_name", "slc", ".", "viz_type", "=", "form_data", "[", "'viz_type'", "]", "slc", ".", "datasource_type", "=", "datasource_type", "slc", ".", "datasource_id", "=", "datasource_id", "slc", ".", "slice_name", "=", "slice_name", "if", "action", "in", "(", "'saveas'", ")", "and", "slice_add_perm", ":", "self", ".", "save_slice", "(", "slc", ")", "elif", "action", "==", "'overwrite'", "and", "slice_overwrite_perm", ":", "self", ".", "overwrite_slice", "(", "slc", ")", "# Adding slice to a dashboard if requested", "dash", "=", "None", "if", "request", ".", "args", ".", "get", "(", "'add_to_dash'", ")", "==", "'existing'", ":", "dash", "=", "(", "db", ".", "session", ".", "query", "(", "models", ".", "Dashboard", ")", ".", "filter_by", "(", "id", "=", "int", "(", "request", ".", "args", ".", "get", "(", "'save_to_dashboard_id'", ")", ")", ")", ".", "one", "(", ")", ")", "# check edit dashboard permissions", "dash_overwrite_perm", "=", "check_ownership", "(", "dash", ",", "raise_if_false", "=", "False", ")", "if", "not", "dash_overwrite_perm", ":", "return", "json_error_response", "(", "_", "(", "'You don\\'t have the rights to '", ")", "+", "_", "(", "'alter this '", ")", "+", "_", "(", "'dashboard'", ")", ",", "status", "=", "400", ")", "flash", "(", "_", "(", "'Chart [{}] was added to dashboard [{}]'", ")", ".", "format", "(", "slc", ".", "slice_name", ",", "dash", ".", "dashboard_title", ")", ",", "'info'", ")", "elif", "request", ".", "args", ".", "get", "(", "'add_to_dash'", ")", "==", "'new'", ":", "# check create dashboard permissions", "dash_add_perm", "=", "security_manager", ".", "can_access", "(", "'can_add'", ",", "'DashboardModelView'", ")", "if", "not", "dash_add_perm", ":", "return", "json_error_response", "(", "_", "(", "'You don\\'t have the rights to '", ")", "+", "_", "(", "'create a '", ")", "+", "_", "(", "'dashboard'", ")", ",", "status", "=", "400", ")", "dash", "=", "models", ".", "Dashboard", "(", "dashboard_title", "=", "request", ".", "args", ".", "get", "(", "'new_dashboard_name'", ")", ",", "owners", "=", "[", "g", ".", "user", "]", "if", "g", ".", "user", "else", "[", "]", ")", "flash", "(", "_", "(", "'Dashboard [{}] just got created and chart [{}] was added '", "'to it'", ")", ".", "format", "(", "dash", ".", "dashboard_title", ",", "slc", ".", "slice_name", ")", ",", "'info'", ")", "if", "dash", "and", "slc", "not", "in", "dash", ".", "slices", ":", "dash", ".", "slices", ".", "append", "(", "slc", ")", "db", ".", "session", ".", "commit", "(", ")", "response", "=", "{", "'can_add'", ":", "slice_add_perm", ",", "'can_download'", ":", "slice_download_perm", ",", "'can_overwrite'", ":", "is_owner", "(", "slc", ",", "g", ".", "user", ")", ",", "'form_data'", ":", "slc", ".", "form_data", ",", "'slice'", ":", "slc", ".", "data", ",", "'dashboard_id'", ":", "dash", ".", "id", "if", "dash", "else", "None", ",", "}", "if", "request", ".", "args", ".", "get", "(", "'goto_dash'", ")", "==", "'true'", ":", "response", ".", "update", "(", "{", "'dashboard'", ":", "dash", ".", "url", "}", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "response", ")", ")" ]
Save or overwrite a slice
[ "Save", "or", "overwrite", "a", "slice" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1417-L1498
train
apache/incubator-superset
superset/views/core.py
Superset.checkbox
def checkbox(self, model_view, id_, attr, value): """endpoint for checking/unchecking any boolean in a sqla model""" modelview_to_model = { '{}ColumnInlineView'.format(name.capitalize()): source.column_class for name, source in ConnectorRegistry.sources.items() } model = modelview_to_model[model_view] col = db.session.query(model).filter_by(id=id_).first() checked = value == 'true' if col: setattr(col, attr, checked) if checked: metrics = col.get_metrics().values() col.datasource.add_missing_metrics(metrics) db.session.commit() return json_success('OK')
python
def checkbox(self, model_view, id_, attr, value): """endpoint for checking/unchecking any boolean in a sqla model""" modelview_to_model = { '{}ColumnInlineView'.format(name.capitalize()): source.column_class for name, source in ConnectorRegistry.sources.items() } model = modelview_to_model[model_view] col = db.session.query(model).filter_by(id=id_).first() checked = value == 'true' if col: setattr(col, attr, checked) if checked: metrics = col.get_metrics().values() col.datasource.add_missing_metrics(metrics) db.session.commit() return json_success('OK')
[ "def", "checkbox", "(", "self", ",", "model_view", ",", "id_", ",", "attr", ",", "value", ")", ":", "modelview_to_model", "=", "{", "'{}ColumnInlineView'", ".", "format", "(", "name", ".", "capitalize", "(", ")", ")", ":", "source", ".", "column_class", "for", "name", ",", "source", "in", "ConnectorRegistry", ".", "sources", ".", "items", "(", ")", "}", "model", "=", "modelview_to_model", "[", "model_view", "]", "col", "=", "db", ".", "session", ".", "query", "(", "model", ")", ".", "filter_by", "(", "id", "=", "id_", ")", ".", "first", "(", ")", "checked", "=", "value", "==", "'true'", "if", "col", ":", "setattr", "(", "col", ",", "attr", ",", "checked", ")", "if", "checked", ":", "metrics", "=", "col", ".", "get_metrics", "(", ")", ".", "values", "(", ")", "col", ".", "datasource", ".", "add_missing_metrics", "(", "metrics", ")", "db", ".", "session", ".", "commit", "(", ")", "return", "json_success", "(", "'OK'", ")" ]
endpoint for checking/unchecking any boolean in a sqla model
[ "endpoint", "for", "checking", "/", "unchecking", "any", "boolean", "in", "a", "sqla", "model" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1517-L1532
train
apache/incubator-superset
superset/views/core.py
Superset.tables
def tables(self, db_id, schema, substr, force_refresh='false'): """Endpoint to fetch the list of tables for given database""" db_id = int(db_id) force_refresh = force_refresh.lower() == 'true' schema = utils.js_string_to_python(schema) substr = utils.js_string_to_python(substr) database = db.session.query(models.Database).filter_by(id=db_id).one() if schema: table_names = database.all_table_names_in_schema( schema=schema, force=force_refresh, cache=database.table_cache_enabled, cache_timeout=database.table_cache_timeout) view_names = database.all_view_names_in_schema( schema=schema, force=force_refresh, cache=database.table_cache_enabled, cache_timeout=database.table_cache_timeout) else: table_names = database.all_table_names_in_database( cache=True, force=False, cache_timeout=24 * 60 * 60) view_names = database.all_view_names_in_database( cache=True, force=False, cache_timeout=24 * 60 * 60) table_names = security_manager.accessible_by_user(database, table_names, schema) view_names = security_manager.accessible_by_user(database, view_names, schema) if substr: table_names = [tn for tn in table_names if substr in tn] view_names = [vn for vn in view_names if substr in vn] if not schema and database.default_schemas: def get_schema(tbl_or_view_name): return tbl_or_view_name.split('.')[0] if '.' in tbl_or_view_name else None user_schema = g.user.email.split('@')[0] valid_schemas = set(database.default_schemas + [user_schema]) table_names = [tn for tn in table_names if get_schema(tn) in valid_schemas] view_names = [vn for vn in view_names if get_schema(vn) in valid_schemas] max_items = config.get('MAX_TABLE_NAMES') or len(table_names) total_items = len(table_names) + len(view_names) max_tables = len(table_names) max_views = len(view_names) if total_items and substr: max_tables = max_items * len(table_names) // total_items max_views = max_items * len(view_names) // total_items table_options = [{'value': tn, 'label': tn} for tn in table_names[:max_tables]] table_options.extend([{'value': vn, 'label': '[view] {}'.format(vn)} for vn in view_names[:max_views]]) payload = { 'tableLength': len(table_names) + len(view_names), 'options': table_options, } return json_success(json.dumps(payload))
python
def tables(self, db_id, schema, substr, force_refresh='false'): """Endpoint to fetch the list of tables for given database""" db_id = int(db_id) force_refresh = force_refresh.lower() == 'true' schema = utils.js_string_to_python(schema) substr = utils.js_string_to_python(substr) database = db.session.query(models.Database).filter_by(id=db_id).one() if schema: table_names = database.all_table_names_in_schema( schema=schema, force=force_refresh, cache=database.table_cache_enabled, cache_timeout=database.table_cache_timeout) view_names = database.all_view_names_in_schema( schema=schema, force=force_refresh, cache=database.table_cache_enabled, cache_timeout=database.table_cache_timeout) else: table_names = database.all_table_names_in_database( cache=True, force=False, cache_timeout=24 * 60 * 60) view_names = database.all_view_names_in_database( cache=True, force=False, cache_timeout=24 * 60 * 60) table_names = security_manager.accessible_by_user(database, table_names, schema) view_names = security_manager.accessible_by_user(database, view_names, schema) if substr: table_names = [tn for tn in table_names if substr in tn] view_names = [vn for vn in view_names if substr in vn] if not schema and database.default_schemas: def get_schema(tbl_or_view_name): return tbl_or_view_name.split('.')[0] if '.' in tbl_or_view_name else None user_schema = g.user.email.split('@')[0] valid_schemas = set(database.default_schemas + [user_schema]) table_names = [tn for tn in table_names if get_schema(tn) in valid_schemas] view_names = [vn for vn in view_names if get_schema(vn) in valid_schemas] max_items = config.get('MAX_TABLE_NAMES') or len(table_names) total_items = len(table_names) + len(view_names) max_tables = len(table_names) max_views = len(view_names) if total_items and substr: max_tables = max_items * len(table_names) // total_items max_views = max_items * len(view_names) // total_items table_options = [{'value': tn, 'label': tn} for tn in table_names[:max_tables]] table_options.extend([{'value': vn, 'label': '[view] {}'.format(vn)} for vn in view_names[:max_views]]) payload = { 'tableLength': len(table_names) + len(view_names), 'options': table_options, } return json_success(json.dumps(payload))
[ "def", "tables", "(", "self", ",", "db_id", ",", "schema", ",", "substr", ",", "force_refresh", "=", "'false'", ")", ":", "db_id", "=", "int", "(", "db_id", ")", "force_refresh", "=", "force_refresh", ".", "lower", "(", ")", "==", "'true'", "schema", "=", "utils", ".", "js_string_to_python", "(", "schema", ")", "substr", "=", "utils", ".", "js_string_to_python", "(", "substr", ")", "database", "=", "db", ".", "session", ".", "query", "(", "models", ".", "Database", ")", ".", "filter_by", "(", "id", "=", "db_id", ")", ".", "one", "(", ")", "if", "schema", ":", "table_names", "=", "database", ".", "all_table_names_in_schema", "(", "schema", "=", "schema", ",", "force", "=", "force_refresh", ",", "cache", "=", "database", ".", "table_cache_enabled", ",", "cache_timeout", "=", "database", ".", "table_cache_timeout", ")", "view_names", "=", "database", ".", "all_view_names_in_schema", "(", "schema", "=", "schema", ",", "force", "=", "force_refresh", ",", "cache", "=", "database", ".", "table_cache_enabled", ",", "cache_timeout", "=", "database", ".", "table_cache_timeout", ")", "else", ":", "table_names", "=", "database", ".", "all_table_names_in_database", "(", "cache", "=", "True", ",", "force", "=", "False", ",", "cache_timeout", "=", "24", "*", "60", "*", "60", ")", "view_names", "=", "database", ".", "all_view_names_in_database", "(", "cache", "=", "True", ",", "force", "=", "False", ",", "cache_timeout", "=", "24", "*", "60", "*", "60", ")", "table_names", "=", "security_manager", ".", "accessible_by_user", "(", "database", ",", "table_names", ",", "schema", ")", "view_names", "=", "security_manager", ".", "accessible_by_user", "(", "database", ",", "view_names", ",", "schema", ")", "if", "substr", ":", "table_names", "=", "[", "tn", "for", "tn", "in", "table_names", "if", "substr", "in", "tn", "]", "view_names", "=", "[", "vn", "for", "vn", "in", "view_names", "if", "substr", "in", "vn", "]", "if", "not", "schema", "and", "database", ".", "default_schemas", ":", "def", "get_schema", "(", "tbl_or_view_name", ")", ":", "return", "tbl_or_view_name", ".", "split", "(", "'.'", ")", "[", "0", "]", "if", "'.'", "in", "tbl_or_view_name", "else", "None", "user_schema", "=", "g", ".", "user", ".", "email", ".", "split", "(", "'@'", ")", "[", "0", "]", "valid_schemas", "=", "set", "(", "database", ".", "default_schemas", "+", "[", "user_schema", "]", ")", "table_names", "=", "[", "tn", "for", "tn", "in", "table_names", "if", "get_schema", "(", "tn", ")", "in", "valid_schemas", "]", "view_names", "=", "[", "vn", "for", "vn", "in", "view_names", "if", "get_schema", "(", "vn", ")", "in", "valid_schemas", "]", "max_items", "=", "config", ".", "get", "(", "'MAX_TABLE_NAMES'", ")", "or", "len", "(", "table_names", ")", "total_items", "=", "len", "(", "table_names", ")", "+", "len", "(", "view_names", ")", "max_tables", "=", "len", "(", "table_names", ")", "max_views", "=", "len", "(", "view_names", ")", "if", "total_items", "and", "substr", ":", "max_tables", "=", "max_items", "*", "len", "(", "table_names", ")", "//", "total_items", "max_views", "=", "max_items", "*", "len", "(", "view_names", ")", "//", "total_items", "table_options", "=", "[", "{", "'value'", ":", "tn", ",", "'label'", ":", "tn", "}", "for", "tn", "in", "table_names", "[", ":", "max_tables", "]", "]", "table_options", ".", "extend", "(", "[", "{", "'value'", ":", "vn", ",", "'label'", ":", "'[view] {}'", ".", "format", "(", "vn", ")", "}", "for", "vn", "in", "view_names", "[", ":", "max_views", "]", "]", ")", "payload", "=", "{", "'tableLength'", ":", "len", "(", "table_names", ")", "+", "len", "(", "view_names", ")", ",", "'options'", ":", "table_options", ",", "}", "return", "json_success", "(", "json", ".", "dumps", "(", "payload", ")", ")" ]
Endpoint to fetch the list of tables for given database
[ "Endpoint", "to", "fetch", "the", "list", "of", "tables", "for", "given", "database" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1564-L1619
train
apache/incubator-superset
superset/views/core.py
Superset.copy_dash
def copy_dash(self, dashboard_id): """Copy dashboard""" session = db.session() data = json.loads(request.form.get('data')) dash = models.Dashboard() original_dash = ( session .query(models.Dashboard) .filter_by(id=dashboard_id).first()) dash.owners = [g.user] if g.user else [] dash.dashboard_title = data['dashboard_title'] if data['duplicate_slices']: # Duplicating slices as well, mapping old ids to new ones old_to_new_sliceids = {} for slc in original_dash.slices: new_slice = slc.clone() new_slice.owners = [g.user] if g.user else [] session.add(new_slice) session.flush() new_slice.dashboards.append(dash) old_to_new_sliceids['{}'.format(slc.id)] = \ '{}'.format(new_slice.id) # update chartId of layout entities # in v2_dash positions json data, chartId should be integer, # while in older version slice_id is string type for value in data['positions'].values(): if ( isinstance(value, dict) and value.get('meta') and value.get('meta').get('chartId') ): old_id = '{}'.format(value.get('meta').get('chartId')) new_id = int(old_to_new_sliceids[old_id]) value['meta']['chartId'] = new_id else: dash.slices = original_dash.slices dash.params = original_dash.params self._set_dash_metadata(dash, data) session.add(dash) session.commit() dash_json = json.dumps(dash.data) session.close() return json_success(dash_json)
python
def copy_dash(self, dashboard_id): """Copy dashboard""" session = db.session() data = json.loads(request.form.get('data')) dash = models.Dashboard() original_dash = ( session .query(models.Dashboard) .filter_by(id=dashboard_id).first()) dash.owners = [g.user] if g.user else [] dash.dashboard_title = data['dashboard_title'] if data['duplicate_slices']: # Duplicating slices as well, mapping old ids to new ones old_to_new_sliceids = {} for slc in original_dash.slices: new_slice = slc.clone() new_slice.owners = [g.user] if g.user else [] session.add(new_slice) session.flush() new_slice.dashboards.append(dash) old_to_new_sliceids['{}'.format(slc.id)] = \ '{}'.format(new_slice.id) # update chartId of layout entities # in v2_dash positions json data, chartId should be integer, # while in older version slice_id is string type for value in data['positions'].values(): if ( isinstance(value, dict) and value.get('meta') and value.get('meta').get('chartId') ): old_id = '{}'.format(value.get('meta').get('chartId')) new_id = int(old_to_new_sliceids[old_id]) value['meta']['chartId'] = new_id else: dash.slices = original_dash.slices dash.params = original_dash.params self._set_dash_metadata(dash, data) session.add(dash) session.commit() dash_json = json.dumps(dash.data) session.close() return json_success(dash_json)
[ "def", "copy_dash", "(", "self", ",", "dashboard_id", ")", ":", "session", "=", "db", ".", "session", "(", ")", "data", "=", "json", ".", "loads", "(", "request", ".", "form", ".", "get", "(", "'data'", ")", ")", "dash", "=", "models", ".", "Dashboard", "(", ")", "original_dash", "=", "(", "session", ".", "query", "(", "models", ".", "Dashboard", ")", ".", "filter_by", "(", "id", "=", "dashboard_id", ")", ".", "first", "(", ")", ")", "dash", ".", "owners", "=", "[", "g", ".", "user", "]", "if", "g", ".", "user", "else", "[", "]", "dash", ".", "dashboard_title", "=", "data", "[", "'dashboard_title'", "]", "if", "data", "[", "'duplicate_slices'", "]", ":", "# Duplicating slices as well, mapping old ids to new ones", "old_to_new_sliceids", "=", "{", "}", "for", "slc", "in", "original_dash", ".", "slices", ":", "new_slice", "=", "slc", ".", "clone", "(", ")", "new_slice", ".", "owners", "=", "[", "g", ".", "user", "]", "if", "g", ".", "user", "else", "[", "]", "session", ".", "add", "(", "new_slice", ")", "session", ".", "flush", "(", ")", "new_slice", ".", "dashboards", ".", "append", "(", "dash", ")", "old_to_new_sliceids", "[", "'{}'", ".", "format", "(", "slc", ".", "id", ")", "]", "=", "'{}'", ".", "format", "(", "new_slice", ".", "id", ")", "# update chartId of layout entities", "# in v2_dash positions json data, chartId should be integer,", "# while in older version slice_id is string type", "for", "value", "in", "data", "[", "'positions'", "]", ".", "values", "(", ")", ":", "if", "(", "isinstance", "(", "value", ",", "dict", ")", "and", "value", ".", "get", "(", "'meta'", ")", "and", "value", ".", "get", "(", "'meta'", ")", ".", "get", "(", "'chartId'", ")", ")", ":", "old_id", "=", "'{}'", ".", "format", "(", "value", ".", "get", "(", "'meta'", ")", ".", "get", "(", "'chartId'", ")", ")", "new_id", "=", "int", "(", "old_to_new_sliceids", "[", "old_id", "]", ")", "value", "[", "'meta'", "]", "[", "'chartId'", "]", "=", "new_id", "else", ":", "dash", ".", "slices", "=", "original_dash", ".", "slices", "dash", ".", "params", "=", "original_dash", ".", "params", "self", ".", "_set_dash_metadata", "(", "dash", ",", "data", ")", "session", ".", "add", "(", "dash", ")", "session", ".", "commit", "(", ")", "dash_json", "=", "json", ".", "dumps", "(", "dash", ".", "data", ")", "session", ".", "close", "(", ")", "return", "json_success", "(", "dash_json", ")" ]
Copy dashboard
[ "Copy", "dashboard" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1624-L1669
train
apache/incubator-superset
superset/views/core.py
Superset.save_dash
def save_dash(self, dashboard_id): """Save a dashboard's metadata""" session = db.session() dash = (session .query(models.Dashboard) .filter_by(id=dashboard_id).first()) check_ownership(dash, raise_if_false=True) data = json.loads(request.form.get('data')) self._set_dash_metadata(dash, data) session.merge(dash) session.commit() session.close() return json_success(json.dumps({'status': 'SUCCESS'}))
python
def save_dash(self, dashboard_id): """Save a dashboard's metadata""" session = db.session() dash = (session .query(models.Dashboard) .filter_by(id=dashboard_id).first()) check_ownership(dash, raise_if_false=True) data = json.loads(request.form.get('data')) self._set_dash_metadata(dash, data) session.merge(dash) session.commit() session.close() return json_success(json.dumps({'status': 'SUCCESS'}))
[ "def", "save_dash", "(", "self", ",", "dashboard_id", ")", ":", "session", "=", "db", ".", "session", "(", ")", "dash", "=", "(", "session", ".", "query", "(", "models", ".", "Dashboard", ")", ".", "filter_by", "(", "id", "=", "dashboard_id", ")", ".", "first", "(", ")", ")", "check_ownership", "(", "dash", ",", "raise_if_false", "=", "True", ")", "data", "=", "json", ".", "loads", "(", "request", ".", "form", ".", "get", "(", "'data'", ")", ")", "self", ".", "_set_dash_metadata", "(", "dash", ",", "data", ")", "session", ".", "merge", "(", "dash", ")", "session", ".", "commit", "(", ")", "session", ".", "close", "(", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "{", "'status'", ":", "'SUCCESS'", "}", ")", ")" ]
Save a dashboard's metadata
[ "Save", "a", "dashboard", "s", "metadata" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1674-L1686
train
apache/incubator-superset
superset/views/core.py
Superset.add_slices
def add_slices(self, dashboard_id): """Add and save slices to a dashboard""" data = json.loads(request.form.get('data')) session = db.session() Slice = models.Slice # noqa dash = ( session.query(models.Dashboard).filter_by(id=dashboard_id).first()) check_ownership(dash, raise_if_false=True) new_slices = session.query(Slice).filter( Slice.id.in_(data['slice_ids'])) dash.slices += new_slices session.merge(dash) session.commit() session.close() return 'SLICES ADDED'
python
def add_slices(self, dashboard_id): """Add and save slices to a dashboard""" data = json.loads(request.form.get('data')) session = db.session() Slice = models.Slice # noqa dash = ( session.query(models.Dashboard).filter_by(id=dashboard_id).first()) check_ownership(dash, raise_if_false=True) new_slices = session.query(Slice).filter( Slice.id.in_(data['slice_ids'])) dash.slices += new_slices session.merge(dash) session.commit() session.close() return 'SLICES ADDED'
[ "def", "add_slices", "(", "self", ",", "dashboard_id", ")", ":", "data", "=", "json", ".", "loads", "(", "request", ".", "form", ".", "get", "(", "'data'", ")", ")", "session", "=", "db", ".", "session", "(", ")", "Slice", "=", "models", ".", "Slice", "# noqa", "dash", "=", "(", "session", ".", "query", "(", "models", ".", "Dashboard", ")", ".", "filter_by", "(", "id", "=", "dashboard_id", ")", ".", "first", "(", ")", ")", "check_ownership", "(", "dash", ",", "raise_if_false", "=", "True", ")", "new_slices", "=", "session", ".", "query", "(", "Slice", ")", ".", "filter", "(", "Slice", ".", "id", ".", "in_", "(", "data", "[", "'slice_ids'", "]", ")", ")", "dash", ".", "slices", "+=", "new_slices", "session", ".", "merge", "(", "dash", ")", "session", ".", "commit", "(", ")", "session", ".", "close", "(", ")", "return", "'SLICES ADDED'" ]
Add and save slices to a dashboard
[ "Add", "and", "save", "slices", "to", "a", "dashboard" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1749-L1763
train
apache/incubator-superset
superset/views/core.py
Superset.recent_activity
def recent_activity(self, user_id): """Recent activity (actions) for a given user""" M = models # noqa if request.args.get('limit'): limit = int(request.args.get('limit')) else: limit = 1000 qry = ( db.session.query(M.Log, M.Dashboard, M.Slice) .outerjoin( M.Dashboard, M.Dashboard.id == M.Log.dashboard_id, ) .outerjoin( M.Slice, M.Slice.id == M.Log.slice_id, ) .filter( sqla.and_( ~M.Log.action.in_(('queries', 'shortner', 'sql_json')), M.Log.user_id == user_id, ), ) .order_by(M.Log.dttm.desc()) .limit(limit) ) payload = [] for log in qry.all(): item_url = None item_title = None if log.Dashboard: item_url = log.Dashboard.url item_title = log.Dashboard.dashboard_title elif log.Slice: item_url = log.Slice.slice_url item_title = log.Slice.slice_name payload.append({ 'action': log.Log.action, 'item_url': item_url, 'item_title': item_title, 'time': log.Log.dttm, }) return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
python
def recent_activity(self, user_id): """Recent activity (actions) for a given user""" M = models # noqa if request.args.get('limit'): limit = int(request.args.get('limit')) else: limit = 1000 qry = ( db.session.query(M.Log, M.Dashboard, M.Slice) .outerjoin( M.Dashboard, M.Dashboard.id == M.Log.dashboard_id, ) .outerjoin( M.Slice, M.Slice.id == M.Log.slice_id, ) .filter( sqla.and_( ~M.Log.action.in_(('queries', 'shortner', 'sql_json')), M.Log.user_id == user_id, ), ) .order_by(M.Log.dttm.desc()) .limit(limit) ) payload = [] for log in qry.all(): item_url = None item_title = None if log.Dashboard: item_url = log.Dashboard.url item_title = log.Dashboard.dashboard_title elif log.Slice: item_url = log.Slice.slice_url item_title = log.Slice.slice_name payload.append({ 'action': log.Log.action, 'item_url': item_url, 'item_title': item_title, 'time': log.Log.dttm, }) return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
[ "def", "recent_activity", "(", "self", ",", "user_id", ")", ":", "M", "=", "models", "# noqa", "if", "request", ".", "args", ".", "get", "(", "'limit'", ")", ":", "limit", "=", "int", "(", "request", ".", "args", ".", "get", "(", "'limit'", ")", ")", "else", ":", "limit", "=", "1000", "qry", "=", "(", "db", ".", "session", ".", "query", "(", "M", ".", "Log", ",", "M", ".", "Dashboard", ",", "M", ".", "Slice", ")", ".", "outerjoin", "(", "M", ".", "Dashboard", ",", "M", ".", "Dashboard", ".", "id", "==", "M", ".", "Log", ".", "dashboard_id", ",", ")", ".", "outerjoin", "(", "M", ".", "Slice", ",", "M", ".", "Slice", ".", "id", "==", "M", ".", "Log", ".", "slice_id", ",", ")", ".", "filter", "(", "sqla", ".", "and_", "(", "~", "M", ".", "Log", ".", "action", ".", "in_", "(", "(", "'queries'", ",", "'shortner'", ",", "'sql_json'", ")", ")", ",", "M", ".", "Log", ".", "user_id", "==", "user_id", ",", ")", ",", ")", ".", "order_by", "(", "M", ".", "Log", ".", "dttm", ".", "desc", "(", ")", ")", ".", "limit", "(", "limit", ")", ")", "payload", "=", "[", "]", "for", "log", "in", "qry", ".", "all", "(", ")", ":", "item_url", "=", "None", "item_title", "=", "None", "if", "log", ".", "Dashboard", ":", "item_url", "=", "log", ".", "Dashboard", ".", "url", "item_title", "=", "log", ".", "Dashboard", ".", "dashboard_title", "elif", "log", ".", "Slice", ":", "item_url", "=", "log", ".", "Slice", ".", "slice_url", "item_title", "=", "log", ".", "Slice", ".", "slice_name", "payload", ".", "append", "(", "{", "'action'", ":", "log", ".", "Log", ".", "action", ",", "'item_url'", ":", "item_url", ",", "'item_title'", ":", "item_title", ",", "'time'", ":", "log", ".", "Log", ".", "dttm", ",", "}", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ")" ]
Recent activity (actions) for a given user
[ "Recent", "activity", "(", "actions", ")", "for", "a", "given", "user" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1826-L1872
train
apache/incubator-superset
superset/views/core.py
Superset.fave_dashboards_by_username
def fave_dashboards_by_username(self, username): """This lets us use a user's username to pull favourite dashboards""" user = security_manager.find_user(username=username) return self.fave_dashboards(user.get_id())
python
def fave_dashboards_by_username(self, username): """This lets us use a user's username to pull favourite dashboards""" user = security_manager.find_user(username=username) return self.fave_dashboards(user.get_id())
[ "def", "fave_dashboards_by_username", "(", "self", ",", "username", ")", ":", "user", "=", "security_manager", ".", "find_user", "(", "username", "=", "username", ")", "return", "self", ".", "fave_dashboards", "(", "user", ".", "get_id", "(", ")", ")" ]
This lets us use a user's username to pull favourite dashboards
[ "This", "lets", "us", "use", "a", "user", "s", "username", "to", "pull", "favourite", "dashboards" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1900-L1903
train
apache/incubator-superset
superset/views/core.py
Superset.user_slices
def user_slices(self, user_id=None): """List of slices a user created, or faved""" if not user_id: user_id = g.user.id Slice = models.Slice # noqa FavStar = models.FavStar # noqa qry = ( db.session.query(Slice, FavStar.dttm).join( models.FavStar, sqla.and_( models.FavStar.user_id == int(user_id), models.FavStar.class_name == 'slice', models.Slice.id == models.FavStar.obj_id, ), isouter=True).filter( sqla.or_( Slice.created_by_fk == user_id, Slice.changed_by_fk == user_id, FavStar.user_id == user_id, ), ) .order_by(Slice.slice_name.asc()) ) payload = [{ 'id': o.Slice.id, 'title': o.Slice.slice_name, 'url': o.Slice.slice_url, 'data': o.Slice.form_data, 'dttm': o.dttm if o.dttm else o.Slice.changed_on, 'viz_type': o.Slice.viz_type, } for o in qry.all()] return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
python
def user_slices(self, user_id=None): """List of slices a user created, or faved""" if not user_id: user_id = g.user.id Slice = models.Slice # noqa FavStar = models.FavStar # noqa qry = ( db.session.query(Slice, FavStar.dttm).join( models.FavStar, sqla.and_( models.FavStar.user_id == int(user_id), models.FavStar.class_name == 'slice', models.Slice.id == models.FavStar.obj_id, ), isouter=True).filter( sqla.or_( Slice.created_by_fk == user_id, Slice.changed_by_fk == user_id, FavStar.user_id == user_id, ), ) .order_by(Slice.slice_name.asc()) ) payload = [{ 'id': o.Slice.id, 'title': o.Slice.slice_name, 'url': o.Slice.slice_url, 'data': o.Slice.form_data, 'dttm': o.dttm if o.dttm else o.Slice.changed_on, 'viz_type': o.Slice.viz_type, } for o in qry.all()] return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
[ "def", "user_slices", "(", "self", ",", "user_id", "=", "None", ")", ":", "if", "not", "user_id", ":", "user_id", "=", "g", ".", "user", ".", "id", "Slice", "=", "models", ".", "Slice", "# noqa", "FavStar", "=", "models", ".", "FavStar", "# noqa", "qry", "=", "(", "db", ".", "session", ".", "query", "(", "Slice", ",", "FavStar", ".", "dttm", ")", ".", "join", "(", "models", ".", "FavStar", ",", "sqla", ".", "and_", "(", "models", ".", "FavStar", ".", "user_id", "==", "int", "(", "user_id", ")", ",", "models", ".", "FavStar", ".", "class_name", "==", "'slice'", ",", "models", ".", "Slice", ".", "id", "==", "models", ".", "FavStar", ".", "obj_id", ",", ")", ",", "isouter", "=", "True", ")", ".", "filter", "(", "sqla", ".", "or_", "(", "Slice", ".", "created_by_fk", "==", "user_id", ",", "Slice", ".", "changed_by_fk", "==", "user_id", ",", "FavStar", ".", "user_id", "==", "user_id", ",", ")", ",", ")", ".", "order_by", "(", "Slice", ".", "slice_name", ".", "asc", "(", ")", ")", ")", "payload", "=", "[", "{", "'id'", ":", "o", ".", "Slice", ".", "id", ",", "'title'", ":", "o", ".", "Slice", ".", "slice_name", ",", "'url'", ":", "o", ".", "Slice", ".", "slice_url", ",", "'data'", ":", "o", ".", "Slice", ".", "form_data", ",", "'dttm'", ":", "o", ".", "dttm", "if", "o", ".", "dttm", "else", "o", ".", "Slice", ".", "changed_on", ",", "'viz_type'", ":", "o", ".", "Slice", ".", "viz_type", ",", "}", "for", "o", "in", "qry", ".", "all", "(", ")", "]", "return", "json_success", "(", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ")" ]
List of slices a user created, or faved
[ "List", "of", "slices", "a", "user", "created", "or", "faved" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L1977-L2010
train
apache/incubator-superset
superset/views/core.py
Superset.created_slices
def created_slices(self, user_id=None): """List of slices created by this user""" if not user_id: user_id = g.user.id Slice = models.Slice # noqa qry = ( db.session.query(Slice) .filter( sqla.or_( Slice.created_by_fk == user_id, Slice.changed_by_fk == user_id, ), ) .order_by(Slice.changed_on.desc()) ) payload = [{ 'id': o.id, 'title': o.slice_name, 'url': o.slice_url, 'dttm': o.changed_on, 'viz_type': o.viz_type, } for o in qry.all()] return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
python
def created_slices(self, user_id=None): """List of slices created by this user""" if not user_id: user_id = g.user.id Slice = models.Slice # noqa qry = ( db.session.query(Slice) .filter( sqla.or_( Slice.created_by_fk == user_id, Slice.changed_by_fk == user_id, ), ) .order_by(Slice.changed_on.desc()) ) payload = [{ 'id': o.id, 'title': o.slice_name, 'url': o.slice_url, 'dttm': o.changed_on, 'viz_type': o.viz_type, } for o in qry.all()] return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
[ "def", "created_slices", "(", "self", ",", "user_id", "=", "None", ")", ":", "if", "not", "user_id", ":", "user_id", "=", "g", ".", "user", ".", "id", "Slice", "=", "models", ".", "Slice", "# noqa", "qry", "=", "(", "db", ".", "session", ".", "query", "(", "Slice", ")", ".", "filter", "(", "sqla", ".", "or_", "(", "Slice", ".", "created_by_fk", "==", "user_id", ",", "Slice", ".", "changed_by_fk", "==", "user_id", ",", ")", ",", ")", ".", "order_by", "(", "Slice", ".", "changed_on", ".", "desc", "(", ")", ")", ")", "payload", "=", "[", "{", "'id'", ":", "o", ".", "id", ",", "'title'", ":", "o", ".", "slice_name", ",", "'url'", ":", "o", ".", "slice_url", ",", "'dttm'", ":", "o", ".", "changed_on", ",", "'viz_type'", ":", "o", ".", "viz_type", ",", "}", "for", "o", "in", "qry", ".", "all", "(", ")", "]", "return", "json_success", "(", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ")" ]
List of slices created by this user
[ "List", "of", "slices", "created", "by", "this", "user" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2016-L2039
train
apache/incubator-superset
superset/views/core.py
Superset.fave_slices
def fave_slices(self, user_id=None): """Favorite slices for a user""" if not user_id: user_id = g.user.id qry = ( db.session.query( models.Slice, models.FavStar.dttm, ) .join( models.FavStar, sqla.and_( models.FavStar.user_id == int(user_id), models.FavStar.class_name == 'slice', models.Slice.id == models.FavStar.obj_id, ), ) .order_by( models.FavStar.dttm.desc(), ) ) payload = [] for o in qry.all(): d = { 'id': o.Slice.id, 'title': o.Slice.slice_name, 'url': o.Slice.slice_url, 'dttm': o.dttm, 'viz_type': o.Slice.viz_type, } if o.Slice.created_by: user = o.Slice.created_by d['creator'] = str(user) d['creator_url'] = '/superset/profile/{}/'.format( user.username) payload.append(d) return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
python
def fave_slices(self, user_id=None): """Favorite slices for a user""" if not user_id: user_id = g.user.id qry = ( db.session.query( models.Slice, models.FavStar.dttm, ) .join( models.FavStar, sqla.and_( models.FavStar.user_id == int(user_id), models.FavStar.class_name == 'slice', models.Slice.id == models.FavStar.obj_id, ), ) .order_by( models.FavStar.dttm.desc(), ) ) payload = [] for o in qry.all(): d = { 'id': o.Slice.id, 'title': o.Slice.slice_name, 'url': o.Slice.slice_url, 'dttm': o.dttm, 'viz_type': o.Slice.viz_type, } if o.Slice.created_by: user = o.Slice.created_by d['creator'] = str(user) d['creator_url'] = '/superset/profile/{}/'.format( user.username) payload.append(d) return json_success( json.dumps(payload, default=utils.json_int_dttm_ser))
[ "def", "fave_slices", "(", "self", ",", "user_id", "=", "None", ")", ":", "if", "not", "user_id", ":", "user_id", "=", "g", ".", "user", ".", "id", "qry", "=", "(", "db", ".", "session", ".", "query", "(", "models", ".", "Slice", ",", "models", ".", "FavStar", ".", "dttm", ",", ")", ".", "join", "(", "models", ".", "FavStar", ",", "sqla", ".", "and_", "(", "models", ".", "FavStar", ".", "user_id", "==", "int", "(", "user_id", ")", ",", "models", ".", "FavStar", ".", "class_name", "==", "'slice'", ",", "models", ".", "Slice", ".", "id", "==", "models", ".", "FavStar", ".", "obj_id", ",", ")", ",", ")", ".", "order_by", "(", "models", ".", "FavStar", ".", "dttm", ".", "desc", "(", ")", ",", ")", ")", "payload", "=", "[", "]", "for", "o", "in", "qry", ".", "all", "(", ")", ":", "d", "=", "{", "'id'", ":", "o", ".", "Slice", ".", "id", ",", "'title'", ":", "o", ".", "Slice", ".", "slice_name", ",", "'url'", ":", "o", ".", "Slice", ".", "slice_url", ",", "'dttm'", ":", "o", ".", "dttm", ",", "'viz_type'", ":", "o", ".", "Slice", ".", "viz_type", ",", "}", "if", "o", ".", "Slice", ".", "created_by", ":", "user", "=", "o", ".", "Slice", ".", "created_by", "d", "[", "'creator'", "]", "=", "str", "(", "user", ")", "d", "[", "'creator_url'", "]", "=", "'/superset/profile/{}/'", ".", "format", "(", "user", ".", "username", ")", "payload", ".", "append", "(", "d", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ")" ]
Favorite slices for a user
[ "Favorite", "slices", "for", "a", "user" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2045-L2082
train
apache/incubator-superset
superset/views/core.py
Superset.warm_up_cache
def warm_up_cache(self): """Warms up the cache for the slice or table. Note for slices a force refresh occurs. """ slices = None session = db.session() slice_id = request.args.get('slice_id') table_name = request.args.get('table_name') db_name = request.args.get('db_name') if not slice_id and not (table_name and db_name): return json_error_response(__( 'Malformed request. slice_id or table_name and db_name ' 'arguments are expected'), status=400) if slice_id: slices = session.query(models.Slice).filter_by(id=slice_id).all() if not slices: return json_error_response(__( 'Chart %(id)s not found', id=slice_id), status=404) elif table_name and db_name: SqlaTable = ConnectorRegistry.sources['table'] table = ( session.query(SqlaTable) .join(models.Database) .filter( models.Database.database_name == db_name or SqlaTable.table_name == table_name) ).first() if not table: return json_error_response(__( "Table %(t)s wasn't found in the database %(d)s", t=table_name, s=db_name), status=404) slices = session.query(models.Slice).filter_by( datasource_id=table.id, datasource_type=table.type).all() for slc in slices: try: form_data = get_form_data(slc.id, use_slice_data=True)[0] obj = get_viz( datasource_type=slc.datasource.type, datasource_id=slc.datasource.id, form_data=form_data, force=True, ) obj.get_json() except Exception as e: return json_error_response(utils.error_msg_from_exception(e)) return json_success(json.dumps( [{'slice_id': slc.id, 'slice_name': slc.slice_name} for slc in slices]))
python
def warm_up_cache(self): """Warms up the cache for the slice or table. Note for slices a force refresh occurs. """ slices = None session = db.session() slice_id = request.args.get('slice_id') table_name = request.args.get('table_name') db_name = request.args.get('db_name') if not slice_id and not (table_name and db_name): return json_error_response(__( 'Malformed request. slice_id or table_name and db_name ' 'arguments are expected'), status=400) if slice_id: slices = session.query(models.Slice).filter_by(id=slice_id).all() if not slices: return json_error_response(__( 'Chart %(id)s not found', id=slice_id), status=404) elif table_name and db_name: SqlaTable = ConnectorRegistry.sources['table'] table = ( session.query(SqlaTable) .join(models.Database) .filter( models.Database.database_name == db_name or SqlaTable.table_name == table_name) ).first() if not table: return json_error_response(__( "Table %(t)s wasn't found in the database %(d)s", t=table_name, s=db_name), status=404) slices = session.query(models.Slice).filter_by( datasource_id=table.id, datasource_type=table.type).all() for slc in slices: try: form_data = get_form_data(slc.id, use_slice_data=True)[0] obj = get_viz( datasource_type=slc.datasource.type, datasource_id=slc.datasource.id, form_data=form_data, force=True, ) obj.get_json() except Exception as e: return json_error_response(utils.error_msg_from_exception(e)) return json_success(json.dumps( [{'slice_id': slc.id, 'slice_name': slc.slice_name} for slc in slices]))
[ "def", "warm_up_cache", "(", "self", ")", ":", "slices", "=", "None", "session", "=", "db", ".", "session", "(", ")", "slice_id", "=", "request", ".", "args", ".", "get", "(", "'slice_id'", ")", "table_name", "=", "request", ".", "args", ".", "get", "(", "'table_name'", ")", "db_name", "=", "request", ".", "args", ".", "get", "(", "'db_name'", ")", "if", "not", "slice_id", "and", "not", "(", "table_name", "and", "db_name", ")", ":", "return", "json_error_response", "(", "__", "(", "'Malformed request. slice_id or table_name and db_name '", "'arguments are expected'", ")", ",", "status", "=", "400", ")", "if", "slice_id", ":", "slices", "=", "session", ".", "query", "(", "models", ".", "Slice", ")", ".", "filter_by", "(", "id", "=", "slice_id", ")", ".", "all", "(", ")", "if", "not", "slices", ":", "return", "json_error_response", "(", "__", "(", "'Chart %(id)s not found'", ",", "id", "=", "slice_id", ")", ",", "status", "=", "404", ")", "elif", "table_name", "and", "db_name", ":", "SqlaTable", "=", "ConnectorRegistry", ".", "sources", "[", "'table'", "]", "table", "=", "(", "session", ".", "query", "(", "SqlaTable", ")", ".", "join", "(", "models", ".", "Database", ")", ".", "filter", "(", "models", ".", "Database", ".", "database_name", "==", "db_name", "or", "SqlaTable", ".", "table_name", "==", "table_name", ")", ")", ".", "first", "(", ")", "if", "not", "table", ":", "return", "json_error_response", "(", "__", "(", "\"Table %(t)s wasn't found in the database %(d)s\"", ",", "t", "=", "table_name", ",", "s", "=", "db_name", ")", ",", "status", "=", "404", ")", "slices", "=", "session", ".", "query", "(", "models", ".", "Slice", ")", ".", "filter_by", "(", "datasource_id", "=", "table", ".", "id", ",", "datasource_type", "=", "table", ".", "type", ")", ".", "all", "(", ")", "for", "slc", "in", "slices", ":", "try", ":", "form_data", "=", "get_form_data", "(", "slc", ".", "id", ",", "use_slice_data", "=", "True", ")", "[", "0", "]", "obj", "=", "get_viz", "(", "datasource_type", "=", "slc", ".", "datasource", ".", "type", ",", "datasource_id", "=", "slc", ".", "datasource", ".", "id", ",", "form_data", "=", "form_data", ",", "force", "=", "True", ",", ")", "obj", ".", "get_json", "(", ")", "except", "Exception", "as", "e", ":", "return", "json_error_response", "(", "utils", ".", "error_msg_from_exception", "(", "e", ")", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "[", "{", "'slice_id'", ":", "slc", ".", "id", ",", "'slice_name'", ":", "slc", ".", "slice_name", "}", "for", "slc", "in", "slices", "]", ")", ")" ]
Warms up the cache for the slice or table. Note for slices a force refresh occurs.
[ "Warms", "up", "the", "cache", "for", "the", "slice", "or", "table", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2087-L2138
train
apache/incubator-superset
superset/views/core.py
Superset.favstar
def favstar(self, class_name, obj_id, action): """Toggle favorite stars on Slices and Dashboard""" session = db.session() FavStar = models.FavStar # noqa count = 0 favs = session.query(FavStar).filter_by( class_name=class_name, obj_id=obj_id, user_id=g.user.get_id()).all() if action == 'select': if not favs: session.add( FavStar( class_name=class_name, obj_id=obj_id, user_id=g.user.get_id(), dttm=datetime.now(), ), ) count = 1 elif action == 'unselect': for fav in favs: session.delete(fav) else: count = len(favs) session.commit() return json_success(json.dumps({'count': count}))
python
def favstar(self, class_name, obj_id, action): """Toggle favorite stars on Slices and Dashboard""" session = db.session() FavStar = models.FavStar # noqa count = 0 favs = session.query(FavStar).filter_by( class_name=class_name, obj_id=obj_id, user_id=g.user.get_id()).all() if action == 'select': if not favs: session.add( FavStar( class_name=class_name, obj_id=obj_id, user_id=g.user.get_id(), dttm=datetime.now(), ), ) count = 1 elif action == 'unselect': for fav in favs: session.delete(fav) else: count = len(favs) session.commit() return json_success(json.dumps({'count': count}))
[ "def", "favstar", "(", "self", ",", "class_name", ",", "obj_id", ",", "action", ")", ":", "session", "=", "db", ".", "session", "(", ")", "FavStar", "=", "models", ".", "FavStar", "# noqa", "count", "=", "0", "favs", "=", "session", ".", "query", "(", "FavStar", ")", ".", "filter_by", "(", "class_name", "=", "class_name", ",", "obj_id", "=", "obj_id", ",", "user_id", "=", "g", ".", "user", ".", "get_id", "(", ")", ")", ".", "all", "(", ")", "if", "action", "==", "'select'", ":", "if", "not", "favs", ":", "session", ".", "add", "(", "FavStar", "(", "class_name", "=", "class_name", ",", "obj_id", "=", "obj_id", ",", "user_id", "=", "g", ".", "user", ".", "get_id", "(", ")", ",", "dttm", "=", "datetime", ".", "now", "(", ")", ",", ")", ",", ")", "count", "=", "1", "elif", "action", "==", "'unselect'", ":", "for", "fav", "in", "favs", ":", "session", ".", "delete", "(", "fav", ")", "else", ":", "count", "=", "len", "(", "favs", ")", "session", ".", "commit", "(", ")", "return", "json_success", "(", "json", ".", "dumps", "(", "{", "'count'", ":", "count", "}", ")", ")" ]
Toggle favorite stars on Slices and Dashboard
[ "Toggle", "favorite", "stars", "on", "Slices", "and", "Dashboard" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2142-L2167
train
apache/incubator-superset
superset/views/core.py
Superset.dashboard
def dashboard(self, dashboard_id): """Server side rendering for a dashboard""" session = db.session() qry = session.query(models.Dashboard) if dashboard_id.isdigit(): qry = qry.filter_by(id=int(dashboard_id)) else: qry = qry.filter_by(slug=dashboard_id) dash = qry.one_or_none() if not dash: abort(404) datasources = set() for slc in dash.slices: datasource = slc.datasource if datasource: datasources.add(datasource) if config.get('ENABLE_ACCESS_REQUEST'): for datasource in datasources: if datasource and not security_manager.datasource_access(datasource): flash( __(security_manager.get_datasource_access_error_msg(datasource)), 'danger') return redirect( 'superset/request_access/?' f'dashboard_id={dash.id}&') dash_edit_perm = check_ownership(dash, raise_if_false=False) and \ security_manager.can_access('can_save_dash', 'Superset') dash_save_perm = security_manager.can_access('can_save_dash', 'Superset') superset_can_explore = security_manager.can_access('can_explore', 'Superset') superset_can_csv = security_manager.can_access('can_csv', 'Superset') slice_can_edit = security_manager.can_access('can_edit', 'SliceModelView') standalone_mode = request.args.get('standalone') == 'true' edit_mode = request.args.get('edit') == 'true' # Hack to log the dashboard_id properly, even when getting a slug @log_this def dashboard(**kwargs): # noqa pass dashboard( dashboard_id=dash.id, dashboard_version='v2', dash_edit_perm=dash_edit_perm, edit_mode=edit_mode) dashboard_data = dash.data dashboard_data.update({ 'standalone_mode': standalone_mode, 'dash_save_perm': dash_save_perm, 'dash_edit_perm': dash_edit_perm, 'superset_can_explore': superset_can_explore, 'superset_can_csv': superset_can_csv, 'slice_can_edit': slice_can_edit, }) bootstrap_data = { 'user_id': g.user.get_id(), 'dashboard_data': dashboard_data, 'datasources': {ds.uid: ds.data for ds in datasources}, 'common': self.common_bootsrap_payload(), 'editMode': edit_mode, } if request.args.get('json') == 'true': return json_success(json.dumps(bootstrap_data)) return self.render_template( 'superset/dashboard.html', entry='dashboard', standalone_mode=standalone_mode, title=dash.dashboard_title, bootstrap_data=json.dumps(bootstrap_data), )
python
def dashboard(self, dashboard_id): """Server side rendering for a dashboard""" session = db.session() qry = session.query(models.Dashboard) if dashboard_id.isdigit(): qry = qry.filter_by(id=int(dashboard_id)) else: qry = qry.filter_by(slug=dashboard_id) dash = qry.one_or_none() if not dash: abort(404) datasources = set() for slc in dash.slices: datasource = slc.datasource if datasource: datasources.add(datasource) if config.get('ENABLE_ACCESS_REQUEST'): for datasource in datasources: if datasource and not security_manager.datasource_access(datasource): flash( __(security_manager.get_datasource_access_error_msg(datasource)), 'danger') return redirect( 'superset/request_access/?' f'dashboard_id={dash.id}&') dash_edit_perm = check_ownership(dash, raise_if_false=False) and \ security_manager.can_access('can_save_dash', 'Superset') dash_save_perm = security_manager.can_access('can_save_dash', 'Superset') superset_can_explore = security_manager.can_access('can_explore', 'Superset') superset_can_csv = security_manager.can_access('can_csv', 'Superset') slice_can_edit = security_manager.can_access('can_edit', 'SliceModelView') standalone_mode = request.args.get('standalone') == 'true' edit_mode = request.args.get('edit') == 'true' # Hack to log the dashboard_id properly, even when getting a slug @log_this def dashboard(**kwargs): # noqa pass dashboard( dashboard_id=dash.id, dashboard_version='v2', dash_edit_perm=dash_edit_perm, edit_mode=edit_mode) dashboard_data = dash.data dashboard_data.update({ 'standalone_mode': standalone_mode, 'dash_save_perm': dash_save_perm, 'dash_edit_perm': dash_edit_perm, 'superset_can_explore': superset_can_explore, 'superset_can_csv': superset_can_csv, 'slice_can_edit': slice_can_edit, }) bootstrap_data = { 'user_id': g.user.get_id(), 'dashboard_data': dashboard_data, 'datasources': {ds.uid: ds.data for ds in datasources}, 'common': self.common_bootsrap_payload(), 'editMode': edit_mode, } if request.args.get('json') == 'true': return json_success(json.dumps(bootstrap_data)) return self.render_template( 'superset/dashboard.html', entry='dashboard', standalone_mode=standalone_mode, title=dash.dashboard_title, bootstrap_data=json.dumps(bootstrap_data), )
[ "def", "dashboard", "(", "self", ",", "dashboard_id", ")", ":", "session", "=", "db", ".", "session", "(", ")", "qry", "=", "session", ".", "query", "(", "models", ".", "Dashboard", ")", "if", "dashboard_id", ".", "isdigit", "(", ")", ":", "qry", "=", "qry", ".", "filter_by", "(", "id", "=", "int", "(", "dashboard_id", ")", ")", "else", ":", "qry", "=", "qry", ".", "filter_by", "(", "slug", "=", "dashboard_id", ")", "dash", "=", "qry", ".", "one_or_none", "(", ")", "if", "not", "dash", ":", "abort", "(", "404", ")", "datasources", "=", "set", "(", ")", "for", "slc", "in", "dash", ".", "slices", ":", "datasource", "=", "slc", ".", "datasource", "if", "datasource", ":", "datasources", ".", "add", "(", "datasource", ")", "if", "config", ".", "get", "(", "'ENABLE_ACCESS_REQUEST'", ")", ":", "for", "datasource", "in", "datasources", ":", "if", "datasource", "and", "not", "security_manager", ".", "datasource_access", "(", "datasource", ")", ":", "flash", "(", "__", "(", "security_manager", ".", "get_datasource_access_error_msg", "(", "datasource", ")", ")", ",", "'danger'", ")", "return", "redirect", "(", "'superset/request_access/?'", "f'dashboard_id={dash.id}&'", ")", "dash_edit_perm", "=", "check_ownership", "(", "dash", ",", "raise_if_false", "=", "False", ")", "and", "security_manager", ".", "can_access", "(", "'can_save_dash'", ",", "'Superset'", ")", "dash_save_perm", "=", "security_manager", ".", "can_access", "(", "'can_save_dash'", ",", "'Superset'", ")", "superset_can_explore", "=", "security_manager", ".", "can_access", "(", "'can_explore'", ",", "'Superset'", ")", "superset_can_csv", "=", "security_manager", ".", "can_access", "(", "'can_csv'", ",", "'Superset'", ")", "slice_can_edit", "=", "security_manager", ".", "can_access", "(", "'can_edit'", ",", "'SliceModelView'", ")", "standalone_mode", "=", "request", ".", "args", ".", "get", "(", "'standalone'", ")", "==", "'true'", "edit_mode", "=", "request", ".", "args", ".", "get", "(", "'edit'", ")", "==", "'true'", "# Hack to log the dashboard_id properly, even when getting a slug", "@", "log_this", "def", "dashboard", "(", "*", "*", "kwargs", ")", ":", "# noqa", "pass", "dashboard", "(", "dashboard_id", "=", "dash", ".", "id", ",", "dashboard_version", "=", "'v2'", ",", "dash_edit_perm", "=", "dash_edit_perm", ",", "edit_mode", "=", "edit_mode", ")", "dashboard_data", "=", "dash", ".", "data", "dashboard_data", ".", "update", "(", "{", "'standalone_mode'", ":", "standalone_mode", ",", "'dash_save_perm'", ":", "dash_save_perm", ",", "'dash_edit_perm'", ":", "dash_edit_perm", ",", "'superset_can_explore'", ":", "superset_can_explore", ",", "'superset_can_csv'", ":", "superset_can_csv", ",", "'slice_can_edit'", ":", "slice_can_edit", ",", "}", ")", "bootstrap_data", "=", "{", "'user_id'", ":", "g", ".", "user", ".", "get_id", "(", ")", ",", "'dashboard_data'", ":", "dashboard_data", ",", "'datasources'", ":", "{", "ds", ".", "uid", ":", "ds", ".", "data", "for", "ds", "in", "datasources", "}", ",", "'common'", ":", "self", ".", "common_bootsrap_payload", "(", ")", ",", "'editMode'", ":", "edit_mode", ",", "}", "if", "request", ".", "args", ".", "get", "(", "'json'", ")", "==", "'true'", ":", "return", "json_success", "(", "json", ".", "dumps", "(", "bootstrap_data", ")", ")", "return", "self", ".", "render_template", "(", "'superset/dashboard.html'", ",", "entry", "=", "'dashboard'", ",", "standalone_mode", "=", "standalone_mode", ",", "title", "=", "dash", ".", "dashboard_title", ",", "bootstrap_data", "=", "json", ".", "dumps", "(", "bootstrap_data", ")", ",", ")" ]
Server side rendering for a dashboard
[ "Server", "side", "rendering", "for", "a", "dashboard" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2171-L2246
train
apache/incubator-superset
superset/views/core.py
Superset.sync_druid_source
def sync_druid_source(self): """Syncs the druid datasource in main db with the provided config. The endpoint takes 3 arguments: user - user name to perform the operation as cluster - name of the druid cluster config - configuration stored in json that contains: name: druid datasource name dimensions: list of the dimensions, they become druid columns with the type STRING metrics_spec: list of metrics (dictionary). Metric consists of 2 attributes: type and name. Type can be count, etc. `count` type is stored internally as longSum other fields will be ignored. Example: { 'name': 'test_click', 'metrics_spec': [{'type': 'count', 'name': 'count'}], 'dimensions': ['affiliate_id', 'campaign', 'first_seen'] } """ payload = request.get_json(force=True) druid_config = payload['config'] user_name = payload['user'] cluster_name = payload['cluster'] user = security_manager.find_user(username=user_name) DruidDatasource = ConnectorRegistry.sources['druid'] DruidCluster = DruidDatasource.cluster_class if not user: err_msg = __("Can't find User '%(name)s', please ask your admin " 'to create one.', name=user_name) logging.error(err_msg) return json_error_response(err_msg) cluster = db.session.query(DruidCluster).filter_by( cluster_name=cluster_name).first() if not cluster: err_msg = __("Can't find DruidCluster with cluster_name = " "'%(name)s'", name=cluster_name) logging.error(err_msg) return json_error_response(err_msg) try: DruidDatasource.sync_to_db_from_config( druid_config, user, cluster) except Exception as e: logging.exception(utils.error_msg_from_exception(e)) return json_error_response(utils.error_msg_from_exception(e)) return Response(status=201)
python
def sync_druid_source(self): """Syncs the druid datasource in main db with the provided config. The endpoint takes 3 arguments: user - user name to perform the operation as cluster - name of the druid cluster config - configuration stored in json that contains: name: druid datasource name dimensions: list of the dimensions, they become druid columns with the type STRING metrics_spec: list of metrics (dictionary). Metric consists of 2 attributes: type and name. Type can be count, etc. `count` type is stored internally as longSum other fields will be ignored. Example: { 'name': 'test_click', 'metrics_spec': [{'type': 'count', 'name': 'count'}], 'dimensions': ['affiliate_id', 'campaign', 'first_seen'] } """ payload = request.get_json(force=True) druid_config = payload['config'] user_name = payload['user'] cluster_name = payload['cluster'] user = security_manager.find_user(username=user_name) DruidDatasource = ConnectorRegistry.sources['druid'] DruidCluster = DruidDatasource.cluster_class if not user: err_msg = __("Can't find User '%(name)s', please ask your admin " 'to create one.', name=user_name) logging.error(err_msg) return json_error_response(err_msg) cluster = db.session.query(DruidCluster).filter_by( cluster_name=cluster_name).first() if not cluster: err_msg = __("Can't find DruidCluster with cluster_name = " "'%(name)s'", name=cluster_name) logging.error(err_msg) return json_error_response(err_msg) try: DruidDatasource.sync_to_db_from_config( druid_config, user, cluster) except Exception as e: logging.exception(utils.error_msg_from_exception(e)) return json_error_response(utils.error_msg_from_exception(e)) return Response(status=201)
[ "def", "sync_druid_source", "(", "self", ")", ":", "payload", "=", "request", ".", "get_json", "(", "force", "=", "True", ")", "druid_config", "=", "payload", "[", "'config'", "]", "user_name", "=", "payload", "[", "'user'", "]", "cluster_name", "=", "payload", "[", "'cluster'", "]", "user", "=", "security_manager", ".", "find_user", "(", "username", "=", "user_name", ")", "DruidDatasource", "=", "ConnectorRegistry", ".", "sources", "[", "'druid'", "]", "DruidCluster", "=", "DruidDatasource", ".", "cluster_class", "if", "not", "user", ":", "err_msg", "=", "__", "(", "\"Can't find User '%(name)s', please ask your admin \"", "'to create one.'", ",", "name", "=", "user_name", ")", "logging", ".", "error", "(", "err_msg", ")", "return", "json_error_response", "(", "err_msg", ")", "cluster", "=", "db", ".", "session", ".", "query", "(", "DruidCluster", ")", ".", "filter_by", "(", "cluster_name", "=", "cluster_name", ")", ".", "first", "(", ")", "if", "not", "cluster", ":", "err_msg", "=", "__", "(", "\"Can't find DruidCluster with cluster_name = \"", "\"'%(name)s'\"", ",", "name", "=", "cluster_name", ")", "logging", ".", "error", "(", "err_msg", ")", "return", "json_error_response", "(", "err_msg", ")", "try", ":", "DruidDatasource", ".", "sync_to_db_from_config", "(", "druid_config", ",", "user", ",", "cluster", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "utils", ".", "error_msg_from_exception", "(", "e", ")", ")", "return", "json_error_response", "(", "utils", ".", "error_msg_from_exception", "(", "e", ")", ")", "return", "Response", "(", "status", "=", "201", ")" ]
Syncs the druid datasource in main db with the provided config. The endpoint takes 3 arguments: user - user name to perform the operation as cluster - name of the druid cluster config - configuration stored in json that contains: name: druid datasource name dimensions: list of the dimensions, they become druid columns with the type STRING metrics_spec: list of metrics (dictionary). Metric consists of 2 attributes: type and name. Type can be count, etc. `count` type is stored internally as longSum other fields will be ignored. Example: { 'name': 'test_click', 'metrics_spec': [{'type': 'count', 'name': 'count'}], 'dimensions': ['affiliate_id', 'campaign', 'first_seen'] }
[ "Syncs", "the", "druid", "datasource", "in", "main", "db", "with", "the", "provided", "config", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2257-L2304
train
apache/incubator-superset
superset/views/core.py
Superset.cache_key_exist
def cache_key_exist(self, key): """Returns if a key from cache exist""" key_exist = True if cache.get(key) else False status = 200 if key_exist else 404 return json_success(json.dumps({'key_exist': key_exist}), status=status)
python
def cache_key_exist(self, key): """Returns if a key from cache exist""" key_exist = True if cache.get(key) else False status = 200 if key_exist else 404 return json_success(json.dumps({'key_exist': key_exist}), status=status)
[ "def", "cache_key_exist", "(", "self", ",", "key", ")", ":", "key_exist", "=", "True", "if", "cache", ".", "get", "(", "key", ")", "else", "False", "status", "=", "200", "if", "key_exist", "else", "404", "return", "json_success", "(", "json", ".", "dumps", "(", "{", "'key_exist'", ":", "key_exist", "}", ")", ",", "status", "=", "status", ")" ]
Returns if a key from cache exist
[ "Returns", "if", "a", "key", "from", "cache", "exist" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2455-L2460
train
apache/incubator-superset
superset/views/core.py
Superset.results
def results(self, key): """Serves a key off of the results backend""" if not results_backend: return json_error_response("Results backend isn't configured") read_from_results_backend_start = now_as_float() blob = results_backend.get(key) stats_logger.timing( 'sqllab.query.results_backend_read', now_as_float() - read_from_results_backend_start, ) if not blob: return json_error_response( 'Data could not be retrieved. ' 'You may want to re-run the query.', status=410, ) query = db.session.query(Query).filter_by(results_key=key).one() rejected_tables = security_manager.rejected_datasources( query.sql, query.database, query.schema) if rejected_tables: return json_error_response(security_manager.get_table_access_error_msg( '{}'.format(rejected_tables)), status=403) payload = utils.zlib_decompress_to_string(blob) display_limit = app.config.get('DEFAULT_SQLLAB_LIMIT', None) if display_limit: payload_json = json.loads(payload) payload_json['data'] = payload_json['data'][:display_limit] return json_success( json.dumps( payload_json, default=utils.json_iso_dttm_ser, ignore_nan=True, ), )
python
def results(self, key): """Serves a key off of the results backend""" if not results_backend: return json_error_response("Results backend isn't configured") read_from_results_backend_start = now_as_float() blob = results_backend.get(key) stats_logger.timing( 'sqllab.query.results_backend_read', now_as_float() - read_from_results_backend_start, ) if not blob: return json_error_response( 'Data could not be retrieved. ' 'You may want to re-run the query.', status=410, ) query = db.session.query(Query).filter_by(results_key=key).one() rejected_tables = security_manager.rejected_datasources( query.sql, query.database, query.schema) if rejected_tables: return json_error_response(security_manager.get_table_access_error_msg( '{}'.format(rejected_tables)), status=403) payload = utils.zlib_decompress_to_string(blob) display_limit = app.config.get('DEFAULT_SQLLAB_LIMIT', None) if display_limit: payload_json = json.loads(payload) payload_json['data'] = payload_json['data'][:display_limit] return json_success( json.dumps( payload_json, default=utils.json_iso_dttm_ser, ignore_nan=True, ), )
[ "def", "results", "(", "self", ",", "key", ")", ":", "if", "not", "results_backend", ":", "return", "json_error_response", "(", "\"Results backend isn't configured\"", ")", "read_from_results_backend_start", "=", "now_as_float", "(", ")", "blob", "=", "results_backend", ".", "get", "(", "key", ")", "stats_logger", ".", "timing", "(", "'sqllab.query.results_backend_read'", ",", "now_as_float", "(", ")", "-", "read_from_results_backend_start", ",", ")", "if", "not", "blob", ":", "return", "json_error_response", "(", "'Data could not be retrieved. '", "'You may want to re-run the query.'", ",", "status", "=", "410", ",", ")", "query", "=", "db", ".", "session", ".", "query", "(", "Query", ")", ".", "filter_by", "(", "results_key", "=", "key", ")", ".", "one", "(", ")", "rejected_tables", "=", "security_manager", ".", "rejected_datasources", "(", "query", ".", "sql", ",", "query", ".", "database", ",", "query", ".", "schema", ")", "if", "rejected_tables", ":", "return", "json_error_response", "(", "security_manager", ".", "get_table_access_error_msg", "(", "'{}'", ".", "format", "(", "rejected_tables", ")", ")", ",", "status", "=", "403", ")", "payload", "=", "utils", ".", "zlib_decompress_to_string", "(", "blob", ")", "display_limit", "=", "app", ".", "config", ".", "get", "(", "'DEFAULT_SQLLAB_LIMIT'", ",", "None", ")", "if", "display_limit", ":", "payload_json", "=", "json", ".", "loads", "(", "payload", ")", "payload_json", "[", "'data'", "]", "=", "payload_json", "[", "'data'", "]", "[", ":", "display_limit", "]", "return", "json_success", "(", "json", ".", "dumps", "(", "payload_json", ",", "default", "=", "utils", ".", "json_iso_dttm_ser", ",", "ignore_nan", "=", "True", ",", ")", ",", ")" ]
Serves a key off of the results backend
[ "Serves", "a", "key", "off", "of", "the", "results", "backend" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2465-L2501
train
apache/incubator-superset
superset/views/core.py
Superset.sql_json
def sql_json(self): """Runs arbitrary sql and returns and json""" async_ = request.form.get('runAsync') == 'true' sql = request.form.get('sql') database_id = request.form.get('database_id') schema = request.form.get('schema') or None template_params = json.loads( request.form.get('templateParams') or '{}') limit = int(request.form.get('queryLimit', 0)) if limit < 0: logging.warning( 'Invalid limit of {} specified. Defaulting to max limit.'.format(limit)) limit = 0 limit = limit or app.config.get('SQL_MAX_ROW') session = db.session() mydb = session.query(models.Database).filter_by(id=database_id).first() if not mydb: json_error_response( 'Database with id {} is missing.'.format(database_id)) rejected_tables = security_manager.rejected_datasources(sql, mydb, schema) if rejected_tables: return json_error_response( security_manager.get_table_access_error_msg(rejected_tables), link=security_manager.get_table_access_link(rejected_tables), status=403) session.commit() select_as_cta = request.form.get('select_as_cta') == 'true' tmp_table_name = request.form.get('tmp_table_name') if select_as_cta and mydb.force_ctas_schema: tmp_table_name = '{}.{}'.format( mydb.force_ctas_schema, tmp_table_name, ) client_id = request.form.get('client_id') or utils.shortid()[:10] query = Query( database_id=int(database_id), sql=sql, schema=schema, select_as_cta=select_as_cta, start_time=now_as_float(), tab_name=request.form.get('tab'), status=QueryStatus.PENDING if async_ else QueryStatus.RUNNING, sql_editor_id=request.form.get('sql_editor_id'), tmp_table_name=tmp_table_name, user_id=g.user.get_id() if g.user else None, client_id=client_id, ) session.add(query) session.flush() query_id = query.id session.commit() # shouldn't be necessary if not query_id: raise Exception(_('Query record was not created as expected.')) logging.info('Triggering query_id: {}'.format(query_id)) try: template_processor = get_template_processor( database=query.database, query=query) rendered_query = template_processor.process_template( query.sql, **template_params) except Exception as e: return json_error_response( 'Template rendering failed: {}'.format(utils.error_msg_from_exception(e))) # set LIMIT after template processing limits = [mydb.db_engine_spec.get_limit_from_sql(rendered_query), limit] query.limit = min(lim for lim in limits if lim is not None) # Async request. if async_: logging.info('Running query on a Celery worker') # Ignore the celery future object and the request may time out. try: sql_lab.get_sql_results.delay( query_id, rendered_query, return_results=False, store_results=not query.select_as_cta, user_name=g.user.username if g.user else None, start_time=now_as_float()) except Exception as e: logging.exception(e) msg = _( 'Failed to start remote query on a worker. ' 'Tell your administrator to verify the availability of ' 'the message queue.') query.status = QueryStatus.FAILED query.error_message = msg session.commit() return json_error_response('{}'.format(msg)) resp = json_success(json.dumps( {'query': query.to_dict()}, default=utils.json_int_dttm_ser, ignore_nan=True), status=202) session.commit() return resp # Sync request. try: timeout = config.get('SQLLAB_TIMEOUT') timeout_msg = ( f'The query exceeded the {timeout} seconds timeout.') with utils.timeout(seconds=timeout, error_message=timeout_msg): # pylint: disable=no-value-for-parameter data = sql_lab.get_sql_results( query_id, rendered_query, return_results=True, user_name=g.user.username if g.user else None) payload = json.dumps( data, default=utils.pessimistic_json_iso_dttm_ser, ignore_nan=True, encoding=None, ) except Exception as e: logging.exception(e) return json_error_response('{}'.format(e)) if data.get('status') == QueryStatus.FAILED: return json_error_response(payload=data) return json_success(payload)
python
def sql_json(self): """Runs arbitrary sql and returns and json""" async_ = request.form.get('runAsync') == 'true' sql = request.form.get('sql') database_id = request.form.get('database_id') schema = request.form.get('schema') or None template_params = json.loads( request.form.get('templateParams') or '{}') limit = int(request.form.get('queryLimit', 0)) if limit < 0: logging.warning( 'Invalid limit of {} specified. Defaulting to max limit.'.format(limit)) limit = 0 limit = limit or app.config.get('SQL_MAX_ROW') session = db.session() mydb = session.query(models.Database).filter_by(id=database_id).first() if not mydb: json_error_response( 'Database with id {} is missing.'.format(database_id)) rejected_tables = security_manager.rejected_datasources(sql, mydb, schema) if rejected_tables: return json_error_response( security_manager.get_table_access_error_msg(rejected_tables), link=security_manager.get_table_access_link(rejected_tables), status=403) session.commit() select_as_cta = request.form.get('select_as_cta') == 'true' tmp_table_name = request.form.get('tmp_table_name') if select_as_cta and mydb.force_ctas_schema: tmp_table_name = '{}.{}'.format( mydb.force_ctas_schema, tmp_table_name, ) client_id = request.form.get('client_id') or utils.shortid()[:10] query = Query( database_id=int(database_id), sql=sql, schema=schema, select_as_cta=select_as_cta, start_time=now_as_float(), tab_name=request.form.get('tab'), status=QueryStatus.PENDING if async_ else QueryStatus.RUNNING, sql_editor_id=request.form.get('sql_editor_id'), tmp_table_name=tmp_table_name, user_id=g.user.get_id() if g.user else None, client_id=client_id, ) session.add(query) session.flush() query_id = query.id session.commit() # shouldn't be necessary if not query_id: raise Exception(_('Query record was not created as expected.')) logging.info('Triggering query_id: {}'.format(query_id)) try: template_processor = get_template_processor( database=query.database, query=query) rendered_query = template_processor.process_template( query.sql, **template_params) except Exception as e: return json_error_response( 'Template rendering failed: {}'.format(utils.error_msg_from_exception(e))) # set LIMIT after template processing limits = [mydb.db_engine_spec.get_limit_from_sql(rendered_query), limit] query.limit = min(lim for lim in limits if lim is not None) # Async request. if async_: logging.info('Running query on a Celery worker') # Ignore the celery future object and the request may time out. try: sql_lab.get_sql_results.delay( query_id, rendered_query, return_results=False, store_results=not query.select_as_cta, user_name=g.user.username if g.user else None, start_time=now_as_float()) except Exception as e: logging.exception(e) msg = _( 'Failed to start remote query on a worker. ' 'Tell your administrator to verify the availability of ' 'the message queue.') query.status = QueryStatus.FAILED query.error_message = msg session.commit() return json_error_response('{}'.format(msg)) resp = json_success(json.dumps( {'query': query.to_dict()}, default=utils.json_int_dttm_ser, ignore_nan=True), status=202) session.commit() return resp # Sync request. try: timeout = config.get('SQLLAB_TIMEOUT') timeout_msg = ( f'The query exceeded the {timeout} seconds timeout.') with utils.timeout(seconds=timeout, error_message=timeout_msg): # pylint: disable=no-value-for-parameter data = sql_lab.get_sql_results( query_id, rendered_query, return_results=True, user_name=g.user.username if g.user else None) payload = json.dumps( data, default=utils.pessimistic_json_iso_dttm_ser, ignore_nan=True, encoding=None, ) except Exception as e: logging.exception(e) return json_error_response('{}'.format(e)) if data.get('status') == QueryStatus.FAILED: return json_error_response(payload=data) return json_success(payload)
[ "def", "sql_json", "(", "self", ")", ":", "async_", "=", "request", ".", "form", ".", "get", "(", "'runAsync'", ")", "==", "'true'", "sql", "=", "request", ".", "form", ".", "get", "(", "'sql'", ")", "database_id", "=", "request", ".", "form", ".", "get", "(", "'database_id'", ")", "schema", "=", "request", ".", "form", ".", "get", "(", "'schema'", ")", "or", "None", "template_params", "=", "json", ".", "loads", "(", "request", ".", "form", ".", "get", "(", "'templateParams'", ")", "or", "'{}'", ")", "limit", "=", "int", "(", "request", ".", "form", ".", "get", "(", "'queryLimit'", ",", "0", ")", ")", "if", "limit", "<", "0", ":", "logging", ".", "warning", "(", "'Invalid limit of {} specified. Defaulting to max limit.'", ".", "format", "(", "limit", ")", ")", "limit", "=", "0", "limit", "=", "limit", "or", "app", ".", "config", ".", "get", "(", "'SQL_MAX_ROW'", ")", "session", "=", "db", ".", "session", "(", ")", "mydb", "=", "session", ".", "query", "(", "models", ".", "Database", ")", ".", "filter_by", "(", "id", "=", "database_id", ")", ".", "first", "(", ")", "if", "not", "mydb", ":", "json_error_response", "(", "'Database with id {} is missing.'", ".", "format", "(", "database_id", ")", ")", "rejected_tables", "=", "security_manager", ".", "rejected_datasources", "(", "sql", ",", "mydb", ",", "schema", ")", "if", "rejected_tables", ":", "return", "json_error_response", "(", "security_manager", ".", "get_table_access_error_msg", "(", "rejected_tables", ")", ",", "link", "=", "security_manager", ".", "get_table_access_link", "(", "rejected_tables", ")", ",", "status", "=", "403", ")", "session", ".", "commit", "(", ")", "select_as_cta", "=", "request", ".", "form", ".", "get", "(", "'select_as_cta'", ")", "==", "'true'", "tmp_table_name", "=", "request", ".", "form", ".", "get", "(", "'tmp_table_name'", ")", "if", "select_as_cta", "and", "mydb", ".", "force_ctas_schema", ":", "tmp_table_name", "=", "'{}.{}'", ".", "format", "(", "mydb", ".", "force_ctas_schema", ",", "tmp_table_name", ",", ")", "client_id", "=", "request", ".", "form", ".", "get", "(", "'client_id'", ")", "or", "utils", ".", "shortid", "(", ")", "[", ":", "10", "]", "query", "=", "Query", "(", "database_id", "=", "int", "(", "database_id", ")", ",", "sql", "=", "sql", ",", "schema", "=", "schema", ",", "select_as_cta", "=", "select_as_cta", ",", "start_time", "=", "now_as_float", "(", ")", ",", "tab_name", "=", "request", ".", "form", ".", "get", "(", "'tab'", ")", ",", "status", "=", "QueryStatus", ".", "PENDING", "if", "async_", "else", "QueryStatus", ".", "RUNNING", ",", "sql_editor_id", "=", "request", ".", "form", ".", "get", "(", "'sql_editor_id'", ")", ",", "tmp_table_name", "=", "tmp_table_name", ",", "user_id", "=", "g", ".", "user", ".", "get_id", "(", ")", "if", "g", ".", "user", "else", "None", ",", "client_id", "=", "client_id", ",", ")", "session", ".", "add", "(", "query", ")", "session", ".", "flush", "(", ")", "query_id", "=", "query", ".", "id", "session", ".", "commit", "(", ")", "# shouldn't be necessary", "if", "not", "query_id", ":", "raise", "Exception", "(", "_", "(", "'Query record was not created as expected.'", ")", ")", "logging", ".", "info", "(", "'Triggering query_id: {}'", ".", "format", "(", "query_id", ")", ")", "try", ":", "template_processor", "=", "get_template_processor", "(", "database", "=", "query", ".", "database", ",", "query", "=", "query", ")", "rendered_query", "=", "template_processor", ".", "process_template", "(", "query", ".", "sql", ",", "*", "*", "template_params", ")", "except", "Exception", "as", "e", ":", "return", "json_error_response", "(", "'Template rendering failed: {}'", ".", "format", "(", "utils", ".", "error_msg_from_exception", "(", "e", ")", ")", ")", "# set LIMIT after template processing", "limits", "=", "[", "mydb", ".", "db_engine_spec", ".", "get_limit_from_sql", "(", "rendered_query", ")", ",", "limit", "]", "query", ".", "limit", "=", "min", "(", "lim", "for", "lim", "in", "limits", "if", "lim", "is", "not", "None", ")", "# Async request.", "if", "async_", ":", "logging", ".", "info", "(", "'Running query on a Celery worker'", ")", "# Ignore the celery future object and the request may time out.", "try", ":", "sql_lab", ".", "get_sql_results", ".", "delay", "(", "query_id", ",", "rendered_query", ",", "return_results", "=", "False", ",", "store_results", "=", "not", "query", ".", "select_as_cta", ",", "user_name", "=", "g", ".", "user", ".", "username", "if", "g", ".", "user", "else", "None", ",", "start_time", "=", "now_as_float", "(", ")", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "msg", "=", "_", "(", "'Failed to start remote query on a worker. '", "'Tell your administrator to verify the availability of '", "'the message queue.'", ")", "query", ".", "status", "=", "QueryStatus", ".", "FAILED", "query", ".", "error_message", "=", "msg", "session", ".", "commit", "(", ")", "return", "json_error_response", "(", "'{}'", ".", "format", "(", "msg", ")", ")", "resp", "=", "json_success", "(", "json", ".", "dumps", "(", "{", "'query'", ":", "query", ".", "to_dict", "(", ")", "}", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ",", "ignore_nan", "=", "True", ")", ",", "status", "=", "202", ")", "session", ".", "commit", "(", ")", "return", "resp", "# Sync request.", "try", ":", "timeout", "=", "config", ".", "get", "(", "'SQLLAB_TIMEOUT'", ")", "timeout_msg", "=", "(", "f'The query exceeded the {timeout} seconds timeout.'", ")", "with", "utils", ".", "timeout", "(", "seconds", "=", "timeout", ",", "error_message", "=", "timeout_msg", ")", ":", "# pylint: disable=no-value-for-parameter", "data", "=", "sql_lab", ".", "get_sql_results", "(", "query_id", ",", "rendered_query", ",", "return_results", "=", "True", ",", "user_name", "=", "g", ".", "user", ".", "username", "if", "g", ".", "user", "else", "None", ")", "payload", "=", "json", ".", "dumps", "(", "data", ",", "default", "=", "utils", ".", "pessimistic_json_iso_dttm_ser", ",", "ignore_nan", "=", "True", ",", "encoding", "=", "None", ",", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "return", "json_error_response", "(", "'{}'", ".", "format", "(", "e", ")", ")", "if", "data", ".", "get", "(", "'status'", ")", "==", "QueryStatus", ".", "FAILED", ":", "return", "json_error_response", "(", "payload", "=", "data", ")", "return", "json_success", "(", "payload", ")" ]
Runs arbitrary sql and returns and json
[ "Runs", "arbitrary", "sql", "and", "returns", "and", "json" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2522-L2649
train
apache/incubator-superset
superset/views/core.py
Superset.csv
def csv(self, client_id): """Download the query results as csv.""" logging.info('Exporting CSV file [{}]'.format(client_id)) query = ( db.session.query(Query) .filter_by(client_id=client_id) .one() ) rejected_tables = security_manager.rejected_datasources( query.sql, query.database, query.schema) if rejected_tables: flash( security_manager.get_table_access_error_msg('{}'.format(rejected_tables))) return redirect('/') blob = None if results_backend and query.results_key: logging.info( 'Fetching CSV from results backend ' '[{}]'.format(query.results_key)) blob = results_backend.get(query.results_key) if blob: logging.info('Decompressing') json_payload = utils.zlib_decompress_to_string(blob) obj = json.loads(json_payload) columns = [c['name'] for c in obj['columns']] df = pd.DataFrame.from_records(obj['data'], columns=columns) logging.info('Using pandas to convert to CSV') csv = df.to_csv(index=False, **config.get('CSV_EXPORT')) else: logging.info('Running a query to turn into CSV') sql = query.select_sql or query.executed_sql df = query.database.get_df(sql, query.schema) # TODO(bkyryliuk): add compression=gzip for big files. csv = df.to_csv(index=False, **config.get('CSV_EXPORT')) response = Response(csv, mimetype='text/csv') response.headers['Content-Disposition'] = f'attachment; filename={query.name}.csv' logging.info('Ready to return response') return response
python
def csv(self, client_id): """Download the query results as csv.""" logging.info('Exporting CSV file [{}]'.format(client_id)) query = ( db.session.query(Query) .filter_by(client_id=client_id) .one() ) rejected_tables = security_manager.rejected_datasources( query.sql, query.database, query.schema) if rejected_tables: flash( security_manager.get_table_access_error_msg('{}'.format(rejected_tables))) return redirect('/') blob = None if results_backend and query.results_key: logging.info( 'Fetching CSV from results backend ' '[{}]'.format(query.results_key)) blob = results_backend.get(query.results_key) if blob: logging.info('Decompressing') json_payload = utils.zlib_decompress_to_string(blob) obj = json.loads(json_payload) columns = [c['name'] for c in obj['columns']] df = pd.DataFrame.from_records(obj['data'], columns=columns) logging.info('Using pandas to convert to CSV') csv = df.to_csv(index=False, **config.get('CSV_EXPORT')) else: logging.info('Running a query to turn into CSV') sql = query.select_sql or query.executed_sql df = query.database.get_df(sql, query.schema) # TODO(bkyryliuk): add compression=gzip for big files. csv = df.to_csv(index=False, **config.get('CSV_EXPORT')) response = Response(csv, mimetype='text/csv') response.headers['Content-Disposition'] = f'attachment; filename={query.name}.csv' logging.info('Ready to return response') return response
[ "def", "csv", "(", "self", ",", "client_id", ")", ":", "logging", ".", "info", "(", "'Exporting CSV file [{}]'", ".", "format", "(", "client_id", ")", ")", "query", "=", "(", "db", ".", "session", ".", "query", "(", "Query", ")", ".", "filter_by", "(", "client_id", "=", "client_id", ")", ".", "one", "(", ")", ")", "rejected_tables", "=", "security_manager", ".", "rejected_datasources", "(", "query", ".", "sql", ",", "query", ".", "database", ",", "query", ".", "schema", ")", "if", "rejected_tables", ":", "flash", "(", "security_manager", ".", "get_table_access_error_msg", "(", "'{}'", ".", "format", "(", "rejected_tables", ")", ")", ")", "return", "redirect", "(", "'/'", ")", "blob", "=", "None", "if", "results_backend", "and", "query", ".", "results_key", ":", "logging", ".", "info", "(", "'Fetching CSV from results backend '", "'[{}]'", ".", "format", "(", "query", ".", "results_key", ")", ")", "blob", "=", "results_backend", ".", "get", "(", "query", ".", "results_key", ")", "if", "blob", ":", "logging", ".", "info", "(", "'Decompressing'", ")", "json_payload", "=", "utils", ".", "zlib_decompress_to_string", "(", "blob", ")", "obj", "=", "json", ".", "loads", "(", "json_payload", ")", "columns", "=", "[", "c", "[", "'name'", "]", "for", "c", "in", "obj", "[", "'columns'", "]", "]", "df", "=", "pd", ".", "DataFrame", ".", "from_records", "(", "obj", "[", "'data'", "]", ",", "columns", "=", "columns", ")", "logging", ".", "info", "(", "'Using pandas to convert to CSV'", ")", "csv", "=", "df", ".", "to_csv", "(", "index", "=", "False", ",", "*", "*", "config", ".", "get", "(", "'CSV_EXPORT'", ")", ")", "else", ":", "logging", ".", "info", "(", "'Running a query to turn into CSV'", ")", "sql", "=", "query", ".", "select_sql", "or", "query", ".", "executed_sql", "df", "=", "query", ".", "database", ".", "get_df", "(", "sql", ",", "query", ".", "schema", ")", "# TODO(bkyryliuk): add compression=gzip for big files.", "csv", "=", "df", ".", "to_csv", "(", "index", "=", "False", ",", "*", "*", "config", ".", "get", "(", "'CSV_EXPORT'", ")", ")", "response", "=", "Response", "(", "csv", ",", "mimetype", "=", "'text/csv'", ")", "response", ".", "headers", "[", "'Content-Disposition'", "]", "=", "f'attachment; filename={query.name}.csv'", "logging", ".", "info", "(", "'Ready to return response'", ")", "return", "response" ]
Download the query results as csv.
[ "Download", "the", "query", "results", "as", "csv", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2654-L2692
train
apache/incubator-superset
superset/views/core.py
Superset.queries
def queries(self, last_updated_ms): """Get the updated queries.""" stats_logger.incr('queries') if not g.user.get_id(): return json_error_response( 'Please login to access the queries.', status=403) # Unix time, milliseconds. last_updated_ms_int = int(float(last_updated_ms)) if last_updated_ms else 0 # UTC date time, same that is stored in the DB. last_updated_dt = utils.EPOCH + timedelta(seconds=last_updated_ms_int / 1000) sql_queries = ( db.session.query(Query) .filter( Query.user_id == g.user.get_id(), Query.changed_on >= last_updated_dt, ) .all() ) dict_queries = {q.client_id: q.to_dict() for q in sql_queries} now = int(round(time.time() * 1000)) unfinished_states = [ QueryStatus.PENDING, QueryStatus.RUNNING, ] queries_to_timeout = [ client_id for client_id, query_dict in dict_queries.items() if ( query_dict['state'] in unfinished_states and ( now - query_dict['startDttm'] > config.get('SQLLAB_ASYNC_TIME_LIMIT_SEC') * 1000 ) ) ] if queries_to_timeout: update(Query).where( and_( Query.user_id == g.user.get_id(), Query.client_id in queries_to_timeout, ), ).values(state=QueryStatus.TIMED_OUT) for client_id in queries_to_timeout: dict_queries[client_id]['status'] = QueryStatus.TIMED_OUT return json_success( json.dumps(dict_queries, default=utils.json_int_dttm_ser))
python
def queries(self, last_updated_ms): """Get the updated queries.""" stats_logger.incr('queries') if not g.user.get_id(): return json_error_response( 'Please login to access the queries.', status=403) # Unix time, milliseconds. last_updated_ms_int = int(float(last_updated_ms)) if last_updated_ms else 0 # UTC date time, same that is stored in the DB. last_updated_dt = utils.EPOCH + timedelta(seconds=last_updated_ms_int / 1000) sql_queries = ( db.session.query(Query) .filter( Query.user_id == g.user.get_id(), Query.changed_on >= last_updated_dt, ) .all() ) dict_queries = {q.client_id: q.to_dict() for q in sql_queries} now = int(round(time.time() * 1000)) unfinished_states = [ QueryStatus.PENDING, QueryStatus.RUNNING, ] queries_to_timeout = [ client_id for client_id, query_dict in dict_queries.items() if ( query_dict['state'] in unfinished_states and ( now - query_dict['startDttm'] > config.get('SQLLAB_ASYNC_TIME_LIMIT_SEC') * 1000 ) ) ] if queries_to_timeout: update(Query).where( and_( Query.user_id == g.user.get_id(), Query.client_id in queries_to_timeout, ), ).values(state=QueryStatus.TIMED_OUT) for client_id in queries_to_timeout: dict_queries[client_id]['status'] = QueryStatus.TIMED_OUT return json_success( json.dumps(dict_queries, default=utils.json_int_dttm_ser))
[ "def", "queries", "(", "self", ",", "last_updated_ms", ")", ":", "stats_logger", ".", "incr", "(", "'queries'", ")", "if", "not", "g", ".", "user", ".", "get_id", "(", ")", ":", "return", "json_error_response", "(", "'Please login to access the queries.'", ",", "status", "=", "403", ")", "# Unix time, milliseconds.", "last_updated_ms_int", "=", "int", "(", "float", "(", "last_updated_ms", ")", ")", "if", "last_updated_ms", "else", "0", "# UTC date time, same that is stored in the DB.", "last_updated_dt", "=", "utils", ".", "EPOCH", "+", "timedelta", "(", "seconds", "=", "last_updated_ms_int", "/", "1000", ")", "sql_queries", "=", "(", "db", ".", "session", ".", "query", "(", "Query", ")", ".", "filter", "(", "Query", ".", "user_id", "==", "g", ".", "user", ".", "get_id", "(", ")", ",", "Query", ".", "changed_on", ">=", "last_updated_dt", ",", ")", ".", "all", "(", ")", ")", "dict_queries", "=", "{", "q", ".", "client_id", ":", "q", ".", "to_dict", "(", ")", "for", "q", "in", "sql_queries", "}", "now", "=", "int", "(", "round", "(", "time", ".", "time", "(", ")", "*", "1000", ")", ")", "unfinished_states", "=", "[", "QueryStatus", ".", "PENDING", ",", "QueryStatus", ".", "RUNNING", ",", "]", "queries_to_timeout", "=", "[", "client_id", "for", "client_id", ",", "query_dict", "in", "dict_queries", ".", "items", "(", ")", "if", "(", "query_dict", "[", "'state'", "]", "in", "unfinished_states", "and", "(", "now", "-", "query_dict", "[", "'startDttm'", "]", ">", "config", ".", "get", "(", "'SQLLAB_ASYNC_TIME_LIMIT_SEC'", ")", "*", "1000", ")", ")", "]", "if", "queries_to_timeout", ":", "update", "(", "Query", ")", ".", "where", "(", "and_", "(", "Query", ".", "user_id", "==", "g", ".", "user", ".", "get_id", "(", ")", ",", "Query", ".", "client_id", "in", "queries_to_timeout", ",", ")", ",", ")", ".", "values", "(", "state", "=", "QueryStatus", ".", "TIMED_OUT", ")", "for", "client_id", "in", "queries_to_timeout", ":", "dict_queries", "[", "client_id", "]", "[", "'status'", "]", "=", "QueryStatus", ".", "TIMED_OUT", "return", "json_success", "(", "json", ".", "dumps", "(", "dict_queries", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ")" ]
Get the updated queries.
[ "Get", "the", "updated", "queries", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2714-L2766
train
apache/incubator-superset
superset/views/core.py
Superset.search_queries
def search_queries(self) -> Response: """ Search for previously run sqllab queries. Used for Sqllab Query Search page /superset/sqllab#search. Custom permission can_only_search_queries_owned restricts queries to only queries run by current user. :returns: Response with list of sql query dicts """ query = db.session.query(Query) if security_manager.can_only_access_owned_queries(): search_user_id = g.user.get_user_id() else: search_user_id = request.args.get('user_id') database_id = request.args.get('database_id') search_text = request.args.get('search_text') status = request.args.get('status') # From and To time stamp should be Epoch timestamp in seconds from_time = request.args.get('from') to_time = request.args.get('to') if search_user_id: # Filter on user_id query = query.filter(Query.user_id == search_user_id) if database_id: # Filter on db Id query = query.filter(Query.database_id == database_id) if status: # Filter on status query = query.filter(Query.status == status) if search_text: # Filter on search text query = query \ .filter(Query.sql.like('%{}%'.format(search_text))) if from_time: query = query.filter(Query.start_time > int(from_time)) if to_time: query = query.filter(Query.start_time < int(to_time)) query_limit = config.get('QUERY_SEARCH_LIMIT', 1000) sql_queries = ( query.order_by(Query.start_time.asc()) .limit(query_limit) .all() ) dict_queries = [q.to_dict() for q in sql_queries] return Response( json.dumps(dict_queries, default=utils.json_int_dttm_ser), status=200, mimetype='application/json')
python
def search_queries(self) -> Response: """ Search for previously run sqllab queries. Used for Sqllab Query Search page /superset/sqllab#search. Custom permission can_only_search_queries_owned restricts queries to only queries run by current user. :returns: Response with list of sql query dicts """ query = db.session.query(Query) if security_manager.can_only_access_owned_queries(): search_user_id = g.user.get_user_id() else: search_user_id = request.args.get('user_id') database_id = request.args.get('database_id') search_text = request.args.get('search_text') status = request.args.get('status') # From and To time stamp should be Epoch timestamp in seconds from_time = request.args.get('from') to_time = request.args.get('to') if search_user_id: # Filter on user_id query = query.filter(Query.user_id == search_user_id) if database_id: # Filter on db Id query = query.filter(Query.database_id == database_id) if status: # Filter on status query = query.filter(Query.status == status) if search_text: # Filter on search text query = query \ .filter(Query.sql.like('%{}%'.format(search_text))) if from_time: query = query.filter(Query.start_time > int(from_time)) if to_time: query = query.filter(Query.start_time < int(to_time)) query_limit = config.get('QUERY_SEARCH_LIMIT', 1000) sql_queries = ( query.order_by(Query.start_time.asc()) .limit(query_limit) .all() ) dict_queries = [q.to_dict() for q in sql_queries] return Response( json.dumps(dict_queries, default=utils.json_int_dttm_ser), status=200, mimetype='application/json')
[ "def", "search_queries", "(", "self", ")", "->", "Response", ":", "query", "=", "db", ".", "session", ".", "query", "(", "Query", ")", "if", "security_manager", ".", "can_only_access_owned_queries", "(", ")", ":", "search_user_id", "=", "g", ".", "user", ".", "get_user_id", "(", ")", "else", ":", "search_user_id", "=", "request", ".", "args", ".", "get", "(", "'user_id'", ")", "database_id", "=", "request", ".", "args", ".", "get", "(", "'database_id'", ")", "search_text", "=", "request", ".", "args", ".", "get", "(", "'search_text'", ")", "status", "=", "request", ".", "args", ".", "get", "(", "'status'", ")", "# From and To time stamp should be Epoch timestamp in seconds", "from_time", "=", "request", ".", "args", ".", "get", "(", "'from'", ")", "to_time", "=", "request", ".", "args", ".", "get", "(", "'to'", ")", "if", "search_user_id", ":", "# Filter on user_id", "query", "=", "query", ".", "filter", "(", "Query", ".", "user_id", "==", "search_user_id", ")", "if", "database_id", ":", "# Filter on db Id", "query", "=", "query", ".", "filter", "(", "Query", ".", "database_id", "==", "database_id", ")", "if", "status", ":", "# Filter on status", "query", "=", "query", ".", "filter", "(", "Query", ".", "status", "==", "status", ")", "if", "search_text", ":", "# Filter on search text", "query", "=", "query", ".", "filter", "(", "Query", ".", "sql", ".", "like", "(", "'%{}%'", ".", "format", "(", "search_text", ")", ")", ")", "if", "from_time", ":", "query", "=", "query", ".", "filter", "(", "Query", ".", "start_time", ">", "int", "(", "from_time", ")", ")", "if", "to_time", ":", "query", "=", "query", ".", "filter", "(", "Query", ".", "start_time", "<", "int", "(", "to_time", ")", ")", "query_limit", "=", "config", ".", "get", "(", "'QUERY_SEARCH_LIMIT'", ",", "1000", ")", "sql_queries", "=", "(", "query", ".", "order_by", "(", "Query", ".", "start_time", ".", "asc", "(", ")", ")", ".", "limit", "(", "query_limit", ")", ".", "all", "(", ")", ")", "dict_queries", "=", "[", "q", ".", "to_dict", "(", ")", "for", "q", "in", "sql_queries", "]", "return", "Response", "(", "json", ".", "dumps", "(", "dict_queries", ",", "default", "=", "utils", ".", "json_int_dttm_ser", ")", ",", "status", "=", "200", ",", "mimetype", "=", "'application/json'", ")" ]
Search for previously run sqllab queries. Used for Sqllab Query Search page /superset/sqllab#search. Custom permission can_only_search_queries_owned restricts queries to only queries run by current user. :returns: Response with list of sql query dicts
[ "Search", "for", "previously", "run", "sqllab", "queries", ".", "Used", "for", "Sqllab", "Query", "Search", "page", "/", "superset", "/", "sqllab#search", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2771-L2828
train
apache/incubator-superset
superset/views/core.py
Superset.welcome
def welcome(self): """Personalized welcome page""" if not g.user or not g.user.get_id(): return redirect(appbuilder.get_url_for_login) welcome_dashboard_id = ( db.session .query(UserAttribute.welcome_dashboard_id) .filter_by(user_id=g.user.get_id()) .scalar() ) if welcome_dashboard_id: return self.dashboard(str(welcome_dashboard_id)) payload = { 'user': bootstrap_user_data(), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', entry='welcome', title='Superset', bootstrap_data=json.dumps(payload, default=utils.json_iso_dttm_ser), )
python
def welcome(self): """Personalized welcome page""" if not g.user or not g.user.get_id(): return redirect(appbuilder.get_url_for_login) welcome_dashboard_id = ( db.session .query(UserAttribute.welcome_dashboard_id) .filter_by(user_id=g.user.get_id()) .scalar() ) if welcome_dashboard_id: return self.dashboard(str(welcome_dashboard_id)) payload = { 'user': bootstrap_user_data(), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', entry='welcome', title='Superset', bootstrap_data=json.dumps(payload, default=utils.json_iso_dttm_ser), )
[ "def", "welcome", "(", "self", ")", ":", "if", "not", "g", ".", "user", "or", "not", "g", ".", "user", ".", "get_id", "(", ")", ":", "return", "redirect", "(", "appbuilder", ".", "get_url_for_login", ")", "welcome_dashboard_id", "=", "(", "db", ".", "session", ".", "query", "(", "UserAttribute", ".", "welcome_dashboard_id", ")", ".", "filter_by", "(", "user_id", "=", "g", ".", "user", ".", "get_id", "(", ")", ")", ".", "scalar", "(", ")", ")", "if", "welcome_dashboard_id", ":", "return", "self", ".", "dashboard", "(", "str", "(", "welcome_dashboard_id", ")", ")", "payload", "=", "{", "'user'", ":", "bootstrap_user_data", "(", ")", ",", "'common'", ":", "self", ".", "common_bootsrap_payload", "(", ")", ",", "}", "return", "self", ".", "render_template", "(", "'superset/basic.html'", ",", "entry", "=", "'welcome'", ",", "title", "=", "'Superset'", ",", "bootstrap_data", "=", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_iso_dttm_ser", ")", ",", ")" ]
Personalized welcome page
[ "Personalized", "welcome", "page" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2838-L2862
train
apache/incubator-superset
superset/views/core.py
Superset.profile
def profile(self, username): """User profile page""" if not username and g.user: username = g.user.username payload = { 'user': bootstrap_user_data(username, include_perms=True), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', title=_("%(user)s's profile", user=username), entry='profile', bootstrap_data=json.dumps(payload, default=utils.json_iso_dttm_ser), )
python
def profile(self, username): """User profile page""" if not username and g.user: username = g.user.username payload = { 'user': bootstrap_user_data(username, include_perms=True), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', title=_("%(user)s's profile", user=username), entry='profile', bootstrap_data=json.dumps(payload, default=utils.json_iso_dttm_ser), )
[ "def", "profile", "(", "self", ",", "username", ")", ":", "if", "not", "username", "and", "g", ".", "user", ":", "username", "=", "g", ".", "user", ".", "username", "payload", "=", "{", "'user'", ":", "bootstrap_user_data", "(", "username", ",", "include_perms", "=", "True", ")", ",", "'common'", ":", "self", ".", "common_bootsrap_payload", "(", ")", ",", "}", "return", "self", ".", "render_template", "(", "'superset/basic.html'", ",", "title", "=", "_", "(", "\"%(user)s's profile\"", ",", "user", "=", "username", ")", ",", "entry", "=", "'profile'", ",", "bootstrap_data", "=", "json", ".", "dumps", "(", "payload", ",", "default", "=", "utils", ".", "json_iso_dttm_ser", ")", ",", ")" ]
User profile page
[ "User", "profile", "page" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2866-L2881
train
apache/incubator-superset
superset/views/core.py
Superset.sqllab
def sqllab(self): """SQL Editor""" d = { 'defaultDbId': config.get('SQLLAB_DEFAULT_DBID'), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', entry='sqllab', bootstrap_data=json.dumps(d, default=utils.json_iso_dttm_ser), )
python
def sqllab(self): """SQL Editor""" d = { 'defaultDbId': config.get('SQLLAB_DEFAULT_DBID'), 'common': self.common_bootsrap_payload(), } return self.render_template( 'superset/basic.html', entry='sqllab', bootstrap_data=json.dumps(d, default=utils.json_iso_dttm_ser), )
[ "def", "sqllab", "(", "self", ")", ":", "d", "=", "{", "'defaultDbId'", ":", "config", ".", "get", "(", "'SQLLAB_DEFAULT_DBID'", ")", ",", "'common'", ":", "self", ".", "common_bootsrap_payload", "(", ")", ",", "}", "return", "self", ".", "render_template", "(", "'superset/basic.html'", ",", "entry", "=", "'sqllab'", ",", "bootstrap_data", "=", "json", ".", "dumps", "(", "d", ",", "default", "=", "utils", ".", "json_iso_dttm_ser", ")", ",", ")" ]
SQL Editor
[ "SQL", "Editor" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2885-L2895
train
apache/incubator-superset
superset/views/core.py
Superset.slice_query
def slice_query(self, slice_id): """ This method exposes an API endpoint to get the database query string for this slice """ viz_obj = get_viz(slice_id) security_manager.assert_datasource_permission(viz_obj.datasource) return self.get_query_string_response(viz_obj)
python
def slice_query(self, slice_id): """ This method exposes an API endpoint to get the database query string for this slice """ viz_obj = get_viz(slice_id) security_manager.assert_datasource_permission(viz_obj.datasource) return self.get_query_string_response(viz_obj)
[ "def", "slice_query", "(", "self", ",", "slice_id", ")", ":", "viz_obj", "=", "get_viz", "(", "slice_id", ")", "security_manager", ".", "assert_datasource_permission", "(", "viz_obj", ".", "datasource", ")", "return", "self", ".", "get_query_string_response", "(", "viz_obj", ")" ]
This method exposes an API endpoint to get the database query string for this slice
[ "This", "method", "exposes", "an", "API", "endpoint", "to", "get", "the", "database", "query", "string", "for", "this", "slice" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2901-L2908
train
apache/incubator-superset
superset/views/core.py
Superset.schemas_access_for_csv_upload
def schemas_access_for_csv_upload(self): """ This method exposes an API endpoint to get the schema access control settings for csv upload in this database """ if not request.args.get('db_id'): return json_error_response( 'No database is allowed for your csv upload') db_id = int(request.args.get('db_id')) database = ( db.session .query(models.Database) .filter_by(id=db_id) .one() ) try: schemas_allowed = database.get_schema_access_for_csv_upload() if (security_manager.database_access(database) or security_manager.all_datasource_access()): return self.json_response(schemas_allowed) # the list schemas_allowed should not be empty here # and the list schemas_allowed_processed returned from security_manager # should not be empty either, # otherwise the database should have been filtered out # in CsvToDatabaseForm schemas_allowed_processed = security_manager.schemas_accessible_by_user( database, schemas_allowed, False) return self.json_response(schemas_allowed_processed) except Exception: return json_error_response(( 'Failed to fetch schemas allowed for csv upload in this database! ' 'Please contact Superset Admin!\n\n' 'The error message returned was:\n{}').format(traceback.format_exc()))
python
def schemas_access_for_csv_upload(self): """ This method exposes an API endpoint to get the schema access control settings for csv upload in this database """ if not request.args.get('db_id'): return json_error_response( 'No database is allowed for your csv upload') db_id = int(request.args.get('db_id')) database = ( db.session .query(models.Database) .filter_by(id=db_id) .one() ) try: schemas_allowed = database.get_schema_access_for_csv_upload() if (security_manager.database_access(database) or security_manager.all_datasource_access()): return self.json_response(schemas_allowed) # the list schemas_allowed should not be empty here # and the list schemas_allowed_processed returned from security_manager # should not be empty either, # otherwise the database should have been filtered out # in CsvToDatabaseForm schemas_allowed_processed = security_manager.schemas_accessible_by_user( database, schemas_allowed, False) return self.json_response(schemas_allowed_processed) except Exception: return json_error_response(( 'Failed to fetch schemas allowed for csv upload in this database! ' 'Please contact Superset Admin!\n\n' 'The error message returned was:\n{}').format(traceback.format_exc()))
[ "def", "schemas_access_for_csv_upload", "(", "self", ")", ":", "if", "not", "request", ".", "args", ".", "get", "(", "'db_id'", ")", ":", "return", "json_error_response", "(", "'No database is allowed for your csv upload'", ")", "db_id", "=", "int", "(", "request", ".", "args", ".", "get", "(", "'db_id'", ")", ")", "database", "=", "(", "db", ".", "session", ".", "query", "(", "models", ".", "Database", ")", ".", "filter_by", "(", "id", "=", "db_id", ")", ".", "one", "(", ")", ")", "try", ":", "schemas_allowed", "=", "database", ".", "get_schema_access_for_csv_upload", "(", ")", "if", "(", "security_manager", ".", "database_access", "(", "database", ")", "or", "security_manager", ".", "all_datasource_access", "(", ")", ")", ":", "return", "self", ".", "json_response", "(", "schemas_allowed", ")", "# the list schemas_allowed should not be empty here", "# and the list schemas_allowed_processed returned from security_manager", "# should not be empty either,", "# otherwise the database should have been filtered out", "# in CsvToDatabaseForm", "schemas_allowed_processed", "=", "security_manager", ".", "schemas_accessible_by_user", "(", "database", ",", "schemas_allowed", ",", "False", ")", "return", "self", ".", "json_response", "(", "schemas_allowed_processed", ")", "except", "Exception", ":", "return", "json_error_response", "(", "(", "'Failed to fetch schemas allowed for csv upload in this database! '", "'Please contact Superset Admin!\\n\\n'", "'The error message returned was:\\n{}'", ")", ".", "format", "(", "traceback", ".", "format_exc", "(", ")", ")", ")" ]
This method exposes an API endpoint to get the schema access control settings for csv upload in this database
[ "This", "method", "exposes", "an", "API", "endpoint", "to", "get", "the", "schema", "access", "control", "settings", "for", "csv", "upload", "in", "this", "database" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/views/core.py#L2913-L2946
train
apache/incubator-superset
superset/utils/decorators.py
stats_timing
def stats_timing(stats_key, stats_logger): """Provide a transactional scope around a series of operations.""" start_ts = now_as_float() try: yield start_ts except Exception as e: raise e finally: stats_logger.timing(stats_key, now_as_float() - start_ts)
python
def stats_timing(stats_key, stats_logger): """Provide a transactional scope around a series of operations.""" start_ts = now_as_float() try: yield start_ts except Exception as e: raise e finally: stats_logger.timing(stats_key, now_as_float() - start_ts)
[ "def", "stats_timing", "(", "stats_key", ",", "stats_logger", ")", ":", "start_ts", "=", "now_as_float", "(", ")", "try", ":", "yield", "start_ts", "except", "Exception", "as", "e", ":", "raise", "e", "finally", ":", "stats_logger", ".", "timing", "(", "stats_key", ",", "now_as_float", "(", ")", "-", "start_ts", ")" ]
Provide a transactional scope around a series of operations.
[ "Provide", "a", "transactional", "scope", "around", "a", "series", "of", "operations", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/decorators.py#L35-L43
train
apache/incubator-superset
superset/utils/decorators.py
etag_cache
def etag_cache(max_age, check_perms=bool): """ A decorator for caching views and handling etag conditional requests. The decorator adds headers to GET requests that help with caching: Last- Modified, Expires and ETag. It also handles conditional requests, when the client send an If-Matches header. If a cache is set, the decorator will cache GET responses, bypassing the dataframe serialization. POST requests will still benefit from the dataframe cache for requests that produce the same SQL. """ def decorator(f): @wraps(f) def wrapper(*args, **kwargs): # check if the user can access the resource check_perms(*args, **kwargs) # for POST requests we can't set cache headers, use the response # cache nor use conditional requests; this will still use the # dataframe cache in `superset/viz.py`, though. if request.method == 'POST': return f(*args, **kwargs) response = None if cache: try: # build the cache key from the function arguments and any # other additional GET arguments (like `form_data`, eg). key_args = list(args) key_kwargs = kwargs.copy() key_kwargs.update(request.args) cache_key = wrapper.make_cache_key(f, *key_args, **key_kwargs) response = cache.get(cache_key) except Exception: # pylint: disable=broad-except if app.debug: raise logging.exception('Exception possibly due to cache backend.') # if no response was cached, compute it using the wrapped function if response is None: response = f(*args, **kwargs) # add headers for caching: Last Modified, Expires and ETag response.cache_control.public = True response.last_modified = datetime.utcnow() expiration = max_age if max_age != 0 else FAR_FUTURE response.expires = \ response.last_modified + timedelta(seconds=expiration) response.add_etag() # if we have a cache, store the response from the request if cache: try: cache.set(cache_key, response, timeout=max_age) except Exception: # pylint: disable=broad-except if app.debug: raise logging.exception('Exception possibly due to cache backend.') return response.make_conditional(request) if cache: wrapper.uncached = f wrapper.cache_timeout = max_age wrapper.make_cache_key = \ cache._memoize_make_cache_key( # pylint: disable=protected-access make_name=None, timeout=max_age) return wrapper return decorator
python
def etag_cache(max_age, check_perms=bool): """ A decorator for caching views and handling etag conditional requests. The decorator adds headers to GET requests that help with caching: Last- Modified, Expires and ETag. It also handles conditional requests, when the client send an If-Matches header. If a cache is set, the decorator will cache GET responses, bypassing the dataframe serialization. POST requests will still benefit from the dataframe cache for requests that produce the same SQL. """ def decorator(f): @wraps(f) def wrapper(*args, **kwargs): # check if the user can access the resource check_perms(*args, **kwargs) # for POST requests we can't set cache headers, use the response # cache nor use conditional requests; this will still use the # dataframe cache in `superset/viz.py`, though. if request.method == 'POST': return f(*args, **kwargs) response = None if cache: try: # build the cache key from the function arguments and any # other additional GET arguments (like `form_data`, eg). key_args = list(args) key_kwargs = kwargs.copy() key_kwargs.update(request.args) cache_key = wrapper.make_cache_key(f, *key_args, **key_kwargs) response = cache.get(cache_key) except Exception: # pylint: disable=broad-except if app.debug: raise logging.exception('Exception possibly due to cache backend.') # if no response was cached, compute it using the wrapped function if response is None: response = f(*args, **kwargs) # add headers for caching: Last Modified, Expires and ETag response.cache_control.public = True response.last_modified = datetime.utcnow() expiration = max_age if max_age != 0 else FAR_FUTURE response.expires = \ response.last_modified + timedelta(seconds=expiration) response.add_etag() # if we have a cache, store the response from the request if cache: try: cache.set(cache_key, response, timeout=max_age) except Exception: # pylint: disable=broad-except if app.debug: raise logging.exception('Exception possibly due to cache backend.') return response.make_conditional(request) if cache: wrapper.uncached = f wrapper.cache_timeout = max_age wrapper.make_cache_key = \ cache._memoize_make_cache_key( # pylint: disable=protected-access make_name=None, timeout=max_age) return wrapper return decorator
[ "def", "etag_cache", "(", "max_age", ",", "check_perms", "=", "bool", ")", ":", "def", "decorator", "(", "f", ")", ":", "@", "wraps", "(", "f", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# check if the user can access the resource", "check_perms", "(", "*", "args", ",", "*", "*", "kwargs", ")", "# for POST requests we can't set cache headers, use the response", "# cache nor use conditional requests; this will still use the", "# dataframe cache in `superset/viz.py`, though.", "if", "request", ".", "method", "==", "'POST'", ":", "return", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "response", "=", "None", "if", "cache", ":", "try", ":", "# build the cache key from the function arguments and any", "# other additional GET arguments (like `form_data`, eg).", "key_args", "=", "list", "(", "args", ")", "key_kwargs", "=", "kwargs", ".", "copy", "(", ")", "key_kwargs", ".", "update", "(", "request", ".", "args", ")", "cache_key", "=", "wrapper", ".", "make_cache_key", "(", "f", ",", "*", "key_args", ",", "*", "*", "key_kwargs", ")", "response", "=", "cache", ".", "get", "(", "cache_key", ")", "except", "Exception", ":", "# pylint: disable=broad-except", "if", "app", ".", "debug", ":", "raise", "logging", ".", "exception", "(", "'Exception possibly due to cache backend.'", ")", "# if no response was cached, compute it using the wrapped function", "if", "response", "is", "None", ":", "response", "=", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "# add headers for caching: Last Modified, Expires and ETag", "response", ".", "cache_control", ".", "public", "=", "True", "response", ".", "last_modified", "=", "datetime", ".", "utcnow", "(", ")", "expiration", "=", "max_age", "if", "max_age", "!=", "0", "else", "FAR_FUTURE", "response", ".", "expires", "=", "response", ".", "last_modified", "+", "timedelta", "(", "seconds", "=", "expiration", ")", "response", ".", "add_etag", "(", ")", "# if we have a cache, store the response from the request", "if", "cache", ":", "try", ":", "cache", ".", "set", "(", "cache_key", ",", "response", ",", "timeout", "=", "max_age", ")", "except", "Exception", ":", "# pylint: disable=broad-except", "if", "app", ".", "debug", ":", "raise", "logging", ".", "exception", "(", "'Exception possibly due to cache backend.'", ")", "return", "response", ".", "make_conditional", "(", "request", ")", "if", "cache", ":", "wrapper", ".", "uncached", "=", "f", "wrapper", ".", "cache_timeout", "=", "max_age", "wrapper", ".", "make_cache_key", "=", "cache", ".", "_memoize_make_cache_key", "(", "# pylint: disable=protected-access", "make_name", "=", "None", ",", "timeout", "=", "max_age", ")", "return", "wrapper", "return", "decorator" ]
A decorator for caching views and handling etag conditional requests. The decorator adds headers to GET requests that help with caching: Last- Modified, Expires and ETag. It also handles conditional requests, when the client send an If-Matches header. If a cache is set, the decorator will cache GET responses, bypassing the dataframe serialization. POST requests will still benefit from the dataframe cache for requests that produce the same SQL.
[ "A", "decorator", "for", "caching", "views", "and", "handling", "etag", "conditional", "requests", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/decorators.py#L46-L118
train
apache/incubator-superset
superset/db_engine_specs.py
BaseEngineSpec.apply_limit_to_sql
def apply_limit_to_sql(cls, sql, limit, database): """Alters the SQL statement to apply a LIMIT clause""" if cls.limit_method == LimitMethod.WRAP_SQL: sql = sql.strip('\t\n ;') qry = ( select('*') .select_from( TextAsFrom(text(sql), ['*']).alias('inner_qry'), ) .limit(limit) ) return database.compile_sqla_query(qry) elif LimitMethod.FORCE_LIMIT: parsed_query = sql_parse.ParsedQuery(sql) sql = parsed_query.get_query_with_new_limit(limit) return sql
python
def apply_limit_to_sql(cls, sql, limit, database): """Alters the SQL statement to apply a LIMIT clause""" if cls.limit_method == LimitMethod.WRAP_SQL: sql = sql.strip('\t\n ;') qry = ( select('*') .select_from( TextAsFrom(text(sql), ['*']).alias('inner_qry'), ) .limit(limit) ) return database.compile_sqla_query(qry) elif LimitMethod.FORCE_LIMIT: parsed_query = sql_parse.ParsedQuery(sql) sql = parsed_query.get_query_with_new_limit(limit) return sql
[ "def", "apply_limit_to_sql", "(", "cls", ",", "sql", ",", "limit", ",", "database", ")", ":", "if", "cls", ".", "limit_method", "==", "LimitMethod", ".", "WRAP_SQL", ":", "sql", "=", "sql", ".", "strip", "(", "'\\t\\n ;'", ")", "qry", "=", "(", "select", "(", "'*'", ")", ".", "select_from", "(", "TextAsFrom", "(", "text", "(", "sql", ")", ",", "[", "'*'", "]", ")", ".", "alias", "(", "'inner_qry'", ")", ",", ")", ".", "limit", "(", "limit", ")", ")", "return", "database", ".", "compile_sqla_query", "(", "qry", ")", "elif", "LimitMethod", ".", "FORCE_LIMIT", ":", "parsed_query", "=", "sql_parse", ".", "ParsedQuery", "(", "sql", ")", "sql", "=", "parsed_query", ".", "get_query_with_new_limit", "(", "limit", ")", "return", "sql" ]
Alters the SQL statement to apply a LIMIT clause
[ "Alters", "the", "SQL", "statement", "to", "apply", "a", "LIMIT", "clause" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L183-L198
train
apache/incubator-superset
superset/db_engine_specs.py
BaseEngineSpec.modify_url_for_impersonation
def modify_url_for_impersonation(cls, url, impersonate_user, username): """ Modify the SQL Alchemy URL object with the user to impersonate if applicable. :param url: SQLAlchemy URL object :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username """ if impersonate_user is not None and username is not None: url.username = username
python
def modify_url_for_impersonation(cls, url, impersonate_user, username): """ Modify the SQL Alchemy URL object with the user to impersonate if applicable. :param url: SQLAlchemy URL object :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username """ if impersonate_user is not None and username is not None: url.username = username
[ "def", "modify_url_for_impersonation", "(", "cls", ",", "url", ",", "impersonate_user", ",", "username", ")", ":", "if", "impersonate_user", "is", "not", "None", "and", "username", "is", "not", "None", ":", "url", ".", "username", "=", "username" ]
Modify the SQL Alchemy URL object with the user to impersonate if applicable. :param url: SQLAlchemy URL object :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username
[ "Modify", "the", "SQL", "Alchemy", "URL", "object", "with", "the", "user", "to", "impersonate", "if", "applicable", ".", ":", "param", "url", ":", "SQLAlchemy", "URL", "object", ":", "param", "impersonate_user", ":", "Bool", "indicating", "if", "impersonation", "is", "enabled", ":", "param", "username", ":", "Effective", "username" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L395-L403
train
apache/incubator-superset
superset/db_engine_specs.py
BaseEngineSpec.make_label_compatible
def make_label_compatible(cls, label): """ Conditionally mutate and/or quote a sql column/expression label. If force_column_alias_quotes is set to True, return the label as a sqlalchemy.sql.elements.quoted_name object to ensure that the select query and query results have same case. Otherwise return the mutated label as a regular string. If maxmimum supported column name length is exceeded, generate a truncated label by calling truncate_label(). """ label_mutated = cls.mutate_label(label) if cls.max_column_name_length and len(label_mutated) > cls.max_column_name_length: label_mutated = cls.truncate_label(label) if cls.force_column_alias_quotes: label_mutated = quoted_name(label_mutated, True) return label_mutated
python
def make_label_compatible(cls, label): """ Conditionally mutate and/or quote a sql column/expression label. If force_column_alias_quotes is set to True, return the label as a sqlalchemy.sql.elements.quoted_name object to ensure that the select query and query results have same case. Otherwise return the mutated label as a regular string. If maxmimum supported column name length is exceeded, generate a truncated label by calling truncate_label(). """ label_mutated = cls.mutate_label(label) if cls.max_column_name_length and len(label_mutated) > cls.max_column_name_length: label_mutated = cls.truncate_label(label) if cls.force_column_alias_quotes: label_mutated = quoted_name(label_mutated, True) return label_mutated
[ "def", "make_label_compatible", "(", "cls", ",", "label", ")", ":", "label_mutated", "=", "cls", ".", "mutate_label", "(", "label", ")", "if", "cls", ".", "max_column_name_length", "and", "len", "(", "label_mutated", ")", ">", "cls", ".", "max_column_name_length", ":", "label_mutated", "=", "cls", ".", "truncate_label", "(", "label", ")", "if", "cls", ".", "force_column_alias_quotes", ":", "label_mutated", "=", "quoted_name", "(", "label_mutated", ",", "True", ")", "return", "label_mutated" ]
Conditionally mutate and/or quote a sql column/expression label. If force_column_alias_quotes is set to True, return the label as a sqlalchemy.sql.elements.quoted_name object to ensure that the select query and query results have same case. Otherwise return the mutated label as a regular string. If maxmimum supported column name length is exceeded, generate a truncated label by calling truncate_label().
[ "Conditionally", "mutate", "and", "/", "or", "quote", "a", "sql", "column", "/", "expression", "label", ".", "If", "force_column_alias_quotes", "is", "set", "to", "True", "return", "the", "label", "as", "a", "sqlalchemy", ".", "sql", ".", "elements", ".", "quoted_name", "object", "to", "ensure", "that", "the", "select", "query", "and", "query", "results", "have", "same", "case", ".", "Otherwise", "return", "the", "mutated", "label", "as", "a", "regular", "string", ".", "If", "maxmimum", "supported", "column", "name", "length", "is", "exceeded", "generate", "a", "truncated", "label", "by", "calling", "truncate_label", "()", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L424-L438
train
apache/incubator-superset
superset/db_engine_specs.py
BaseEngineSpec.truncate_label
def truncate_label(cls, label): """ In the case that a label exceeds the max length supported by the engine, this method is used to construct a deterministic and unique label based on an md5 hash. """ label = hashlib.md5(label.encode('utf-8')).hexdigest() # truncate hash if it exceeds max length if cls.max_column_name_length and len(label) > cls.max_column_name_length: label = label[:cls.max_column_name_length] return label
python
def truncate_label(cls, label): """ In the case that a label exceeds the max length supported by the engine, this method is used to construct a deterministic and unique label based on an md5 hash. """ label = hashlib.md5(label.encode('utf-8')).hexdigest() # truncate hash if it exceeds max length if cls.max_column_name_length and len(label) > cls.max_column_name_length: label = label[:cls.max_column_name_length] return label
[ "def", "truncate_label", "(", "cls", ",", "label", ")", ":", "label", "=", "hashlib", ".", "md5", "(", "label", ".", "encode", "(", "'utf-8'", ")", ")", ".", "hexdigest", "(", ")", "# truncate hash if it exceeds max length", "if", "cls", ".", "max_column_name_length", "and", "len", "(", "label", ")", ">", "cls", ".", "max_column_name_length", ":", "label", "=", "label", "[", ":", "cls", ".", "max_column_name_length", "]", "return", "label" ]
In the case that a label exceeds the max length supported by the engine, this method is used to construct a deterministic and unique label based on an md5 hash.
[ "In", "the", "case", "that", "a", "label", "exceeds", "the", "max", "length", "supported", "by", "the", "engine", "this", "method", "is", "used", "to", "construct", "a", "deterministic", "and", "unique", "label", "based", "on", "an", "md5", "hash", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L463-L473
train
apache/incubator-superset
superset/db_engine_specs.py
PostgresEngineSpec.get_table_names
def get_table_names(cls, inspector, schema): """Need to consider foreign tables for PostgreSQL""" tables = inspector.get_table_names(schema) tables.extend(inspector.get_foreign_table_names(schema)) return sorted(tables)
python
def get_table_names(cls, inspector, schema): """Need to consider foreign tables for PostgreSQL""" tables = inspector.get_table_names(schema) tables.extend(inspector.get_foreign_table_names(schema)) return sorted(tables)
[ "def", "get_table_names", "(", "cls", ",", "inspector", ",", "schema", ")", ":", "tables", "=", "inspector", ".", "get_table_names", "(", "schema", ")", "tables", ".", "extend", "(", "inspector", ".", "get_foreign_table_names", "(", "schema", ")", ")", "return", "sorted", "(", "tables", ")" ]
Need to consider foreign tables for PostgreSQL
[ "Need", "to", "consider", "foreign", "tables", "for", "PostgreSQL" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L522-L526
train
apache/incubator-superset
superset/db_engine_specs.py
PostgresEngineSpec.get_timestamp_column
def get_timestamp_column(expression, column_name): """Postgres is unable to identify mixed case column names unless they are quoted.""" if expression: return expression elif column_name.lower() != column_name: return f'"{column_name}"' return column_name
python
def get_timestamp_column(expression, column_name): """Postgres is unable to identify mixed case column names unless they are quoted.""" if expression: return expression elif column_name.lower() != column_name: return f'"{column_name}"' return column_name
[ "def", "get_timestamp_column", "(", "expression", ",", "column_name", ")", ":", "if", "expression", ":", "return", "expression", "elif", "column_name", ".", "lower", "(", ")", "!=", "column_name", ":", "return", "f'\"{column_name}\"'", "return", "column_name" ]
Postgres is unable to identify mixed case column names unless they are quoted.
[ "Postgres", "is", "unable", "to", "identify", "mixed", "case", "column", "names", "unless", "they", "are", "quoted", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L529-L536
train
apache/incubator-superset
superset/db_engine_specs.py
MySQLEngineSpec.extract_error_message
def extract_error_message(cls, e): """Extract error message for queries""" message = str(e) try: if isinstance(e.args, tuple) and len(e.args) > 1: message = e.args[1] except Exception: pass return message
python
def extract_error_message(cls, e): """Extract error message for queries""" message = str(e) try: if isinstance(e.args, tuple) and len(e.args) > 1: message = e.args[1] except Exception: pass return message
[ "def", "extract_error_message", "(", "cls", ",", "e", ")", ":", "message", "=", "str", "(", "e", ")", "try", ":", "if", "isinstance", "(", "e", ".", "args", ",", "tuple", ")", "and", "len", "(", "e", ".", "args", ")", ">", "1", ":", "message", "=", "e", ".", "args", "[", "1", "]", "except", "Exception", ":", "pass", "return", "message" ]
Extract error message for queries
[ "Extract", "error", "message", "for", "queries" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L775-L783
train
apache/incubator-superset
superset/db_engine_specs.py
PrestoEngineSpec.fetch_result_sets
def fetch_result_sets(cls, db, datasource_type): """Returns a list of tables [schema1.table1, schema2.table2, ...] Datasource_type can be 'table' or 'view'. Empty schema corresponds to the list of full names of the all tables or views: <schema>.<result_set_name>. """ result_set_df = db.get_df( """SELECT table_schema, table_name FROM INFORMATION_SCHEMA.{}S ORDER BY concat(table_schema, '.', table_name)""".format( datasource_type.upper(), ), None) result_sets = [] for unused, row in result_set_df.iterrows(): result_sets.append('{}.{}'.format( row['table_schema'], row['table_name'])) return result_sets
python
def fetch_result_sets(cls, db, datasource_type): """Returns a list of tables [schema1.table1, schema2.table2, ...] Datasource_type can be 'table' or 'view'. Empty schema corresponds to the list of full names of the all tables or views: <schema>.<result_set_name>. """ result_set_df = db.get_df( """SELECT table_schema, table_name FROM INFORMATION_SCHEMA.{}S ORDER BY concat(table_schema, '.', table_name)""".format( datasource_type.upper(), ), None) result_sets = [] for unused, row in result_set_df.iterrows(): result_sets.append('{}.{}'.format( row['table_schema'], row['table_name'])) return result_sets
[ "def", "fetch_result_sets", "(", "cls", ",", "db", ",", "datasource_type", ")", ":", "result_set_df", "=", "db", ".", "get_df", "(", "\"\"\"SELECT table_schema, table_name FROM INFORMATION_SCHEMA.{}S\n ORDER BY concat(table_schema, '.', table_name)\"\"\"", ".", "format", "(", "datasource_type", ".", "upper", "(", ")", ",", ")", ",", "None", ")", "result_sets", "=", "[", "]", "for", "unused", ",", "row", "in", "result_set_df", ".", "iterrows", "(", ")", ":", "result_sets", ".", "append", "(", "'{}.{}'", ".", "format", "(", "row", "[", "'table_schema'", "]", ",", "row", "[", "'table_name'", "]", ")", ")", "return", "result_sets" ]
Returns a list of tables [schema1.table1, schema2.table2, ...] Datasource_type can be 'table' or 'view'. Empty schema corresponds to the list of full names of the all tables or views: <schema>.<result_set_name>.
[ "Returns", "a", "list", "of", "tables", "[", "schema1", ".", "table1", "schema2", ".", "table2", "...", "]" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L844-L861
train
apache/incubator-superset
superset/db_engine_specs.py
PrestoEngineSpec.handle_cursor
def handle_cursor(cls, cursor, query, session): """Updates progress information""" logging.info('Polling the cursor for progress') polled = cursor.poll() # poll returns dict -- JSON status information or ``None`` # if the query is done # https://github.com/dropbox/PyHive/blob/ # b34bdbf51378b3979eaf5eca9e956f06ddc36ca0/pyhive/presto.py#L178 while polled: # Update the object and wait for the kill signal. stats = polled.get('stats', {}) query = session.query(type(query)).filter_by(id=query.id).one() if query.status in [QueryStatus.STOPPED, QueryStatus.TIMED_OUT]: cursor.cancel() break if stats: state = stats.get('state') # if already finished, then stop polling if state == 'FINISHED': break completed_splits = float(stats.get('completedSplits')) total_splits = float(stats.get('totalSplits')) if total_splits and completed_splits: progress = 100 * (completed_splits / total_splits) logging.info( 'Query progress: {} / {} ' 'splits'.format(completed_splits, total_splits)) if progress > query.progress: query.progress = progress session.commit() time.sleep(1) logging.info('Polling the cursor for progress') polled = cursor.poll()
python
def handle_cursor(cls, cursor, query, session): """Updates progress information""" logging.info('Polling the cursor for progress') polled = cursor.poll() # poll returns dict -- JSON status information or ``None`` # if the query is done # https://github.com/dropbox/PyHive/blob/ # b34bdbf51378b3979eaf5eca9e956f06ddc36ca0/pyhive/presto.py#L178 while polled: # Update the object and wait for the kill signal. stats = polled.get('stats', {}) query = session.query(type(query)).filter_by(id=query.id).one() if query.status in [QueryStatus.STOPPED, QueryStatus.TIMED_OUT]: cursor.cancel() break if stats: state = stats.get('state') # if already finished, then stop polling if state == 'FINISHED': break completed_splits = float(stats.get('completedSplits')) total_splits = float(stats.get('totalSplits')) if total_splits and completed_splits: progress = 100 * (completed_splits / total_splits) logging.info( 'Query progress: {} / {} ' 'splits'.format(completed_splits, total_splits)) if progress > query.progress: query.progress = progress session.commit() time.sleep(1) logging.info('Polling the cursor for progress') polled = cursor.poll()
[ "def", "handle_cursor", "(", "cls", ",", "cursor", ",", "query", ",", "session", ")", ":", "logging", ".", "info", "(", "'Polling the cursor for progress'", ")", "polled", "=", "cursor", ".", "poll", "(", ")", "# poll returns dict -- JSON status information or ``None``", "# if the query is done", "# https://github.com/dropbox/PyHive/blob/", "# b34bdbf51378b3979eaf5eca9e956f06ddc36ca0/pyhive/presto.py#L178", "while", "polled", ":", "# Update the object and wait for the kill signal.", "stats", "=", "polled", ".", "get", "(", "'stats'", ",", "{", "}", ")", "query", "=", "session", ".", "query", "(", "type", "(", "query", ")", ")", ".", "filter_by", "(", "id", "=", "query", ".", "id", ")", ".", "one", "(", ")", "if", "query", ".", "status", "in", "[", "QueryStatus", ".", "STOPPED", ",", "QueryStatus", ".", "TIMED_OUT", "]", ":", "cursor", ".", "cancel", "(", ")", "break", "if", "stats", ":", "state", "=", "stats", ".", "get", "(", "'state'", ")", "# if already finished, then stop polling", "if", "state", "==", "'FINISHED'", ":", "break", "completed_splits", "=", "float", "(", "stats", ".", "get", "(", "'completedSplits'", ")", ")", "total_splits", "=", "float", "(", "stats", ".", "get", "(", "'totalSplits'", ")", ")", "if", "total_splits", "and", "completed_splits", ":", "progress", "=", "100", "*", "(", "completed_splits", "/", "total_splits", ")", "logging", ".", "info", "(", "'Query progress: {} / {} '", "'splits'", ".", "format", "(", "completed_splits", ",", "total_splits", ")", ")", "if", "progress", ">", "query", ".", "progress", ":", "query", ".", "progress", "=", "progress", "session", ".", "commit", "(", ")", "time", ".", "sleep", "(", "1", ")", "logging", ".", "info", "(", "'Polling the cursor for progress'", ")", "polled", "=", "cursor", ".", "poll", "(", ")" ]
Updates progress information
[ "Updates", "progress", "information" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L884-L920
train
apache/incubator-superset
superset/db_engine_specs.py
PrestoEngineSpec._partition_query
def _partition_query( cls, table_name, limit=0, order_by=None, filters=None): """Returns a partition query :param table_name: the name of the table to get partitions from :type table_name: str :param limit: the number of partitions to be returned :type limit: int :param order_by: a list of tuples of field name and a boolean that determines if that field should be sorted in descending order :type order_by: list of (str, bool) tuples :param filters: dict of field name and filter value combinations """ limit_clause = 'LIMIT {}'.format(limit) if limit else '' order_by_clause = '' if order_by: l = [] # noqa: E741 for field, desc in order_by: l.append(field + ' DESC' if desc else '') order_by_clause = 'ORDER BY ' + ', '.join(l) where_clause = '' if filters: l = [] # noqa: E741 for field, value in filters.items(): l.append(f"{field} = '{value}'") where_clause = 'WHERE ' + ' AND '.join(l) sql = textwrap.dedent(f"""\ SELECT * FROM "{table_name}$partitions" {where_clause} {order_by_clause} {limit_clause} """) return sql
python
def _partition_query( cls, table_name, limit=0, order_by=None, filters=None): """Returns a partition query :param table_name: the name of the table to get partitions from :type table_name: str :param limit: the number of partitions to be returned :type limit: int :param order_by: a list of tuples of field name and a boolean that determines if that field should be sorted in descending order :type order_by: list of (str, bool) tuples :param filters: dict of field name and filter value combinations """ limit_clause = 'LIMIT {}'.format(limit) if limit else '' order_by_clause = '' if order_by: l = [] # noqa: E741 for field, desc in order_by: l.append(field + ' DESC' if desc else '') order_by_clause = 'ORDER BY ' + ', '.join(l) where_clause = '' if filters: l = [] # noqa: E741 for field, value in filters.items(): l.append(f"{field} = '{value}'") where_clause = 'WHERE ' + ' AND '.join(l) sql = textwrap.dedent(f"""\ SELECT * FROM "{table_name}$partitions" {where_clause} {order_by_clause} {limit_clause} """) return sql
[ "def", "_partition_query", "(", "cls", ",", "table_name", ",", "limit", "=", "0", ",", "order_by", "=", "None", ",", "filters", "=", "None", ")", ":", "limit_clause", "=", "'LIMIT {}'", ".", "format", "(", "limit", ")", "if", "limit", "else", "''", "order_by_clause", "=", "''", "if", "order_by", ":", "l", "=", "[", "]", "# noqa: E741", "for", "field", ",", "desc", "in", "order_by", ":", "l", ".", "append", "(", "field", "+", "' DESC'", "if", "desc", "else", "''", ")", "order_by_clause", "=", "'ORDER BY '", "+", "', '", ".", "join", "(", "l", ")", "where_clause", "=", "''", "if", "filters", ":", "l", "=", "[", "]", "# noqa: E741", "for", "field", ",", "value", "in", "filters", ".", "items", "(", ")", ":", "l", ".", "append", "(", "f\"{field} = '{value}'\"", ")", "where_clause", "=", "'WHERE '", "+", "' AND '", ".", "join", "(", "l", ")", "sql", "=", "textwrap", ".", "dedent", "(", "f\"\"\"\\\n SELECT * FROM \"{table_name}$partitions\"\n\n {where_clause}\n {order_by_clause}\n {limit_clause}\n \"\"\"", ")", "return", "sql" ]
Returns a partition query :param table_name: the name of the table to get partitions from :type table_name: str :param limit: the number of partitions to be returned :type limit: int :param order_by: a list of tuples of field name and a boolean that determines if that field should be sorted in descending order :type order_by: list of (str, bool) tuples :param filters: dict of field name and filter value combinations
[ "Returns", "a", "partition", "query" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L944-L980
train
apache/incubator-superset
superset/db_engine_specs.py
HiveEngineSpec.create_table_from_csv
def create_table_from_csv(form, table): """Uploads a csv file and creates a superset datasource in Hive.""" def convert_to_hive_type(col_type): """maps tableschema's types to hive types""" tableschema_to_hive_types = { 'boolean': 'BOOLEAN', 'integer': 'INT', 'number': 'DOUBLE', 'string': 'STRING', } return tableschema_to_hive_types.get(col_type, 'STRING') bucket_path = config['CSV_TO_HIVE_UPLOAD_S3_BUCKET'] if not bucket_path: logging.info('No upload bucket specified') raise Exception( 'No upload bucket specified. You can specify one in the config file.') table_name = form.name.data schema_name = form.schema.data if config.get('UPLOADED_CSV_HIVE_NAMESPACE'): if '.' in table_name or schema_name: raise Exception( "You can't specify a namespace. " 'All tables will be uploaded to the `{}` namespace'.format( config.get('HIVE_NAMESPACE'))) full_table_name = '{}.{}'.format( config.get('UPLOADED_CSV_HIVE_NAMESPACE'), table_name) else: if '.' in table_name and schema_name: raise Exception( "You can't specify a namespace both in the name of the table " 'and in the schema field. Please remove one') full_table_name = '{}.{}'.format( schema_name, table_name) if schema_name else table_name filename = form.csv_file.data.filename upload_prefix = config['CSV_TO_HIVE_UPLOAD_DIRECTORY'] upload_path = config['UPLOAD_FOLDER'] + \ secure_filename(filename) # Optional dependency from tableschema import Table # pylint: disable=import-error hive_table_schema = Table(upload_path).infer() column_name_and_type = [] for column_info in hive_table_schema['fields']: column_name_and_type.append( '`{}` {}'.format( column_info['name'], convert_to_hive_type(column_info['type']))) schema_definition = ', '.join(column_name_and_type) # Optional dependency import boto3 # pylint: disable=import-error s3 = boto3.client('s3') location = os.path.join('s3a://', bucket_path, upload_prefix, table_name) s3.upload_file( upload_path, bucket_path, os.path.join(upload_prefix, table_name, filename)) sql = f"""CREATE TABLE {full_table_name} ( {schema_definition} ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION '{location}' tblproperties ('skip.header.line.count'='1')""" logging.info(form.con.data) engine = create_engine(form.con.data.sqlalchemy_uri_decrypted) engine.execute(sql)
python
def create_table_from_csv(form, table): """Uploads a csv file and creates a superset datasource in Hive.""" def convert_to_hive_type(col_type): """maps tableschema's types to hive types""" tableschema_to_hive_types = { 'boolean': 'BOOLEAN', 'integer': 'INT', 'number': 'DOUBLE', 'string': 'STRING', } return tableschema_to_hive_types.get(col_type, 'STRING') bucket_path = config['CSV_TO_HIVE_UPLOAD_S3_BUCKET'] if not bucket_path: logging.info('No upload bucket specified') raise Exception( 'No upload bucket specified. You can specify one in the config file.') table_name = form.name.data schema_name = form.schema.data if config.get('UPLOADED_CSV_HIVE_NAMESPACE'): if '.' in table_name or schema_name: raise Exception( "You can't specify a namespace. " 'All tables will be uploaded to the `{}` namespace'.format( config.get('HIVE_NAMESPACE'))) full_table_name = '{}.{}'.format( config.get('UPLOADED_CSV_HIVE_NAMESPACE'), table_name) else: if '.' in table_name and schema_name: raise Exception( "You can't specify a namespace both in the name of the table " 'and in the schema field. Please remove one') full_table_name = '{}.{}'.format( schema_name, table_name) if schema_name else table_name filename = form.csv_file.data.filename upload_prefix = config['CSV_TO_HIVE_UPLOAD_DIRECTORY'] upload_path = config['UPLOAD_FOLDER'] + \ secure_filename(filename) # Optional dependency from tableschema import Table # pylint: disable=import-error hive_table_schema = Table(upload_path).infer() column_name_and_type = [] for column_info in hive_table_schema['fields']: column_name_and_type.append( '`{}` {}'.format( column_info['name'], convert_to_hive_type(column_info['type']))) schema_definition = ', '.join(column_name_and_type) # Optional dependency import boto3 # pylint: disable=import-error s3 = boto3.client('s3') location = os.path.join('s3a://', bucket_path, upload_prefix, table_name) s3.upload_file( upload_path, bucket_path, os.path.join(upload_prefix, table_name, filename)) sql = f"""CREATE TABLE {full_table_name} ( {schema_definition} ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION '{location}' tblproperties ('skip.header.line.count'='1')""" logging.info(form.con.data) engine = create_engine(form.con.data.sqlalchemy_uri_decrypted) engine.execute(sql)
[ "def", "create_table_from_csv", "(", "form", ",", "table", ")", ":", "def", "convert_to_hive_type", "(", "col_type", ")", ":", "\"\"\"maps tableschema's types to hive types\"\"\"", "tableschema_to_hive_types", "=", "{", "'boolean'", ":", "'BOOLEAN'", ",", "'integer'", ":", "'INT'", ",", "'number'", ":", "'DOUBLE'", ",", "'string'", ":", "'STRING'", ",", "}", "return", "tableschema_to_hive_types", ".", "get", "(", "col_type", ",", "'STRING'", ")", "bucket_path", "=", "config", "[", "'CSV_TO_HIVE_UPLOAD_S3_BUCKET'", "]", "if", "not", "bucket_path", ":", "logging", ".", "info", "(", "'No upload bucket specified'", ")", "raise", "Exception", "(", "'No upload bucket specified. You can specify one in the config file.'", ")", "table_name", "=", "form", ".", "name", ".", "data", "schema_name", "=", "form", ".", "schema", ".", "data", "if", "config", ".", "get", "(", "'UPLOADED_CSV_HIVE_NAMESPACE'", ")", ":", "if", "'.'", "in", "table_name", "or", "schema_name", ":", "raise", "Exception", "(", "\"You can't specify a namespace. \"", "'All tables will be uploaded to the `{}` namespace'", ".", "format", "(", "config", ".", "get", "(", "'HIVE_NAMESPACE'", ")", ")", ")", "full_table_name", "=", "'{}.{}'", ".", "format", "(", "config", ".", "get", "(", "'UPLOADED_CSV_HIVE_NAMESPACE'", ")", ",", "table_name", ")", "else", ":", "if", "'.'", "in", "table_name", "and", "schema_name", ":", "raise", "Exception", "(", "\"You can't specify a namespace both in the name of the table \"", "'and in the schema field. Please remove one'", ")", "full_table_name", "=", "'{}.{}'", ".", "format", "(", "schema_name", ",", "table_name", ")", "if", "schema_name", "else", "table_name", "filename", "=", "form", ".", "csv_file", ".", "data", ".", "filename", "upload_prefix", "=", "config", "[", "'CSV_TO_HIVE_UPLOAD_DIRECTORY'", "]", "upload_path", "=", "config", "[", "'UPLOAD_FOLDER'", "]", "+", "secure_filename", "(", "filename", ")", "# Optional dependency", "from", "tableschema", "import", "Table", "# pylint: disable=import-error", "hive_table_schema", "=", "Table", "(", "upload_path", ")", ".", "infer", "(", ")", "column_name_and_type", "=", "[", "]", "for", "column_info", "in", "hive_table_schema", "[", "'fields'", "]", ":", "column_name_and_type", ".", "append", "(", "'`{}` {}'", ".", "format", "(", "column_info", "[", "'name'", "]", ",", "convert_to_hive_type", "(", "column_info", "[", "'type'", "]", ")", ")", ")", "schema_definition", "=", "', '", ".", "join", "(", "column_name_and_type", ")", "# Optional dependency", "import", "boto3", "# pylint: disable=import-error", "s3", "=", "boto3", ".", "client", "(", "'s3'", ")", "location", "=", "os", ".", "path", ".", "join", "(", "'s3a://'", ",", "bucket_path", ",", "upload_prefix", ",", "table_name", ")", "s3", ".", "upload_file", "(", "upload_path", ",", "bucket_path", ",", "os", ".", "path", ".", "join", "(", "upload_prefix", ",", "table_name", ",", "filename", ")", ")", "sql", "=", "f\"\"\"CREATE TABLE {full_table_name} ( {schema_definition} )\n ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS\n TEXTFILE LOCATION '{location}'\n tblproperties ('skip.header.line.count'='1')\"\"\"", "logging", ".", "info", "(", "form", ".", "con", ".", "data", ")", "engine", "=", "create_engine", "(", "form", ".", "con", ".", "data", ".", "sqlalchemy_uri_decrypted", ")", "engine", ".", "execute", "(", "sql", ")" ]
Uploads a csv file and creates a superset datasource in Hive.
[ "Uploads", "a", "csv", "file", "and", "creates", "a", "superset", "datasource", "in", "Hive", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L1139-L1209
train
apache/incubator-superset
superset/db_engine_specs.py
HiveEngineSpec.handle_cursor
def handle_cursor(cls, cursor, query, session): """Updates progress information""" from pyhive import hive # pylint: disable=no-name-in-module unfinished_states = ( hive.ttypes.TOperationState.INITIALIZED_STATE, hive.ttypes.TOperationState.RUNNING_STATE, ) polled = cursor.poll() last_log_line = 0 tracking_url = None job_id = None while polled.operationState in unfinished_states: query = session.query(type(query)).filter_by(id=query.id).one() if query.status == QueryStatus.STOPPED: cursor.cancel() break log = cursor.fetch_logs() or '' if log: log_lines = log.splitlines() progress = cls.progress(log_lines) logging.info('Progress total: {}'.format(progress)) needs_commit = False if progress > query.progress: query.progress = progress needs_commit = True if not tracking_url: tracking_url = cls.get_tracking_url(log_lines) if tracking_url: job_id = tracking_url.split('/')[-2] logging.info( 'Found the tracking url: {}'.format(tracking_url)) tracking_url = tracking_url_trans(tracking_url) logging.info( 'Transformation applied: {}'.format(tracking_url)) query.tracking_url = tracking_url logging.info('Job id: {}'.format(job_id)) needs_commit = True if job_id and len(log_lines) > last_log_line: # Wait for job id before logging things out # this allows for prefixing all log lines and becoming # searchable in something like Kibana for l in log_lines[last_log_line:]: logging.info('[{}] {}'.format(job_id, l)) last_log_line = len(log_lines) if needs_commit: session.commit() time.sleep(hive_poll_interval) polled = cursor.poll()
python
def handle_cursor(cls, cursor, query, session): """Updates progress information""" from pyhive import hive # pylint: disable=no-name-in-module unfinished_states = ( hive.ttypes.TOperationState.INITIALIZED_STATE, hive.ttypes.TOperationState.RUNNING_STATE, ) polled = cursor.poll() last_log_line = 0 tracking_url = None job_id = None while polled.operationState in unfinished_states: query = session.query(type(query)).filter_by(id=query.id).one() if query.status == QueryStatus.STOPPED: cursor.cancel() break log = cursor.fetch_logs() or '' if log: log_lines = log.splitlines() progress = cls.progress(log_lines) logging.info('Progress total: {}'.format(progress)) needs_commit = False if progress > query.progress: query.progress = progress needs_commit = True if not tracking_url: tracking_url = cls.get_tracking_url(log_lines) if tracking_url: job_id = tracking_url.split('/')[-2] logging.info( 'Found the tracking url: {}'.format(tracking_url)) tracking_url = tracking_url_trans(tracking_url) logging.info( 'Transformation applied: {}'.format(tracking_url)) query.tracking_url = tracking_url logging.info('Job id: {}'.format(job_id)) needs_commit = True if job_id and len(log_lines) > last_log_line: # Wait for job id before logging things out # this allows for prefixing all log lines and becoming # searchable in something like Kibana for l in log_lines[last_log_line:]: logging.info('[{}] {}'.format(job_id, l)) last_log_line = len(log_lines) if needs_commit: session.commit() time.sleep(hive_poll_interval) polled = cursor.poll()
[ "def", "handle_cursor", "(", "cls", ",", "cursor", ",", "query", ",", "session", ")", ":", "from", "pyhive", "import", "hive", "# pylint: disable=no-name-in-module", "unfinished_states", "=", "(", "hive", ".", "ttypes", ".", "TOperationState", ".", "INITIALIZED_STATE", ",", "hive", ".", "ttypes", ".", "TOperationState", ".", "RUNNING_STATE", ",", ")", "polled", "=", "cursor", ".", "poll", "(", ")", "last_log_line", "=", "0", "tracking_url", "=", "None", "job_id", "=", "None", "while", "polled", ".", "operationState", "in", "unfinished_states", ":", "query", "=", "session", ".", "query", "(", "type", "(", "query", ")", ")", ".", "filter_by", "(", "id", "=", "query", ".", "id", ")", ".", "one", "(", ")", "if", "query", ".", "status", "==", "QueryStatus", ".", "STOPPED", ":", "cursor", ".", "cancel", "(", ")", "break", "log", "=", "cursor", ".", "fetch_logs", "(", ")", "or", "''", "if", "log", ":", "log_lines", "=", "log", ".", "splitlines", "(", ")", "progress", "=", "cls", ".", "progress", "(", "log_lines", ")", "logging", ".", "info", "(", "'Progress total: {}'", ".", "format", "(", "progress", ")", ")", "needs_commit", "=", "False", "if", "progress", ">", "query", ".", "progress", ":", "query", ".", "progress", "=", "progress", "needs_commit", "=", "True", "if", "not", "tracking_url", ":", "tracking_url", "=", "cls", ".", "get_tracking_url", "(", "log_lines", ")", "if", "tracking_url", ":", "job_id", "=", "tracking_url", ".", "split", "(", "'/'", ")", "[", "-", "2", "]", "logging", ".", "info", "(", "'Found the tracking url: {}'", ".", "format", "(", "tracking_url", ")", ")", "tracking_url", "=", "tracking_url_trans", "(", "tracking_url", ")", "logging", ".", "info", "(", "'Transformation applied: {}'", ".", "format", "(", "tracking_url", ")", ")", "query", ".", "tracking_url", "=", "tracking_url", "logging", ".", "info", "(", "'Job id: {}'", ".", "format", "(", "job_id", ")", ")", "needs_commit", "=", "True", "if", "job_id", "and", "len", "(", "log_lines", ")", ">", "last_log_line", ":", "# Wait for job id before logging things out", "# this allows for prefixing all log lines and becoming", "# searchable in something like Kibana", "for", "l", "in", "log_lines", "[", "last_log_line", ":", "]", ":", "logging", ".", "info", "(", "'[{}] {}'", ".", "format", "(", "job_id", ",", "l", ")", ")", "last_log_line", "=", "len", "(", "log_lines", ")", "if", "needs_commit", ":", "session", ".", "commit", "(", ")", "time", ".", "sleep", "(", "hive_poll_interval", ")", "polled", "=", "cursor", ".", "poll", "(", ")" ]
Updates progress information
[ "Updates", "progress", "information" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L1276-L1324
train
apache/incubator-superset
superset/db_engine_specs.py
HiveEngineSpec.get_configuration_for_impersonation
def get_configuration_for_impersonation(cls, uri, impersonate_user, username): """ Return a configuration dictionary that can be merged with other configs that can set the correct properties for impersonating users :param uri: URI string :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username :return: Dictionary with configs required for impersonation """ configuration = {} url = make_url(uri) backend_name = url.get_backend_name() # Must be Hive connection, enable impersonation, and set param auth=LDAP|KERBEROS if (backend_name == 'hive' and 'auth' in url.query.keys() and impersonate_user is True and username is not None): configuration['hive.server2.proxy.user'] = username return configuration
python
def get_configuration_for_impersonation(cls, uri, impersonate_user, username): """ Return a configuration dictionary that can be merged with other configs that can set the correct properties for impersonating users :param uri: URI string :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username :return: Dictionary with configs required for impersonation """ configuration = {} url = make_url(uri) backend_name = url.get_backend_name() # Must be Hive connection, enable impersonation, and set param auth=LDAP|KERBEROS if (backend_name == 'hive' and 'auth' in url.query.keys() and impersonate_user is True and username is not None): configuration['hive.server2.proxy.user'] = username return configuration
[ "def", "get_configuration_for_impersonation", "(", "cls", ",", "uri", ",", "impersonate_user", ",", "username", ")", ":", "configuration", "=", "{", "}", "url", "=", "make_url", "(", "uri", ")", "backend_name", "=", "url", ".", "get_backend_name", "(", ")", "# Must be Hive connection, enable impersonation, and set param auth=LDAP|KERBEROS", "if", "(", "backend_name", "==", "'hive'", "and", "'auth'", "in", "url", ".", "query", ".", "keys", "(", ")", "and", "impersonate_user", "is", "True", "and", "username", "is", "not", "None", ")", ":", "configuration", "[", "'hive.server2.proxy.user'", "]", "=", "username", "return", "configuration" ]
Return a configuration dictionary that can be merged with other configs that can set the correct properties for impersonating users :param uri: URI string :param impersonate_user: Bool indicating if impersonation is enabled :param username: Effective username :return: Dictionary with configs required for impersonation
[ "Return", "a", "configuration", "dictionary", "that", "can", "be", "merged", "with", "other", "configs", "that", "can", "set", "the", "correct", "properties", "for", "impersonating", "users", ":", "param", "uri", ":", "URI", "string", ":", "param", "impersonate_user", ":", "Bool", "indicating", "if", "impersonation", "is", "enabled", ":", "param", "username", ":", "Effective", "username", ":", "return", ":", "Dictionary", "with", "configs", "required", "for", "impersonation" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L1370-L1387
train
apache/incubator-superset
superset/db_engine_specs.py
BQEngineSpec.mutate_label
def mutate_label(label): """ BigQuery field_name should start with a letter or underscore and contain only alphanumeric characters. Labels that start with a number are prefixed with an underscore. Any unsupported characters are replaced with underscores and an md5 hash is added to the end of the label to avoid possible collisions. :param str label: the original label which might include unsupported characters :return: String that is supported by the database """ label_hashed = '_' + hashlib.md5(label.encode('utf-8')).hexdigest() # if label starts with number, add underscore as first character label_mutated = '_' + label if re.match(r'^\d', label) else label # replace non-alphanumeric characters with underscores label_mutated = re.sub(r'[^\w]+', '_', label_mutated) if label_mutated != label: # add md5 hash to label to avoid possible collisions label_mutated += label_hashed return label_mutated
python
def mutate_label(label): """ BigQuery field_name should start with a letter or underscore and contain only alphanumeric characters. Labels that start with a number are prefixed with an underscore. Any unsupported characters are replaced with underscores and an md5 hash is added to the end of the label to avoid possible collisions. :param str label: the original label which might include unsupported characters :return: String that is supported by the database """ label_hashed = '_' + hashlib.md5(label.encode('utf-8')).hexdigest() # if label starts with number, add underscore as first character label_mutated = '_' + label if re.match(r'^\d', label) else label # replace non-alphanumeric characters with underscores label_mutated = re.sub(r'[^\w]+', '_', label_mutated) if label_mutated != label: # add md5 hash to label to avoid possible collisions label_mutated += label_hashed return label_mutated
[ "def", "mutate_label", "(", "label", ")", ":", "label_hashed", "=", "'_'", "+", "hashlib", ".", "md5", "(", "label", ".", "encode", "(", "'utf-8'", ")", ")", ".", "hexdigest", "(", ")", "# if label starts with number, add underscore as first character", "label_mutated", "=", "'_'", "+", "label", "if", "re", ".", "match", "(", "r'^\\d'", ",", "label", ")", "else", "label", "# replace non-alphanumeric characters with underscores", "label_mutated", "=", "re", ".", "sub", "(", "r'[^\\w]+'", ",", "'_'", ",", "label_mutated", ")", "if", "label_mutated", "!=", "label", ":", "# add md5 hash to label to avoid possible collisions", "label_mutated", "+=", "label_hashed", "return", "label_mutated" ]
BigQuery field_name should start with a letter or underscore and contain only alphanumeric characters. Labels that start with a number are prefixed with an underscore. Any unsupported characters are replaced with underscores and an md5 hash is added to the end of the label to avoid possible collisions. :param str label: the original label which might include unsupported characters :return: String that is supported by the database
[ "BigQuery", "field_name", "should", "start", "with", "a", "letter", "or", "underscore", "and", "contain", "only", "alphanumeric", "characters", ".", "Labels", "that", "start", "with", "a", "number", "are", "prefixed", "with", "an", "underscore", ".", "Any", "unsupported", "characters", "are", "replaced", "with", "underscores", "and", "an", "md5", "hash", "is", "added", "to", "the", "end", "of", "the", "label", "to", "avoid", "possible", "collisions", ".", ":", "param", "str", "label", ":", "the", "original", "label", "which", "might", "include", "unsupported", "characters", ":", "return", ":", "String", "that", "is", "supported", "by", "the", "database" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L1612-L1632
train
apache/incubator-superset
superset/db_engine_specs.py
BQEngineSpec._get_fields
def _get_fields(cls, cols): """ BigQuery dialect requires us to not use backtick in the fieldname which are nested. Using literal_column handles that issue. https://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-more-specific-text-with-table-literal-column-and-column Also explicility specifying column names so we don't encounter duplicate column names in the result. """ return [sqla.literal_column(c.get('name')).label(c.get('name').replace('.', '__')) for c in cols]
python
def _get_fields(cls, cols): """ BigQuery dialect requires us to not use backtick in the fieldname which are nested. Using literal_column handles that issue. https://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-more-specific-text-with-table-literal-column-and-column Also explicility specifying column names so we don't encounter duplicate column names in the result. """ return [sqla.literal_column(c.get('name')).label(c.get('name').replace('.', '__')) for c in cols]
[ "def", "_get_fields", "(", "cls", ",", "cols", ")", ":", "return", "[", "sqla", ".", "literal_column", "(", "c", ".", "get", "(", "'name'", ")", ")", ".", "label", "(", "c", ".", "get", "(", "'name'", ")", ".", "replace", "(", "'.'", ",", "'__'", ")", ")", "for", "c", "in", "cols", "]" ]
BigQuery dialect requires us to not use backtick in the fieldname which are nested. Using literal_column handles that issue. https://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-more-specific-text-with-table-literal-column-and-column Also explicility specifying column names so we don't encounter duplicate column names in the result.
[ "BigQuery", "dialect", "requires", "us", "to", "not", "use", "backtick", "in", "the", "fieldname", "which", "are", "nested", ".", "Using", "literal_column", "handles", "that", "issue", ".", "https", ":", "//", "docs", ".", "sqlalchemy", ".", "org", "/", "en", "/", "latest", "/", "core", "/", "tutorial", ".", "html#using", "-", "more", "-", "specific", "-", "text", "-", "with", "-", "table", "-", "literal", "-", "column", "-", "and", "-", "column", "Also", "explicility", "specifying", "column", "names", "so", "we", "don", "t", "encounter", "duplicate", "column", "names", "in", "the", "result", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/db_engine_specs.py#L1665-L1675
train
apache/incubator-superset
superset/data/multiformat_time_series.py
load_multiformat_time_series
def load_multiformat_time_series(): """Loading time series data from a zip file in the repo""" data = get_example_data('multiformat_time_series.json.gz') pdf = pd.read_json(data) pdf.ds = pd.to_datetime(pdf.ds, unit='s') pdf.ds2 = pd.to_datetime(pdf.ds2, unit='s') pdf.to_sql( 'multiformat_time_series', db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': Date, 'ds2': DateTime, 'epoch_s': BigInteger, 'epoch_ms': BigInteger, 'string0': String(100), 'string1': String(100), 'string2': String(100), 'string3': String(100), }, index=False) print('Done loading table!') print('-' * 80) print('Creating table [multiformat_time_series] reference') obj = db.session.query(TBL).filter_by(table_name='multiformat_time_series').first() if not obj: obj = TBL(table_name='multiformat_time_series') obj.main_dttm_col = 'ds' obj.database = utils.get_or_create_main_db() dttm_and_expr_dict = { 'ds': [None, None], 'ds2': [None, None], 'epoch_s': ['epoch_s', None], 'epoch_ms': ['epoch_ms', None], 'string2': ['%Y%m%d-%H%M%S', None], 'string1': ['%Y-%m-%d^%H:%M:%S', None], 'string0': ['%Y-%m-%d %H:%M:%S.%f', None], 'string3': ['%Y/%m/%d%H:%M:%S.%f', None], } for col in obj.columns: dttm_and_expr = dttm_and_expr_dict[col.column_name] col.python_date_format = dttm_and_expr[0] col.dbatabase_expr = dttm_and_expr[1] col.is_dttm = True db.session.merge(obj) db.session.commit() obj.fetch_metadata() tbl = obj print('Creating Heatmap charts') for i, col in enumerate(tbl.columns): slice_data = { 'metrics': ['count'], 'granularity_sqla': col.column_name, 'row_limit': config.get('ROW_LIMIT'), 'since': '2015', 'until': '2016', 'where': '', 'viz_type': 'cal_heatmap', 'domain_granularity': 'month', 'subdomain_granularity': 'day', } slc = Slice( slice_name=f'Calendar Heatmap multiformat {i}', viz_type='cal_heatmap', datasource_type='table', datasource_id=tbl.id, params=get_slice_json(slice_data), ) merge_slice(slc) misc_dash_slices.add('Calendar Heatmap multiformat 0')
python
def load_multiformat_time_series(): """Loading time series data from a zip file in the repo""" data = get_example_data('multiformat_time_series.json.gz') pdf = pd.read_json(data) pdf.ds = pd.to_datetime(pdf.ds, unit='s') pdf.ds2 = pd.to_datetime(pdf.ds2, unit='s') pdf.to_sql( 'multiformat_time_series', db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': Date, 'ds2': DateTime, 'epoch_s': BigInteger, 'epoch_ms': BigInteger, 'string0': String(100), 'string1': String(100), 'string2': String(100), 'string3': String(100), }, index=False) print('Done loading table!') print('-' * 80) print('Creating table [multiformat_time_series] reference') obj = db.session.query(TBL).filter_by(table_name='multiformat_time_series').first() if not obj: obj = TBL(table_name='multiformat_time_series') obj.main_dttm_col = 'ds' obj.database = utils.get_or_create_main_db() dttm_and_expr_dict = { 'ds': [None, None], 'ds2': [None, None], 'epoch_s': ['epoch_s', None], 'epoch_ms': ['epoch_ms', None], 'string2': ['%Y%m%d-%H%M%S', None], 'string1': ['%Y-%m-%d^%H:%M:%S', None], 'string0': ['%Y-%m-%d %H:%M:%S.%f', None], 'string3': ['%Y/%m/%d%H:%M:%S.%f', None], } for col in obj.columns: dttm_and_expr = dttm_and_expr_dict[col.column_name] col.python_date_format = dttm_and_expr[0] col.dbatabase_expr = dttm_and_expr[1] col.is_dttm = True db.session.merge(obj) db.session.commit() obj.fetch_metadata() tbl = obj print('Creating Heatmap charts') for i, col in enumerate(tbl.columns): slice_data = { 'metrics': ['count'], 'granularity_sqla': col.column_name, 'row_limit': config.get('ROW_LIMIT'), 'since': '2015', 'until': '2016', 'where': '', 'viz_type': 'cal_heatmap', 'domain_granularity': 'month', 'subdomain_granularity': 'day', } slc = Slice( slice_name=f'Calendar Heatmap multiformat {i}', viz_type='cal_heatmap', datasource_type='table', datasource_id=tbl.id, params=get_slice_json(slice_data), ) merge_slice(slc) misc_dash_slices.add('Calendar Heatmap multiformat 0')
[ "def", "load_multiformat_time_series", "(", ")", ":", "data", "=", "get_example_data", "(", "'multiformat_time_series.json.gz'", ")", "pdf", "=", "pd", ".", "read_json", "(", "data", ")", "pdf", ".", "ds", "=", "pd", ".", "to_datetime", "(", "pdf", ".", "ds", ",", "unit", "=", "'s'", ")", "pdf", ".", "ds2", "=", "pd", ".", "to_datetime", "(", "pdf", ".", "ds2", ",", "unit", "=", "'s'", ")", "pdf", ".", "to_sql", "(", "'multiformat_time_series'", ",", "db", ".", "engine", ",", "if_exists", "=", "'replace'", ",", "chunksize", "=", "500", ",", "dtype", "=", "{", "'ds'", ":", "Date", ",", "'ds2'", ":", "DateTime", ",", "'epoch_s'", ":", "BigInteger", ",", "'epoch_ms'", ":", "BigInteger", ",", "'string0'", ":", "String", "(", "100", ")", ",", "'string1'", ":", "String", "(", "100", ")", ",", "'string2'", ":", "String", "(", "100", ")", ",", "'string3'", ":", "String", "(", "100", ")", ",", "}", ",", "index", "=", "False", ")", "print", "(", "'Done loading table!'", ")", "print", "(", "'-'", "*", "80", ")", "print", "(", "'Creating table [multiformat_time_series] reference'", ")", "obj", "=", "db", ".", "session", ".", "query", "(", "TBL", ")", ".", "filter_by", "(", "table_name", "=", "'multiformat_time_series'", ")", ".", "first", "(", ")", "if", "not", "obj", ":", "obj", "=", "TBL", "(", "table_name", "=", "'multiformat_time_series'", ")", "obj", ".", "main_dttm_col", "=", "'ds'", "obj", ".", "database", "=", "utils", ".", "get_or_create_main_db", "(", ")", "dttm_and_expr_dict", "=", "{", "'ds'", ":", "[", "None", ",", "None", "]", ",", "'ds2'", ":", "[", "None", ",", "None", "]", ",", "'epoch_s'", ":", "[", "'epoch_s'", ",", "None", "]", ",", "'epoch_ms'", ":", "[", "'epoch_ms'", ",", "None", "]", ",", "'string2'", ":", "[", "'%Y%m%d-%H%M%S'", ",", "None", "]", ",", "'string1'", ":", "[", "'%Y-%m-%d^%H:%M:%S'", ",", "None", "]", ",", "'string0'", ":", "[", "'%Y-%m-%d %H:%M:%S.%f'", ",", "None", "]", ",", "'string3'", ":", "[", "'%Y/%m/%d%H:%M:%S.%f'", ",", "None", "]", ",", "}", "for", "col", "in", "obj", ".", "columns", ":", "dttm_and_expr", "=", "dttm_and_expr_dict", "[", "col", ".", "column_name", "]", "col", ".", "python_date_format", "=", "dttm_and_expr", "[", "0", "]", "col", ".", "dbatabase_expr", "=", "dttm_and_expr", "[", "1", "]", "col", ".", "is_dttm", "=", "True", "db", ".", "session", ".", "merge", "(", "obj", ")", "db", ".", "session", ".", "commit", "(", ")", "obj", ".", "fetch_metadata", "(", ")", "tbl", "=", "obj", "print", "(", "'Creating Heatmap charts'", ")", "for", "i", ",", "col", "in", "enumerate", "(", "tbl", ".", "columns", ")", ":", "slice_data", "=", "{", "'metrics'", ":", "[", "'count'", "]", ",", "'granularity_sqla'", ":", "col", ".", "column_name", ",", "'row_limit'", ":", "config", ".", "get", "(", "'ROW_LIMIT'", ")", ",", "'since'", ":", "'2015'", ",", "'until'", ":", "'2016'", ",", "'where'", ":", "''", ",", "'viz_type'", ":", "'cal_heatmap'", ",", "'domain_granularity'", ":", "'month'", ",", "'subdomain_granularity'", ":", "'day'", ",", "}", "slc", "=", "Slice", "(", "slice_name", "=", "f'Calendar Heatmap multiformat {i}'", ",", "viz_type", "=", "'cal_heatmap'", ",", "datasource_type", "=", "'table'", ",", "datasource_id", "=", "tbl", ".", "id", ",", "params", "=", "get_slice_json", "(", "slice_data", ")", ",", ")", "merge_slice", "(", "slc", ")", "misc_dash_slices", ".", "add", "(", "'Calendar Heatmap multiformat 0'", ")" ]
Loading time series data from a zip file in the repo
[ "Loading", "time", "series", "data", "from", "a", "zip", "file", "in", "the", "repo" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/data/multiformat_time_series.py#L34-L107
train
apache/incubator-superset
superset/utils/dashboard_import_export.py
import_dashboards
def import_dashboards(session, data_stream, import_time=None): """Imports dashboards from a stream to databases""" current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data['datasources']: type(table).import_obj(table, import_time=import_time) session.commit() for dashboard in data['dashboards']: Dashboard.import_obj( dashboard, import_time=import_time) session.commit()
python
def import_dashboards(session, data_stream, import_time=None): """Imports dashboards from a stream to databases""" current_tt = int(time.time()) import_time = current_tt if import_time is None else import_time data = json.loads(data_stream.read(), object_hook=decode_dashboards) # TODO: import DRUID datasources for table in data['datasources']: type(table).import_obj(table, import_time=import_time) session.commit() for dashboard in data['dashboards']: Dashboard.import_obj( dashboard, import_time=import_time) session.commit()
[ "def", "import_dashboards", "(", "session", ",", "data_stream", ",", "import_time", "=", "None", ")", ":", "current_tt", "=", "int", "(", "time", ".", "time", "(", ")", ")", "import_time", "=", "current_tt", "if", "import_time", "is", "None", "else", "import_time", "data", "=", "json", ".", "loads", "(", "data_stream", ".", "read", "(", ")", ",", "object_hook", "=", "decode_dashboards", ")", "# TODO: import DRUID datasources", "for", "table", "in", "data", "[", "'datasources'", "]", ":", "type", "(", "table", ")", ".", "import_obj", "(", "table", ",", "import_time", "=", "import_time", ")", "session", ".", "commit", "(", ")", "for", "dashboard", "in", "data", "[", "'dashboards'", "]", ":", "Dashboard", ".", "import_obj", "(", "dashboard", ",", "import_time", "=", "import_time", ")", "session", ".", "commit", "(", ")" ]
Imports dashboards from a stream to databases
[ "Imports", "dashboards", "from", "a", "stream", "to", "databases" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/dashboard_import_export.py#L26-L38
train
apache/incubator-superset
superset/utils/dashboard_import_export.py
export_dashboards
def export_dashboards(session): """Returns all dashboards metadata as a json dump""" logging.info('Starting export') dashboards = session.query(Dashboard) dashboard_ids = [] for dashboard in dashboards: dashboard_ids.append(dashboard.id) data = Dashboard.export_dashboards(dashboard_ids) return data
python
def export_dashboards(session): """Returns all dashboards metadata as a json dump""" logging.info('Starting export') dashboards = session.query(Dashboard) dashboard_ids = [] for dashboard in dashboards: dashboard_ids.append(dashboard.id) data = Dashboard.export_dashboards(dashboard_ids) return data
[ "def", "export_dashboards", "(", "session", ")", ":", "logging", ".", "info", "(", "'Starting export'", ")", "dashboards", "=", "session", ".", "query", "(", "Dashboard", ")", "dashboard_ids", "=", "[", "]", "for", "dashboard", "in", "dashboards", ":", "dashboard_ids", ".", "append", "(", "dashboard", ".", "id", ")", "data", "=", "Dashboard", ".", "export_dashboards", "(", "dashboard_ids", ")", "return", "data" ]
Returns all dashboards metadata as a json dump
[ "Returns", "all", "dashboards", "metadata", "as", "a", "json", "dump" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/dashboard_import_export.py#L41-L49
train
apache/incubator-superset
superset/common/query_object.py
QueryObject.cache_key
def cache_key(self, **extra): """ The cache key is made out of the key/values in `query_obj`, plus any other key/values in `extra` We remove datetime bounds that are hard values, and replace them with the use-provided inputs to bounds, which may be time-relative (as in "5 days ago" or "now"). """ cache_dict = self.to_dict() cache_dict.update(extra) for k in ['from_dttm', 'to_dttm']: del cache_dict[k] if self.time_range: cache_dict['time_range'] = self.time_range json_data = self.json_dumps(cache_dict, sort_keys=True) return hashlib.md5(json_data.encode('utf-8')).hexdigest()
python
def cache_key(self, **extra): """ The cache key is made out of the key/values in `query_obj`, plus any other key/values in `extra` We remove datetime bounds that are hard values, and replace them with the use-provided inputs to bounds, which may be time-relative (as in "5 days ago" or "now"). """ cache_dict = self.to_dict() cache_dict.update(extra) for k in ['from_dttm', 'to_dttm']: del cache_dict[k] if self.time_range: cache_dict['time_range'] = self.time_range json_data = self.json_dumps(cache_dict, sort_keys=True) return hashlib.md5(json_data.encode('utf-8')).hexdigest()
[ "def", "cache_key", "(", "self", ",", "*", "*", "extra", ")", ":", "cache_dict", "=", "self", ".", "to_dict", "(", ")", "cache_dict", ".", "update", "(", "extra", ")", "for", "k", "in", "[", "'from_dttm'", ",", "'to_dttm'", "]", ":", "del", "cache_dict", "[", "k", "]", "if", "self", ".", "time_range", ":", "cache_dict", "[", "'time_range'", "]", "=", "self", ".", "time_range", "json_data", "=", "self", ".", "json_dumps", "(", "cache_dict", ",", "sort_keys", "=", "True", ")", "return", "hashlib", ".", "md5", "(", "json_data", ".", "encode", "(", "'utf-8'", ")", ")", ".", "hexdigest", "(", ")" ]
The cache key is made out of the key/values in `query_obj`, plus any other key/values in `extra` We remove datetime bounds that are hard values, and replace them with the use-provided inputs to bounds, which may be time-relative (as in "5 days ago" or "now").
[ "The", "cache", "key", "is", "made", "out", "of", "the", "key", "/", "values", "in", "query_obj", "plus", "any", "other", "key", "/", "values", "in", "extra", "We", "remove", "datetime", "bounds", "that", "are", "hard", "values", "and", "replace", "them", "with", "the", "use", "-", "provided", "inputs", "to", "bounds", "which", "may", "be", "time", "-", "relative", "(", "as", "in", "5", "days", "ago", "or", "now", ")", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/common/query_object.py#L100-L116
train
apache/incubator-superset
superset/sql_lab.py
handle_query_error
def handle_query_error(msg, query, session, payload=None): """Local method handling error while processing the SQL""" payload = payload or {} troubleshooting_link = config['TROUBLESHOOTING_LINK'] query.error_message = msg query.status = QueryStatus.FAILED query.tmp_table_name = None session.commit() payload.update({ 'status': query.status, 'error': msg, }) if troubleshooting_link: payload['link'] = troubleshooting_link return payload
python
def handle_query_error(msg, query, session, payload=None): """Local method handling error while processing the SQL""" payload = payload or {} troubleshooting_link = config['TROUBLESHOOTING_LINK'] query.error_message = msg query.status = QueryStatus.FAILED query.tmp_table_name = None session.commit() payload.update({ 'status': query.status, 'error': msg, }) if troubleshooting_link: payload['link'] = troubleshooting_link return payload
[ "def", "handle_query_error", "(", "msg", ",", "query", ",", "session", ",", "payload", "=", "None", ")", ":", "payload", "=", "payload", "or", "{", "}", "troubleshooting_link", "=", "config", "[", "'TROUBLESHOOTING_LINK'", "]", "query", ".", "error_message", "=", "msg", "query", ".", "status", "=", "QueryStatus", ".", "FAILED", "query", ".", "tmp_table_name", "=", "None", "session", ".", "commit", "(", ")", "payload", ".", "update", "(", "{", "'status'", ":", "query", ".", "status", ",", "'error'", ":", "msg", ",", "}", ")", "if", "troubleshooting_link", ":", "payload", "[", "'link'", "]", "=", "troubleshooting_link", "return", "payload" ]
Local method handling error while processing the SQL
[ "Local", "method", "handling", "error", "while", "processing", "the", "SQL" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L63-L77
train
apache/incubator-superset
superset/sql_lab.py
get_query
def get_query(query_id, session, retry_count=5): """attemps to get the query and retry if it cannot""" query = None attempt = 0 while not query and attempt < retry_count: try: query = session.query(Query).filter_by(id=query_id).one() except Exception: attempt += 1 logging.error( 'Query with id `{}` could not be retrieved'.format(query_id)) stats_logger.incr('error_attempting_orm_query_' + str(attempt)) logging.error('Sleeping for a sec before retrying...') sleep(1) if not query: stats_logger.incr('error_failed_at_getting_orm_query') raise SqlLabException('Failed at getting query') return query
python
def get_query(query_id, session, retry_count=5): """attemps to get the query and retry if it cannot""" query = None attempt = 0 while not query and attempt < retry_count: try: query = session.query(Query).filter_by(id=query_id).one() except Exception: attempt += 1 logging.error( 'Query with id `{}` could not be retrieved'.format(query_id)) stats_logger.incr('error_attempting_orm_query_' + str(attempt)) logging.error('Sleeping for a sec before retrying...') sleep(1) if not query: stats_logger.incr('error_failed_at_getting_orm_query') raise SqlLabException('Failed at getting query') return query
[ "def", "get_query", "(", "query_id", ",", "session", ",", "retry_count", "=", "5", ")", ":", "query", "=", "None", "attempt", "=", "0", "while", "not", "query", "and", "attempt", "<", "retry_count", ":", "try", ":", "query", "=", "session", ".", "query", "(", "Query", ")", ".", "filter_by", "(", "id", "=", "query_id", ")", ".", "one", "(", ")", "except", "Exception", ":", "attempt", "+=", "1", "logging", ".", "error", "(", "'Query with id `{}` could not be retrieved'", ".", "format", "(", "query_id", ")", ")", "stats_logger", ".", "incr", "(", "'error_attempting_orm_query_'", "+", "str", "(", "attempt", ")", ")", "logging", ".", "error", "(", "'Sleeping for a sec before retrying...'", ")", "sleep", "(", "1", ")", "if", "not", "query", ":", "stats_logger", ".", "incr", "(", "'error_failed_at_getting_orm_query'", ")", "raise", "SqlLabException", "(", "'Failed at getting query'", ")", "return", "query" ]
attemps to get the query and retry if it cannot
[ "attemps", "to", "get", "the", "query", "and", "retry", "if", "it", "cannot" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L80-L97
train
apache/incubator-superset
superset/sql_lab.py
session_scope
def session_scope(nullpool): """Provide a transactional scope around a series of operations.""" if nullpool: engine = sqlalchemy.create_engine( app.config.get('SQLALCHEMY_DATABASE_URI'), poolclass=NullPool) session_class = sessionmaker() session_class.configure(bind=engine) session = session_class() else: session = db.session() session.commit() # HACK try: yield session session.commit() except Exception as e: session.rollback() logging.exception(e) raise finally: session.close()
python
def session_scope(nullpool): """Provide a transactional scope around a series of operations.""" if nullpool: engine = sqlalchemy.create_engine( app.config.get('SQLALCHEMY_DATABASE_URI'), poolclass=NullPool) session_class = sessionmaker() session_class.configure(bind=engine) session = session_class() else: session = db.session() session.commit() # HACK try: yield session session.commit() except Exception as e: session.rollback() logging.exception(e) raise finally: session.close()
[ "def", "session_scope", "(", "nullpool", ")", ":", "if", "nullpool", ":", "engine", "=", "sqlalchemy", ".", "create_engine", "(", "app", ".", "config", ".", "get", "(", "'SQLALCHEMY_DATABASE_URI'", ")", ",", "poolclass", "=", "NullPool", ")", "session_class", "=", "sessionmaker", "(", ")", "session_class", ".", "configure", "(", "bind", "=", "engine", ")", "session", "=", "session_class", "(", ")", "else", ":", "session", "=", "db", ".", "session", "(", ")", "session", ".", "commit", "(", ")", "# HACK", "try", ":", "yield", "session", "session", ".", "commit", "(", ")", "except", "Exception", "as", "e", ":", "session", ".", "rollback", "(", ")", "logging", ".", "exception", "(", "e", ")", "raise", "finally", ":", "session", ".", "close", "(", ")" ]
Provide a transactional scope around a series of operations.
[ "Provide", "a", "transactional", "scope", "around", "a", "series", "of", "operations", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L101-L121
train
apache/incubator-superset
superset/sql_lab.py
get_sql_results
def get_sql_results( ctask, query_id, rendered_query, return_results=True, store_results=False, user_name=None, start_time=None): """Executes the sql query returns the results.""" with session_scope(not ctask.request.called_directly) as session: try: return execute_sql_statements( ctask, query_id, rendered_query, return_results, store_results, user_name, session=session, start_time=start_time) except Exception as e: logging.exception(e) stats_logger.incr('error_sqllab_unhandled') query = get_query(query_id, session) return handle_query_error(str(e), query, session)
python
def get_sql_results( ctask, query_id, rendered_query, return_results=True, store_results=False, user_name=None, start_time=None): """Executes the sql query returns the results.""" with session_scope(not ctask.request.called_directly) as session: try: return execute_sql_statements( ctask, query_id, rendered_query, return_results, store_results, user_name, session=session, start_time=start_time) except Exception as e: logging.exception(e) stats_logger.incr('error_sqllab_unhandled') query = get_query(query_id, session) return handle_query_error(str(e), query, session)
[ "def", "get_sql_results", "(", "ctask", ",", "query_id", ",", "rendered_query", ",", "return_results", "=", "True", ",", "store_results", "=", "False", ",", "user_name", "=", "None", ",", "start_time", "=", "None", ")", ":", "with", "session_scope", "(", "not", "ctask", ".", "request", ".", "called_directly", ")", "as", "session", ":", "try", ":", "return", "execute_sql_statements", "(", "ctask", ",", "query_id", ",", "rendered_query", ",", "return_results", ",", "store_results", ",", "user_name", ",", "session", "=", "session", ",", "start_time", "=", "start_time", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "stats_logger", ".", "incr", "(", "'error_sqllab_unhandled'", ")", "query", "=", "get_query", "(", "query_id", ",", "session", ")", "return", "handle_query_error", "(", "str", "(", "e", ")", ",", "query", ",", "session", ")" ]
Executes the sql query returns the results.
[ "Executes", "the", "sql", "query", "returns", "the", "results", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L127-L141
train
apache/incubator-superset
superset/sql_lab.py
execute_sql_statement
def execute_sql_statement(sql_statement, query, user_name, session, cursor): """Executes a single SQL statement""" database = query.database db_engine_spec = database.db_engine_spec parsed_query = ParsedQuery(sql_statement) sql = parsed_query.stripped() SQL_MAX_ROWS = app.config.get('SQL_MAX_ROW') if not parsed_query.is_readonly() and not database.allow_dml: raise SqlLabSecurityException( _('Only `SELECT` statements are allowed against this database')) if query.select_as_cta: if not parsed_query.is_select(): raise SqlLabException(_( 'Only `SELECT` statements can be used with the CREATE TABLE ' 'feature.')) if not query.tmp_table_name: start_dttm = datetime.fromtimestamp(query.start_time) query.tmp_table_name = 'tmp_{}_table_{}'.format( query.user_id, start_dttm.strftime('%Y_%m_%d_%H_%M_%S')) sql = parsed_query.as_create_table(query.tmp_table_name) query.select_as_cta_used = True if parsed_query.is_select(): if SQL_MAX_ROWS and (not query.limit or query.limit > SQL_MAX_ROWS): query.limit = SQL_MAX_ROWS if query.limit: sql = database.apply_limit_to_sql(sql, query.limit) # Hook to allow environment-specific mutation (usually comments) to the SQL SQL_QUERY_MUTATOR = config.get('SQL_QUERY_MUTATOR') if SQL_QUERY_MUTATOR: sql = SQL_QUERY_MUTATOR(sql, user_name, security_manager, database) try: if log_query: log_query( query.database.sqlalchemy_uri, query.executed_sql, query.schema, user_name, __name__, security_manager, ) query.executed_sql = sql with stats_timing('sqllab.query.time_executing_query', stats_logger): logging.info('Running query: \n{}'.format(sql)) db_engine_spec.execute(cursor, sql, async_=True) logging.info('Handling cursor') db_engine_spec.handle_cursor(cursor, query, session) with stats_timing('sqllab.query.time_fetching_results', stats_logger): logging.debug('Fetching data for query object: {}'.format(query.to_dict())) data = db_engine_spec.fetch_data(cursor, query.limit) except SoftTimeLimitExceeded as e: logging.exception(e) raise SqlLabTimeoutException( "SQL Lab timeout. This environment's policy is to kill queries " 'after {} seconds.'.format(SQLLAB_TIMEOUT)) except Exception as e: logging.exception(e) raise SqlLabException(db_engine_spec.extract_error_message(e)) logging.debug('Fetching cursor description') cursor_description = cursor.description return dataframe.SupersetDataFrame(data, cursor_description, db_engine_spec)
python
def execute_sql_statement(sql_statement, query, user_name, session, cursor): """Executes a single SQL statement""" database = query.database db_engine_spec = database.db_engine_spec parsed_query = ParsedQuery(sql_statement) sql = parsed_query.stripped() SQL_MAX_ROWS = app.config.get('SQL_MAX_ROW') if not parsed_query.is_readonly() and not database.allow_dml: raise SqlLabSecurityException( _('Only `SELECT` statements are allowed against this database')) if query.select_as_cta: if not parsed_query.is_select(): raise SqlLabException(_( 'Only `SELECT` statements can be used with the CREATE TABLE ' 'feature.')) if not query.tmp_table_name: start_dttm = datetime.fromtimestamp(query.start_time) query.tmp_table_name = 'tmp_{}_table_{}'.format( query.user_id, start_dttm.strftime('%Y_%m_%d_%H_%M_%S')) sql = parsed_query.as_create_table(query.tmp_table_name) query.select_as_cta_used = True if parsed_query.is_select(): if SQL_MAX_ROWS and (not query.limit or query.limit > SQL_MAX_ROWS): query.limit = SQL_MAX_ROWS if query.limit: sql = database.apply_limit_to_sql(sql, query.limit) # Hook to allow environment-specific mutation (usually comments) to the SQL SQL_QUERY_MUTATOR = config.get('SQL_QUERY_MUTATOR') if SQL_QUERY_MUTATOR: sql = SQL_QUERY_MUTATOR(sql, user_name, security_manager, database) try: if log_query: log_query( query.database.sqlalchemy_uri, query.executed_sql, query.schema, user_name, __name__, security_manager, ) query.executed_sql = sql with stats_timing('sqllab.query.time_executing_query', stats_logger): logging.info('Running query: \n{}'.format(sql)) db_engine_spec.execute(cursor, sql, async_=True) logging.info('Handling cursor') db_engine_spec.handle_cursor(cursor, query, session) with stats_timing('sqllab.query.time_fetching_results', stats_logger): logging.debug('Fetching data for query object: {}'.format(query.to_dict())) data = db_engine_spec.fetch_data(cursor, query.limit) except SoftTimeLimitExceeded as e: logging.exception(e) raise SqlLabTimeoutException( "SQL Lab timeout. This environment's policy is to kill queries " 'after {} seconds.'.format(SQLLAB_TIMEOUT)) except Exception as e: logging.exception(e) raise SqlLabException(db_engine_spec.extract_error_message(e)) logging.debug('Fetching cursor description') cursor_description = cursor.description return dataframe.SupersetDataFrame(data, cursor_description, db_engine_spec)
[ "def", "execute_sql_statement", "(", "sql_statement", ",", "query", ",", "user_name", ",", "session", ",", "cursor", ")", ":", "database", "=", "query", ".", "database", "db_engine_spec", "=", "database", ".", "db_engine_spec", "parsed_query", "=", "ParsedQuery", "(", "sql_statement", ")", "sql", "=", "parsed_query", ".", "stripped", "(", ")", "SQL_MAX_ROWS", "=", "app", ".", "config", ".", "get", "(", "'SQL_MAX_ROW'", ")", "if", "not", "parsed_query", ".", "is_readonly", "(", ")", "and", "not", "database", ".", "allow_dml", ":", "raise", "SqlLabSecurityException", "(", "_", "(", "'Only `SELECT` statements are allowed against this database'", ")", ")", "if", "query", ".", "select_as_cta", ":", "if", "not", "parsed_query", ".", "is_select", "(", ")", ":", "raise", "SqlLabException", "(", "_", "(", "'Only `SELECT` statements can be used with the CREATE TABLE '", "'feature.'", ")", ")", "if", "not", "query", ".", "tmp_table_name", ":", "start_dttm", "=", "datetime", ".", "fromtimestamp", "(", "query", ".", "start_time", ")", "query", ".", "tmp_table_name", "=", "'tmp_{}_table_{}'", ".", "format", "(", "query", ".", "user_id", ",", "start_dttm", ".", "strftime", "(", "'%Y_%m_%d_%H_%M_%S'", ")", ")", "sql", "=", "parsed_query", ".", "as_create_table", "(", "query", ".", "tmp_table_name", ")", "query", ".", "select_as_cta_used", "=", "True", "if", "parsed_query", ".", "is_select", "(", ")", ":", "if", "SQL_MAX_ROWS", "and", "(", "not", "query", ".", "limit", "or", "query", ".", "limit", ">", "SQL_MAX_ROWS", ")", ":", "query", ".", "limit", "=", "SQL_MAX_ROWS", "if", "query", ".", "limit", ":", "sql", "=", "database", ".", "apply_limit_to_sql", "(", "sql", ",", "query", ".", "limit", ")", "# Hook to allow environment-specific mutation (usually comments) to the SQL", "SQL_QUERY_MUTATOR", "=", "config", ".", "get", "(", "'SQL_QUERY_MUTATOR'", ")", "if", "SQL_QUERY_MUTATOR", ":", "sql", "=", "SQL_QUERY_MUTATOR", "(", "sql", ",", "user_name", ",", "security_manager", ",", "database", ")", "try", ":", "if", "log_query", ":", "log_query", "(", "query", ".", "database", ".", "sqlalchemy_uri", ",", "query", ".", "executed_sql", ",", "query", ".", "schema", ",", "user_name", ",", "__name__", ",", "security_manager", ",", ")", "query", ".", "executed_sql", "=", "sql", "with", "stats_timing", "(", "'sqllab.query.time_executing_query'", ",", "stats_logger", ")", ":", "logging", ".", "info", "(", "'Running query: \\n{}'", ".", "format", "(", "sql", ")", ")", "db_engine_spec", ".", "execute", "(", "cursor", ",", "sql", ",", "async_", "=", "True", ")", "logging", ".", "info", "(", "'Handling cursor'", ")", "db_engine_spec", ".", "handle_cursor", "(", "cursor", ",", "query", ",", "session", ")", "with", "stats_timing", "(", "'sqllab.query.time_fetching_results'", ",", "stats_logger", ")", ":", "logging", ".", "debug", "(", "'Fetching data for query object: {}'", ".", "format", "(", "query", ".", "to_dict", "(", ")", ")", ")", "data", "=", "db_engine_spec", ".", "fetch_data", "(", "cursor", ",", "query", ".", "limit", ")", "except", "SoftTimeLimitExceeded", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "raise", "SqlLabTimeoutException", "(", "\"SQL Lab timeout. This environment's policy is to kill queries \"", "'after {} seconds.'", ".", "format", "(", "SQLLAB_TIMEOUT", ")", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "raise", "SqlLabException", "(", "db_engine_spec", ".", "extract_error_message", "(", "e", ")", ")", "logging", ".", "debug", "(", "'Fetching cursor description'", ")", "cursor_description", "=", "cursor", ".", "description", "return", "dataframe", ".", "SupersetDataFrame", "(", "data", ",", "cursor_description", ",", "db_engine_spec", ")" ]
Executes a single SQL statement
[ "Executes", "a", "single", "SQL", "statement" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L144-L209
train
apache/incubator-superset
superset/sql_lab.py
execute_sql_statements
def execute_sql_statements( ctask, query_id, rendered_query, return_results=True, store_results=False, user_name=None, session=None, start_time=None, ): """Executes the sql query returns the results.""" if store_results and start_time: # only asynchronous queries stats_logger.timing( 'sqllab.query.time_pending', now_as_float() - start_time) query = get_query(query_id, session) payload = dict(query_id=query_id) database = query.database db_engine_spec = database.db_engine_spec db_engine_spec.patch() if store_results and not results_backend: raise SqlLabException("Results backend isn't configured.") # Breaking down into multiple statements parsed_query = ParsedQuery(rendered_query) statements = parsed_query.get_statements() logging.info(f'Executing {len(statements)} statement(s)') logging.info("Set query to 'running'") query.status = QueryStatus.RUNNING query.start_running_time = now_as_float() engine = database.get_sqla_engine( schema=query.schema, nullpool=True, user_name=user_name, source=sources.get('sql_lab', None), ) # Sharing a single connection and cursor across the # execution of all statements (if many) with closing(engine.raw_connection()) as conn: with closing(conn.cursor()) as cursor: statement_count = len(statements) for i, statement in enumerate(statements): # TODO CHECK IF STOPPED msg = f'Running statement {i+1} out of {statement_count}' logging.info(msg) query.set_extra_json_key('progress', msg) session.commit() try: cdf = execute_sql_statement( statement, query, user_name, session, cursor) msg = f'Running statement {i+1} out of {statement_count}' except Exception as e: msg = str(e) if statement_count > 1: msg = f'[Statement {i+1} out of {statement_count}] ' + msg payload = handle_query_error(msg, query, session, payload) return payload # Success, updating the query entry in database query.rows = cdf.size query.progress = 100 query.set_extra_json_key('progress', None) query.status = QueryStatus.SUCCESS if query.select_as_cta: query.select_sql = database.select_star( query.tmp_table_name, limit=query.limit, schema=database.force_ctas_schema, show_cols=False, latest_partition=False) query.end_time = now_as_float() payload.update({ 'status': query.status, 'data': cdf.data if cdf.data else [], 'columns': cdf.columns if cdf.columns else [], 'query': query.to_dict(), }) if store_results: key = str(uuid.uuid4()) logging.info(f'Storing results in results backend, key: {key}') with stats_timing('sqllab.query.results_backend_write', stats_logger): json_payload = json.dumps( payload, default=json_iso_dttm_ser, ignore_nan=True) cache_timeout = database.cache_timeout if cache_timeout is None: cache_timeout = config.get('CACHE_DEFAULT_TIMEOUT', 0) results_backend.set(key, zlib_compress(json_payload), cache_timeout) query.results_key = key session.commit() if return_results: return payload
python
def execute_sql_statements( ctask, query_id, rendered_query, return_results=True, store_results=False, user_name=None, session=None, start_time=None, ): """Executes the sql query returns the results.""" if store_results and start_time: # only asynchronous queries stats_logger.timing( 'sqllab.query.time_pending', now_as_float() - start_time) query = get_query(query_id, session) payload = dict(query_id=query_id) database = query.database db_engine_spec = database.db_engine_spec db_engine_spec.patch() if store_results and not results_backend: raise SqlLabException("Results backend isn't configured.") # Breaking down into multiple statements parsed_query = ParsedQuery(rendered_query) statements = parsed_query.get_statements() logging.info(f'Executing {len(statements)} statement(s)') logging.info("Set query to 'running'") query.status = QueryStatus.RUNNING query.start_running_time = now_as_float() engine = database.get_sqla_engine( schema=query.schema, nullpool=True, user_name=user_name, source=sources.get('sql_lab', None), ) # Sharing a single connection and cursor across the # execution of all statements (if many) with closing(engine.raw_connection()) as conn: with closing(conn.cursor()) as cursor: statement_count = len(statements) for i, statement in enumerate(statements): # TODO CHECK IF STOPPED msg = f'Running statement {i+1} out of {statement_count}' logging.info(msg) query.set_extra_json_key('progress', msg) session.commit() try: cdf = execute_sql_statement( statement, query, user_name, session, cursor) msg = f'Running statement {i+1} out of {statement_count}' except Exception as e: msg = str(e) if statement_count > 1: msg = f'[Statement {i+1} out of {statement_count}] ' + msg payload = handle_query_error(msg, query, session, payload) return payload # Success, updating the query entry in database query.rows = cdf.size query.progress = 100 query.set_extra_json_key('progress', None) query.status = QueryStatus.SUCCESS if query.select_as_cta: query.select_sql = database.select_star( query.tmp_table_name, limit=query.limit, schema=database.force_ctas_schema, show_cols=False, latest_partition=False) query.end_time = now_as_float() payload.update({ 'status': query.status, 'data': cdf.data if cdf.data else [], 'columns': cdf.columns if cdf.columns else [], 'query': query.to_dict(), }) if store_results: key = str(uuid.uuid4()) logging.info(f'Storing results in results backend, key: {key}') with stats_timing('sqllab.query.results_backend_write', stats_logger): json_payload = json.dumps( payload, default=json_iso_dttm_ser, ignore_nan=True) cache_timeout = database.cache_timeout if cache_timeout is None: cache_timeout = config.get('CACHE_DEFAULT_TIMEOUT', 0) results_backend.set(key, zlib_compress(json_payload), cache_timeout) query.results_key = key session.commit() if return_results: return payload
[ "def", "execute_sql_statements", "(", "ctask", ",", "query_id", ",", "rendered_query", ",", "return_results", "=", "True", ",", "store_results", "=", "False", ",", "user_name", "=", "None", ",", "session", "=", "None", ",", "start_time", "=", "None", ",", ")", ":", "if", "store_results", "and", "start_time", ":", "# only asynchronous queries", "stats_logger", ".", "timing", "(", "'sqllab.query.time_pending'", ",", "now_as_float", "(", ")", "-", "start_time", ")", "query", "=", "get_query", "(", "query_id", ",", "session", ")", "payload", "=", "dict", "(", "query_id", "=", "query_id", ")", "database", "=", "query", ".", "database", "db_engine_spec", "=", "database", ".", "db_engine_spec", "db_engine_spec", ".", "patch", "(", ")", "if", "store_results", "and", "not", "results_backend", ":", "raise", "SqlLabException", "(", "\"Results backend isn't configured.\"", ")", "# Breaking down into multiple statements", "parsed_query", "=", "ParsedQuery", "(", "rendered_query", ")", "statements", "=", "parsed_query", ".", "get_statements", "(", ")", "logging", ".", "info", "(", "f'Executing {len(statements)} statement(s)'", ")", "logging", ".", "info", "(", "\"Set query to 'running'\"", ")", "query", ".", "status", "=", "QueryStatus", ".", "RUNNING", "query", ".", "start_running_time", "=", "now_as_float", "(", ")", "engine", "=", "database", ".", "get_sqla_engine", "(", "schema", "=", "query", ".", "schema", ",", "nullpool", "=", "True", ",", "user_name", "=", "user_name", ",", "source", "=", "sources", ".", "get", "(", "'sql_lab'", ",", "None", ")", ",", ")", "# Sharing a single connection and cursor across the", "# execution of all statements (if many)", "with", "closing", "(", "engine", ".", "raw_connection", "(", ")", ")", "as", "conn", ":", "with", "closing", "(", "conn", ".", "cursor", "(", ")", ")", "as", "cursor", ":", "statement_count", "=", "len", "(", "statements", ")", "for", "i", ",", "statement", "in", "enumerate", "(", "statements", ")", ":", "# TODO CHECK IF STOPPED", "msg", "=", "f'Running statement {i+1} out of {statement_count}'", "logging", ".", "info", "(", "msg", ")", "query", ".", "set_extra_json_key", "(", "'progress'", ",", "msg", ")", "session", ".", "commit", "(", ")", "try", ":", "cdf", "=", "execute_sql_statement", "(", "statement", ",", "query", ",", "user_name", ",", "session", ",", "cursor", ")", "msg", "=", "f'Running statement {i+1} out of {statement_count}'", "except", "Exception", "as", "e", ":", "msg", "=", "str", "(", "e", ")", "if", "statement_count", ">", "1", ":", "msg", "=", "f'[Statement {i+1} out of {statement_count}] '", "+", "msg", "payload", "=", "handle_query_error", "(", "msg", ",", "query", ",", "session", ",", "payload", ")", "return", "payload", "# Success, updating the query entry in database", "query", ".", "rows", "=", "cdf", ".", "size", "query", ".", "progress", "=", "100", "query", ".", "set_extra_json_key", "(", "'progress'", ",", "None", ")", "query", ".", "status", "=", "QueryStatus", ".", "SUCCESS", "if", "query", ".", "select_as_cta", ":", "query", ".", "select_sql", "=", "database", ".", "select_star", "(", "query", ".", "tmp_table_name", ",", "limit", "=", "query", ".", "limit", ",", "schema", "=", "database", ".", "force_ctas_schema", ",", "show_cols", "=", "False", ",", "latest_partition", "=", "False", ")", "query", ".", "end_time", "=", "now_as_float", "(", ")", "payload", ".", "update", "(", "{", "'status'", ":", "query", ".", "status", ",", "'data'", ":", "cdf", ".", "data", "if", "cdf", ".", "data", "else", "[", "]", ",", "'columns'", ":", "cdf", ".", "columns", "if", "cdf", ".", "columns", "else", "[", "]", ",", "'query'", ":", "query", ".", "to_dict", "(", ")", ",", "}", ")", "if", "store_results", ":", "key", "=", "str", "(", "uuid", ".", "uuid4", "(", ")", ")", "logging", ".", "info", "(", "f'Storing results in results backend, key: {key}'", ")", "with", "stats_timing", "(", "'sqllab.query.results_backend_write'", ",", "stats_logger", ")", ":", "json_payload", "=", "json", ".", "dumps", "(", "payload", ",", "default", "=", "json_iso_dttm_ser", ",", "ignore_nan", "=", "True", ")", "cache_timeout", "=", "database", ".", "cache_timeout", "if", "cache_timeout", "is", "None", ":", "cache_timeout", "=", "config", ".", "get", "(", "'CACHE_DEFAULT_TIMEOUT'", ",", "0", ")", "results_backend", ".", "set", "(", "key", ",", "zlib_compress", "(", "json_payload", ")", ",", "cache_timeout", ")", "query", ".", "results_key", "=", "key", "session", ".", "commit", "(", ")", "if", "return_results", ":", "return", "payload" ]
Executes the sql query returns the results.
[ "Executes", "the", "sql", "query", "returns", "the", "results", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/sql_lab.py#L212-L303
train
apache/incubator-superset
superset/utils/core.py
flasher
def flasher(msg, severity=None): """Flask's flash if available, logging call if not""" try: flash(msg, severity) except RuntimeError: if severity == 'danger': logging.error(msg) else: logging.info(msg)
python
def flasher(msg, severity=None): """Flask's flash if available, logging call if not""" try: flash(msg, severity) except RuntimeError: if severity == 'danger': logging.error(msg) else: logging.info(msg)
[ "def", "flasher", "(", "msg", ",", "severity", "=", "None", ")", ":", "try", ":", "flash", "(", "msg", ",", "severity", ")", "except", "RuntimeError", ":", "if", "severity", "==", "'danger'", ":", "logging", ".", "error", "(", "msg", ")", "else", ":", "logging", ".", "info", "(", "msg", ")" ]
Flask's flash if available, logging call if not
[ "Flask", "s", "flash", "if", "available", "logging", "call", "if", "not" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L81-L89
train
apache/incubator-superset
superset/utils/core.py
string_to_num
def string_to_num(s: str): """Converts a string to an int/float Returns ``None`` if it can't be converted >>> string_to_num('5') 5 >>> string_to_num('5.2') 5.2 >>> string_to_num(10) 10 >>> string_to_num(10.1) 10.1 >>> string_to_num('this is not a string') is None True """ if isinstance(s, (int, float)): return s if s.isdigit(): return int(s) try: return float(s) except ValueError: return None
python
def string_to_num(s: str): """Converts a string to an int/float Returns ``None`` if it can't be converted >>> string_to_num('5') 5 >>> string_to_num('5.2') 5.2 >>> string_to_num(10) 10 >>> string_to_num(10.1) 10.1 >>> string_to_num('this is not a string') is None True """ if isinstance(s, (int, float)): return s if s.isdigit(): return int(s) try: return float(s) except ValueError: return None
[ "def", "string_to_num", "(", "s", ":", "str", ")", ":", "if", "isinstance", "(", "s", ",", "(", "int", ",", "float", ")", ")", ":", "return", "s", "if", "s", ".", "isdigit", "(", ")", ":", "return", "int", "(", "s", ")", "try", ":", "return", "float", "(", "s", ")", "except", "ValueError", ":", "return", "None" ]
Converts a string to an int/float Returns ``None`` if it can't be converted >>> string_to_num('5') 5 >>> string_to_num('5.2') 5.2 >>> string_to_num(10) 10 >>> string_to_num(10.1) 10.1 >>> string_to_num('this is not a string') is None True
[ "Converts", "a", "string", "to", "an", "int", "/", "float" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L148-L171
train
apache/incubator-superset
superset/utils/core.py
list_minus
def list_minus(l: List, minus: List) -> List: """Returns l without what is in minus >>> list_minus([1, 2, 3], [2]) [1, 3] """ return [o for o in l if o not in minus]
python
def list_minus(l: List, minus: List) -> List: """Returns l without what is in minus >>> list_minus([1, 2, 3], [2]) [1, 3] """ return [o for o in l if o not in minus]
[ "def", "list_minus", "(", "l", ":", "List", ",", "minus", ":", "List", ")", "->", "List", ":", "return", "[", "o", "for", "o", "in", "l", "if", "o", "not", "in", "minus", "]" ]
Returns l without what is in minus >>> list_minus([1, 2, 3], [2]) [1, 3]
[ "Returns", "l", "without", "what", "is", "in", "minus" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L188-L194
train
apache/incubator-superset
superset/utils/core.py
parse_human_datetime
def parse_human_datetime(s): """ Returns ``datetime.datetime`` from human readable strings >>> from datetime import date, timedelta >>> from dateutil.relativedelta import relativedelta >>> parse_human_datetime('2015-04-03') datetime.datetime(2015, 4, 3, 0, 0) >>> parse_human_datetime('2/3/1969') datetime.datetime(1969, 2, 3, 0, 0) >>> parse_human_datetime('now') <= datetime.now() True >>> parse_human_datetime('yesterday') <= datetime.now() True >>> date.today() - timedelta(1) == parse_human_datetime('yesterday').date() True >>> year_ago_1 = parse_human_datetime('one year ago').date() >>> year_ago_2 = (datetime.now() - relativedelta(years=1) ).date() >>> year_ago_1 == year_ago_2 True """ if not s: return None try: dttm = parse(s) except Exception: try: cal = parsedatetime.Calendar() parsed_dttm, parsed_flags = cal.parseDT(s) # when time is not extracted, we 'reset to midnight' if parsed_flags & 2 == 0: parsed_dttm = parsed_dttm.replace(hour=0, minute=0, second=0) dttm = dttm_from_timtuple(parsed_dttm.utctimetuple()) except Exception as e: logging.exception(e) raise ValueError("Couldn't parse date string [{}]".format(s)) return dttm
python
def parse_human_datetime(s): """ Returns ``datetime.datetime`` from human readable strings >>> from datetime import date, timedelta >>> from dateutil.relativedelta import relativedelta >>> parse_human_datetime('2015-04-03') datetime.datetime(2015, 4, 3, 0, 0) >>> parse_human_datetime('2/3/1969') datetime.datetime(1969, 2, 3, 0, 0) >>> parse_human_datetime('now') <= datetime.now() True >>> parse_human_datetime('yesterday') <= datetime.now() True >>> date.today() - timedelta(1) == parse_human_datetime('yesterday').date() True >>> year_ago_1 = parse_human_datetime('one year ago').date() >>> year_ago_2 = (datetime.now() - relativedelta(years=1) ).date() >>> year_ago_1 == year_ago_2 True """ if not s: return None try: dttm = parse(s) except Exception: try: cal = parsedatetime.Calendar() parsed_dttm, parsed_flags = cal.parseDT(s) # when time is not extracted, we 'reset to midnight' if parsed_flags & 2 == 0: parsed_dttm = parsed_dttm.replace(hour=0, minute=0, second=0) dttm = dttm_from_timtuple(parsed_dttm.utctimetuple()) except Exception as e: logging.exception(e) raise ValueError("Couldn't parse date string [{}]".format(s)) return dttm
[ "def", "parse_human_datetime", "(", "s", ")", ":", "if", "not", "s", ":", "return", "None", "try", ":", "dttm", "=", "parse", "(", "s", ")", "except", "Exception", ":", "try", ":", "cal", "=", "parsedatetime", ".", "Calendar", "(", ")", "parsed_dttm", ",", "parsed_flags", "=", "cal", ".", "parseDT", "(", "s", ")", "# when time is not extracted, we 'reset to midnight'", "if", "parsed_flags", "&", "2", "==", "0", ":", "parsed_dttm", "=", "parsed_dttm", ".", "replace", "(", "hour", "=", "0", ",", "minute", "=", "0", ",", "second", "=", "0", ")", "dttm", "=", "dttm_from_timtuple", "(", "parsed_dttm", ".", "utctimetuple", "(", ")", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "raise", "ValueError", "(", "\"Couldn't parse date string [{}]\"", ".", "format", "(", "s", ")", ")", "return", "dttm" ]
Returns ``datetime.datetime`` from human readable strings >>> from datetime import date, timedelta >>> from dateutil.relativedelta import relativedelta >>> parse_human_datetime('2015-04-03') datetime.datetime(2015, 4, 3, 0, 0) >>> parse_human_datetime('2/3/1969') datetime.datetime(1969, 2, 3, 0, 0) >>> parse_human_datetime('now') <= datetime.now() True >>> parse_human_datetime('yesterday') <= datetime.now() True >>> date.today() - timedelta(1) == parse_human_datetime('yesterday').date() True >>> year_ago_1 = parse_human_datetime('one year ago').date() >>> year_ago_2 = (datetime.now() - relativedelta(years=1) ).date() >>> year_ago_1 == year_ago_2 True
[ "Returns", "datetime", ".", "datetime", "from", "human", "readable", "strings" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L197-L233
train
apache/incubator-superset
superset/utils/core.py
decode_dashboards
def decode_dashboards(o): """ Function to be passed into json.loads obj_hook parameter Recreates the dashboard object from a json representation. """ import superset.models.core as models from superset.connectors.sqla.models import ( SqlaTable, SqlMetric, TableColumn, ) if '__Dashboard__' in o: d = models.Dashboard() d.__dict__.update(o['__Dashboard__']) return d elif '__Slice__' in o: d = models.Slice() d.__dict__.update(o['__Slice__']) return d elif '__TableColumn__' in o: d = TableColumn() d.__dict__.update(o['__TableColumn__']) return d elif '__SqlaTable__' in o: d = SqlaTable() d.__dict__.update(o['__SqlaTable__']) return d elif '__SqlMetric__' in o: d = SqlMetric() d.__dict__.update(o['__SqlMetric__']) return d elif '__datetime__' in o: return datetime.strptime(o['__datetime__'], '%Y-%m-%dT%H:%M:%S') else: return o
python
def decode_dashboards(o): """ Function to be passed into json.loads obj_hook parameter Recreates the dashboard object from a json representation. """ import superset.models.core as models from superset.connectors.sqla.models import ( SqlaTable, SqlMetric, TableColumn, ) if '__Dashboard__' in o: d = models.Dashboard() d.__dict__.update(o['__Dashboard__']) return d elif '__Slice__' in o: d = models.Slice() d.__dict__.update(o['__Slice__']) return d elif '__TableColumn__' in o: d = TableColumn() d.__dict__.update(o['__TableColumn__']) return d elif '__SqlaTable__' in o: d = SqlaTable() d.__dict__.update(o['__SqlaTable__']) return d elif '__SqlMetric__' in o: d = SqlMetric() d.__dict__.update(o['__SqlMetric__']) return d elif '__datetime__' in o: return datetime.strptime(o['__datetime__'], '%Y-%m-%dT%H:%M:%S') else: return o
[ "def", "decode_dashboards", "(", "o", ")", ":", "import", "superset", ".", "models", ".", "core", "as", "models", "from", "superset", ".", "connectors", ".", "sqla", ".", "models", "import", "(", "SqlaTable", ",", "SqlMetric", ",", "TableColumn", ",", ")", "if", "'__Dashboard__'", "in", "o", ":", "d", "=", "models", ".", "Dashboard", "(", ")", "d", ".", "__dict__", ".", "update", "(", "o", "[", "'__Dashboard__'", "]", ")", "return", "d", "elif", "'__Slice__'", "in", "o", ":", "d", "=", "models", ".", "Slice", "(", ")", "d", ".", "__dict__", ".", "update", "(", "o", "[", "'__Slice__'", "]", ")", "return", "d", "elif", "'__TableColumn__'", "in", "o", ":", "d", "=", "TableColumn", "(", ")", "d", ".", "__dict__", ".", "update", "(", "o", "[", "'__TableColumn__'", "]", ")", "return", "d", "elif", "'__SqlaTable__'", "in", "o", ":", "d", "=", "SqlaTable", "(", ")", "d", ".", "__dict__", ".", "update", "(", "o", "[", "'__SqlaTable__'", "]", ")", "return", "d", "elif", "'__SqlMetric__'", "in", "o", ":", "d", "=", "SqlMetric", "(", ")", "d", ".", "__dict__", ".", "update", "(", "o", "[", "'__SqlMetric__'", "]", ")", "return", "d", "elif", "'__datetime__'", "in", "o", ":", "return", "datetime", ".", "strptime", "(", "o", "[", "'__datetime__'", "]", ",", "'%Y-%m-%dT%H:%M:%S'", ")", "else", ":", "return", "o" ]
Function to be passed into json.loads obj_hook parameter Recreates the dashboard object from a json representation.
[ "Function", "to", "be", "passed", "into", "json", ".", "loads", "obj_hook", "parameter", "Recreates", "the", "dashboard", "object", "from", "a", "json", "representation", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L241-L274
train
apache/incubator-superset
superset/utils/core.py
parse_human_timedelta
def parse_human_timedelta(s: str): """ Returns ``datetime.datetime`` from natural language time deltas >>> parse_human_datetime('now') <= datetime.now() True """ cal = parsedatetime.Calendar() dttm = dttm_from_timtuple(datetime.now().timetuple()) d = cal.parse(s or '', dttm)[0] d = datetime(d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec) return d - dttm
python
def parse_human_timedelta(s: str): """ Returns ``datetime.datetime`` from natural language time deltas >>> parse_human_datetime('now') <= datetime.now() True """ cal = parsedatetime.Calendar() dttm = dttm_from_timtuple(datetime.now().timetuple()) d = cal.parse(s or '', dttm)[0] d = datetime(d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec) return d - dttm
[ "def", "parse_human_timedelta", "(", "s", ":", "str", ")", ":", "cal", "=", "parsedatetime", ".", "Calendar", "(", ")", "dttm", "=", "dttm_from_timtuple", "(", "datetime", ".", "now", "(", ")", ".", "timetuple", "(", ")", ")", "d", "=", "cal", ".", "parse", "(", "s", "or", "''", ",", "dttm", ")", "[", "0", "]", "d", "=", "datetime", "(", "d", ".", "tm_year", ",", "d", ".", "tm_mon", ",", "d", ".", "tm_mday", ",", "d", ".", "tm_hour", ",", "d", ".", "tm_min", ",", "d", ".", "tm_sec", ")", "return", "d", "-", "dttm" ]
Returns ``datetime.datetime`` from natural language time deltas >>> parse_human_datetime('now') <= datetime.now() True
[ "Returns", "datetime", ".", "datetime", "from", "natural", "language", "time", "deltas" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L290-L301
train
apache/incubator-superset
superset/utils/core.py
datetime_f
def datetime_f(dttm): """Formats datetime to take less room when it is recent""" if dttm: dttm = dttm.isoformat() now_iso = datetime.now().isoformat() if now_iso[:10] == dttm[:10]: dttm = dttm[11:] elif now_iso[:4] == dttm[:4]: dttm = dttm[5:] return '<nobr>{}</nobr>'.format(dttm)
python
def datetime_f(dttm): """Formats datetime to take less room when it is recent""" if dttm: dttm = dttm.isoformat() now_iso = datetime.now().isoformat() if now_iso[:10] == dttm[:10]: dttm = dttm[11:] elif now_iso[:4] == dttm[:4]: dttm = dttm[5:] return '<nobr>{}</nobr>'.format(dttm)
[ "def", "datetime_f", "(", "dttm", ")", ":", "if", "dttm", ":", "dttm", "=", "dttm", ".", "isoformat", "(", ")", "now_iso", "=", "datetime", ".", "now", "(", ")", ".", "isoformat", "(", ")", "if", "now_iso", "[", ":", "10", "]", "==", "dttm", "[", ":", "10", "]", ":", "dttm", "=", "dttm", "[", "11", ":", "]", "elif", "now_iso", "[", ":", "4", "]", "==", "dttm", "[", ":", "4", "]", ":", "dttm", "=", "dttm", "[", "5", ":", "]", "return", "'<nobr>{}</nobr>'", ".", "format", "(", "dttm", ")" ]
Formats datetime to take less room when it is recent
[ "Formats", "datetime", "to", "take", "less", "room", "when", "it", "is", "recent" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L321-L330
train
apache/incubator-superset
superset/utils/core.py
json_iso_dttm_ser
def json_iso_dttm_ser(obj, pessimistic: Optional[bool] = False): """ json serializer that deals with dates >>> dttm = datetime(1970, 1, 1) >>> json.dumps({'dttm': dttm}, default=json_iso_dttm_ser) '{"dttm": "1970-01-01T00:00:00"}' """ val = base_json_conv(obj) if val is not None: return val if isinstance(obj, (datetime, date, time, pd.Timestamp)): obj = obj.isoformat() else: if pessimistic: return 'Unserializable [{}]'.format(type(obj)) else: raise TypeError( 'Unserializable object {} of type {}'.format(obj, type(obj))) return obj
python
def json_iso_dttm_ser(obj, pessimistic: Optional[bool] = False): """ json serializer that deals with dates >>> dttm = datetime(1970, 1, 1) >>> json.dumps({'dttm': dttm}, default=json_iso_dttm_ser) '{"dttm": "1970-01-01T00:00:00"}' """ val = base_json_conv(obj) if val is not None: return val if isinstance(obj, (datetime, date, time, pd.Timestamp)): obj = obj.isoformat() else: if pessimistic: return 'Unserializable [{}]'.format(type(obj)) else: raise TypeError( 'Unserializable object {} of type {}'.format(obj, type(obj))) return obj
[ "def", "json_iso_dttm_ser", "(", "obj", ",", "pessimistic", ":", "Optional", "[", "bool", "]", "=", "False", ")", ":", "val", "=", "base_json_conv", "(", "obj", ")", "if", "val", "is", "not", "None", ":", "return", "val", "if", "isinstance", "(", "obj", ",", "(", "datetime", ",", "date", ",", "time", ",", "pd", ".", "Timestamp", ")", ")", ":", "obj", "=", "obj", ".", "isoformat", "(", ")", "else", ":", "if", "pessimistic", ":", "return", "'Unserializable [{}]'", ".", "format", "(", "type", "(", "obj", ")", ")", "else", ":", "raise", "TypeError", "(", "'Unserializable object {} of type {}'", ".", "format", "(", "obj", ",", "type", "(", "obj", ")", ")", ")", "return", "obj" ]
json serializer that deals with dates >>> dttm = datetime(1970, 1, 1) >>> json.dumps({'dttm': dttm}, default=json_iso_dttm_ser) '{"dttm": "1970-01-01T00:00:00"}'
[ "json", "serializer", "that", "deals", "with", "dates" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L355-L374
train
apache/incubator-superset
superset/utils/core.py
json_int_dttm_ser
def json_int_dttm_ser(obj): """json serializer that deals with dates""" val = base_json_conv(obj) if val is not None: return val if isinstance(obj, (datetime, pd.Timestamp)): obj = datetime_to_epoch(obj) elif isinstance(obj, date): obj = (obj - EPOCH.date()).total_seconds() * 1000 else: raise TypeError( 'Unserializable object {} of type {}'.format(obj, type(obj))) return obj
python
def json_int_dttm_ser(obj): """json serializer that deals with dates""" val = base_json_conv(obj) if val is not None: return val if isinstance(obj, (datetime, pd.Timestamp)): obj = datetime_to_epoch(obj) elif isinstance(obj, date): obj = (obj - EPOCH.date()).total_seconds() * 1000 else: raise TypeError( 'Unserializable object {} of type {}'.format(obj, type(obj))) return obj
[ "def", "json_int_dttm_ser", "(", "obj", ")", ":", "val", "=", "base_json_conv", "(", "obj", ")", "if", "val", "is", "not", "None", ":", "return", "val", "if", "isinstance", "(", "obj", ",", "(", "datetime", ",", "pd", ".", "Timestamp", ")", ")", ":", "obj", "=", "datetime_to_epoch", "(", "obj", ")", "elif", "isinstance", "(", "obj", ",", "date", ")", ":", "obj", "=", "(", "obj", "-", "EPOCH", ".", "date", "(", ")", ")", ".", "total_seconds", "(", ")", "*", "1000", "else", ":", "raise", "TypeError", "(", "'Unserializable object {} of type {}'", ".", "format", "(", "obj", ",", "type", "(", "obj", ")", ")", ")", "return", "obj" ]
json serializer that deals with dates
[ "json", "serializer", "that", "deals", "with", "dates" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L384-L396
train
apache/incubator-superset
superset/utils/core.py
error_msg_from_exception
def error_msg_from_exception(e): """Translate exception into error message Database have different ways to handle exception. This function attempts to make sense of the exception object and construct a human readable sentence. TODO(bkyryliuk): parse the Presto error message from the connection created via create_engine. engine = create_engine('presto://localhost:3506/silver') - gives an e.message as the str(dict) presto.connect('localhost', port=3506, catalog='silver') - as a dict. The latter version is parsed correctly by this function. """ msg = '' if hasattr(e, 'message'): if isinstance(e.message, dict): msg = e.message.get('message') elif e.message: msg = '{}'.format(e.message) return msg or '{}'.format(e)
python
def error_msg_from_exception(e): """Translate exception into error message Database have different ways to handle exception. This function attempts to make sense of the exception object and construct a human readable sentence. TODO(bkyryliuk): parse the Presto error message from the connection created via create_engine. engine = create_engine('presto://localhost:3506/silver') - gives an e.message as the str(dict) presto.connect('localhost', port=3506, catalog='silver') - as a dict. The latter version is parsed correctly by this function. """ msg = '' if hasattr(e, 'message'): if isinstance(e.message, dict): msg = e.message.get('message') elif e.message: msg = '{}'.format(e.message) return msg or '{}'.format(e)
[ "def", "error_msg_from_exception", "(", "e", ")", ":", "msg", "=", "''", "if", "hasattr", "(", "e", ",", "'message'", ")", ":", "if", "isinstance", "(", "e", ".", "message", ",", "dict", ")", ":", "msg", "=", "e", ".", "message", ".", "get", "(", "'message'", ")", "elif", "e", ".", "message", ":", "msg", "=", "'{}'", ".", "format", "(", "e", ".", "message", ")", "return", "msg", "or", "'{}'", ".", "format", "(", "e", ")" ]
Translate exception into error message Database have different ways to handle exception. This function attempts to make sense of the exception object and construct a human readable sentence. TODO(bkyryliuk): parse the Presto error message from the connection created via create_engine. engine = create_engine('presto://localhost:3506/silver') - gives an e.message as the str(dict) presto.connect('localhost', port=3506, catalog='silver') - as a dict. The latter version is parsed correctly by this function.
[ "Translate", "exception", "into", "error", "message" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L403-L423
train
apache/incubator-superset
superset/utils/core.py
generic_find_constraint_name
def generic_find_constraint_name(table, columns, referenced, db): """Utility to find a constraint name in alembic migrations""" t = sa.Table(table, db.metadata, autoload=True, autoload_with=db.engine) for fk in t.foreign_key_constraints: if fk.referred_table.name == referenced and set(fk.column_keys) == columns: return fk.name
python
def generic_find_constraint_name(table, columns, referenced, db): """Utility to find a constraint name in alembic migrations""" t = sa.Table(table, db.metadata, autoload=True, autoload_with=db.engine) for fk in t.foreign_key_constraints: if fk.referred_table.name == referenced and set(fk.column_keys) == columns: return fk.name
[ "def", "generic_find_constraint_name", "(", "table", ",", "columns", ",", "referenced", ",", "db", ")", ":", "t", "=", "sa", ".", "Table", "(", "table", ",", "db", ".", "metadata", ",", "autoload", "=", "True", ",", "autoload_with", "=", "db", ".", "engine", ")", "for", "fk", "in", "t", ".", "foreign_key_constraints", ":", "if", "fk", ".", "referred_table", ".", "name", "==", "referenced", "and", "set", "(", "fk", ".", "column_keys", ")", "==", "columns", ":", "return", "fk", ".", "name" ]
Utility to find a constraint name in alembic migrations
[ "Utility", "to", "find", "a", "constraint", "name", "in", "alembic", "migrations" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L450-L456
train
apache/incubator-superset
superset/utils/core.py
generic_find_fk_constraint_name
def generic_find_fk_constraint_name(table, columns, referenced, insp): """Utility to find a foreign-key constraint name in alembic migrations""" for fk in insp.get_foreign_keys(table): if fk['referred_table'] == referenced and set(fk['referred_columns']) == columns: return fk['name']
python
def generic_find_fk_constraint_name(table, columns, referenced, insp): """Utility to find a foreign-key constraint name in alembic migrations""" for fk in insp.get_foreign_keys(table): if fk['referred_table'] == referenced and set(fk['referred_columns']) == columns: return fk['name']
[ "def", "generic_find_fk_constraint_name", "(", "table", ",", "columns", ",", "referenced", ",", "insp", ")", ":", "for", "fk", "in", "insp", ".", "get_foreign_keys", "(", "table", ")", ":", "if", "fk", "[", "'referred_table'", "]", "==", "referenced", "and", "set", "(", "fk", "[", "'referred_columns'", "]", ")", "==", "columns", ":", "return", "fk", "[", "'name'", "]" ]
Utility to find a foreign-key constraint name in alembic migrations
[ "Utility", "to", "find", "a", "foreign", "-", "key", "constraint", "name", "in", "alembic", "migrations" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L459-L463
train
apache/incubator-superset
superset/utils/core.py
generic_find_fk_constraint_names
def generic_find_fk_constraint_names(table, columns, referenced, insp): """Utility to find foreign-key constraint names in alembic migrations""" names = set() for fk in insp.get_foreign_keys(table): if fk['referred_table'] == referenced and set(fk['referred_columns']) == columns: names.add(fk['name']) return names
python
def generic_find_fk_constraint_names(table, columns, referenced, insp): """Utility to find foreign-key constraint names in alembic migrations""" names = set() for fk in insp.get_foreign_keys(table): if fk['referred_table'] == referenced and set(fk['referred_columns']) == columns: names.add(fk['name']) return names
[ "def", "generic_find_fk_constraint_names", "(", "table", ",", "columns", ",", "referenced", ",", "insp", ")", ":", "names", "=", "set", "(", ")", "for", "fk", "in", "insp", ".", "get_foreign_keys", "(", "table", ")", ":", "if", "fk", "[", "'referred_table'", "]", "==", "referenced", "and", "set", "(", "fk", "[", "'referred_columns'", "]", ")", "==", "columns", ":", "names", ".", "add", "(", "fk", "[", "'name'", "]", ")", "return", "names" ]
Utility to find foreign-key constraint names in alembic migrations
[ "Utility", "to", "find", "foreign", "-", "key", "constraint", "names", "in", "alembic", "migrations" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L466-L474
train
apache/incubator-superset
superset/utils/core.py
generic_find_uq_constraint_name
def generic_find_uq_constraint_name(table, columns, insp): """Utility to find a unique constraint name in alembic migrations""" for uq in insp.get_unique_constraints(table): if columns == set(uq['column_names']): return uq['name']
python
def generic_find_uq_constraint_name(table, columns, insp): """Utility to find a unique constraint name in alembic migrations""" for uq in insp.get_unique_constraints(table): if columns == set(uq['column_names']): return uq['name']
[ "def", "generic_find_uq_constraint_name", "(", "table", ",", "columns", ",", "insp", ")", ":", "for", "uq", "in", "insp", ".", "get_unique_constraints", "(", "table", ")", ":", "if", "columns", "==", "set", "(", "uq", "[", "'column_names'", "]", ")", ":", "return", "uq", "[", "'name'", "]" ]
Utility to find a unique constraint name in alembic migrations
[ "Utility", "to", "find", "a", "unique", "constraint", "name", "in", "alembic", "migrations" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L477-L482
train
apache/incubator-superset
superset/utils/core.py
table_has_constraint
def table_has_constraint(table, name, db): """Utility to find a constraint name in alembic migrations""" t = sa.Table(table, db.metadata, autoload=True, autoload_with=db.engine) for c in t.constraints: if c.name == name: return True return False
python
def table_has_constraint(table, name, db): """Utility to find a constraint name in alembic migrations""" t = sa.Table(table, db.metadata, autoload=True, autoload_with=db.engine) for c in t.constraints: if c.name == name: return True return False
[ "def", "table_has_constraint", "(", "table", ",", "name", ",", "db", ")", ":", "t", "=", "sa", ".", "Table", "(", "table", ",", "db", ".", "metadata", ",", "autoload", "=", "True", ",", "autoload_with", "=", "db", ".", "engine", ")", "for", "c", "in", "t", ".", "constraints", ":", "if", "c", ".", "name", "==", "name", ":", "return", "True", "return", "False" ]
Utility to find a constraint name in alembic migrations
[ "Utility", "to", "find", "a", "constraint", "name", "in", "alembic", "migrations" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L499-L506
train
apache/incubator-superset
superset/utils/core.py
send_email_smtp
def send_email_smtp(to, subject, html_content, config, files=None, data=None, images=None, dryrun=False, cc=None, bcc=None, mime_subtype='mixed'): """ Send an email with html content, eg: send_email_smtp( 'test@example.com', 'foo', '<b>Foo</b> bar',['/dev/null'], dryrun=True) """ smtp_mail_from = config.get('SMTP_MAIL_FROM') to = get_email_address_list(to) msg = MIMEMultipart(mime_subtype) msg['Subject'] = subject msg['From'] = smtp_mail_from msg['To'] = ', '.join(to) msg.preamble = 'This is a multi-part message in MIME format.' recipients = to if cc: cc = get_email_address_list(cc) msg['CC'] = ', '.join(cc) recipients = recipients + cc if bcc: # don't add bcc in header bcc = get_email_address_list(bcc) recipients = recipients + bcc msg['Date'] = formatdate(localtime=True) mime_text = MIMEText(html_content, 'html') msg.attach(mime_text) # Attach files by reading them from disk for fname in files or []: basename = os.path.basename(fname) with open(fname, 'rb') as f: msg.attach( MIMEApplication( f.read(), Content_Disposition="attachment; filename='%s'" % basename, Name=basename)) # Attach any files passed directly for name, body in (data or {}).items(): msg.attach( MIMEApplication( body, Content_Disposition="attachment; filename='%s'" % name, Name=name, )) # Attach any inline images, which may be required for display in # HTML content (inline) for msgid, body in (images or {}).items(): image = MIMEImage(body) image.add_header('Content-ID', '<%s>' % msgid) image.add_header('Content-Disposition', 'inline') msg.attach(image) send_MIME_email(smtp_mail_from, recipients, msg, config, dryrun=dryrun)
python
def send_email_smtp(to, subject, html_content, config, files=None, data=None, images=None, dryrun=False, cc=None, bcc=None, mime_subtype='mixed'): """ Send an email with html content, eg: send_email_smtp( 'test@example.com', 'foo', '<b>Foo</b> bar',['/dev/null'], dryrun=True) """ smtp_mail_from = config.get('SMTP_MAIL_FROM') to = get_email_address_list(to) msg = MIMEMultipart(mime_subtype) msg['Subject'] = subject msg['From'] = smtp_mail_from msg['To'] = ', '.join(to) msg.preamble = 'This is a multi-part message in MIME format.' recipients = to if cc: cc = get_email_address_list(cc) msg['CC'] = ', '.join(cc) recipients = recipients + cc if bcc: # don't add bcc in header bcc = get_email_address_list(bcc) recipients = recipients + bcc msg['Date'] = formatdate(localtime=True) mime_text = MIMEText(html_content, 'html') msg.attach(mime_text) # Attach files by reading them from disk for fname in files or []: basename = os.path.basename(fname) with open(fname, 'rb') as f: msg.attach( MIMEApplication( f.read(), Content_Disposition="attachment; filename='%s'" % basename, Name=basename)) # Attach any files passed directly for name, body in (data or {}).items(): msg.attach( MIMEApplication( body, Content_Disposition="attachment; filename='%s'" % name, Name=name, )) # Attach any inline images, which may be required for display in # HTML content (inline) for msgid, body in (images or {}).items(): image = MIMEImage(body) image.add_header('Content-ID', '<%s>' % msgid) image.add_header('Content-Disposition', 'inline') msg.attach(image) send_MIME_email(smtp_mail_from, recipients, msg, config, dryrun=dryrun)
[ "def", "send_email_smtp", "(", "to", ",", "subject", ",", "html_content", ",", "config", ",", "files", "=", "None", ",", "data", "=", "None", ",", "images", "=", "None", ",", "dryrun", "=", "False", ",", "cc", "=", "None", ",", "bcc", "=", "None", ",", "mime_subtype", "=", "'mixed'", ")", ":", "smtp_mail_from", "=", "config", ".", "get", "(", "'SMTP_MAIL_FROM'", ")", "to", "=", "get_email_address_list", "(", "to", ")", "msg", "=", "MIMEMultipart", "(", "mime_subtype", ")", "msg", "[", "'Subject'", "]", "=", "subject", "msg", "[", "'From'", "]", "=", "smtp_mail_from", "msg", "[", "'To'", "]", "=", "', '", ".", "join", "(", "to", ")", "msg", ".", "preamble", "=", "'This is a multi-part message in MIME format.'", "recipients", "=", "to", "if", "cc", ":", "cc", "=", "get_email_address_list", "(", "cc", ")", "msg", "[", "'CC'", "]", "=", "', '", ".", "join", "(", "cc", ")", "recipients", "=", "recipients", "+", "cc", "if", "bcc", ":", "# don't add bcc in header", "bcc", "=", "get_email_address_list", "(", "bcc", ")", "recipients", "=", "recipients", "+", "bcc", "msg", "[", "'Date'", "]", "=", "formatdate", "(", "localtime", "=", "True", ")", "mime_text", "=", "MIMEText", "(", "html_content", ",", "'html'", ")", "msg", ".", "attach", "(", "mime_text", ")", "# Attach files by reading them from disk", "for", "fname", "in", "files", "or", "[", "]", ":", "basename", "=", "os", ".", "path", ".", "basename", "(", "fname", ")", "with", "open", "(", "fname", ",", "'rb'", ")", "as", "f", ":", "msg", ".", "attach", "(", "MIMEApplication", "(", "f", ".", "read", "(", ")", ",", "Content_Disposition", "=", "\"attachment; filename='%s'\"", "%", "basename", ",", "Name", "=", "basename", ")", ")", "# Attach any files passed directly", "for", "name", ",", "body", "in", "(", "data", "or", "{", "}", ")", ".", "items", "(", ")", ":", "msg", ".", "attach", "(", "MIMEApplication", "(", "body", ",", "Content_Disposition", "=", "\"attachment; filename='%s'\"", "%", "name", ",", "Name", "=", "name", ",", ")", ")", "# Attach any inline images, which may be required for display in", "# HTML content (inline)", "for", "msgid", ",", "body", "in", "(", "images", "or", "{", "}", ")", ".", "items", "(", ")", ":", "image", "=", "MIMEImage", "(", "body", ")", "image", ".", "add_header", "(", "'Content-ID'", ",", "'<%s>'", "%", "msgid", ")", "image", ".", "add_header", "(", "'Content-Disposition'", ",", "'inline'", ")", "msg", ".", "attach", "(", "image", ")", "send_MIME_email", "(", "smtp_mail_from", ",", "recipients", ",", "msg", ",", "config", ",", "dryrun", "=", "dryrun", ")" ]
Send an email with html content, eg: send_email_smtp( 'test@example.com', 'foo', '<b>Foo</b> bar',['/dev/null'], dryrun=True)
[ "Send", "an", "email", "with", "html", "content", "eg", ":", "send_email_smtp", "(", "test" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L598-L657
train
apache/incubator-superset
superset/utils/core.py
setup_cache
def setup_cache(app: Flask, cache_config) -> Optional[Cache]: """Setup the flask-cache on a flask app""" if cache_config and cache_config.get('CACHE_TYPE') != 'null': return Cache(app, config=cache_config) return None
python
def setup_cache(app: Flask, cache_config) -> Optional[Cache]: """Setup the flask-cache on a flask app""" if cache_config and cache_config.get('CACHE_TYPE') != 'null': return Cache(app, config=cache_config) return None
[ "def", "setup_cache", "(", "app", ":", "Flask", ",", "cache_config", ")", "->", "Optional", "[", "Cache", "]", ":", "if", "cache_config", "and", "cache_config", ".", "get", "(", "'CACHE_TYPE'", ")", "!=", "'null'", ":", "return", "Cache", "(", "app", ",", "config", "=", "cache_config", ")", "return", "None" ]
Setup the flask-cache on a flask app
[ "Setup", "the", "flask", "-", "cache", "on", "a", "flask", "app" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L702-L707
train
apache/incubator-superset
superset/utils/core.py
zlib_compress
def zlib_compress(data): """ Compress things in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str) """ if PY3K: if isinstance(data, str): return zlib.compress(bytes(data, 'utf-8')) return zlib.compress(data) return zlib.compress(data)
python
def zlib_compress(data): """ Compress things in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str) """ if PY3K: if isinstance(data, str): return zlib.compress(bytes(data, 'utf-8')) return zlib.compress(data) return zlib.compress(data)
[ "def", "zlib_compress", "(", "data", ")", ":", "if", "PY3K", ":", "if", "isinstance", "(", "data", ",", "str", ")", ":", "return", "zlib", ".", "compress", "(", "bytes", "(", "data", ",", "'utf-8'", ")", ")", "return", "zlib", ".", "compress", "(", "data", ")", "return", "zlib", ".", "compress", "(", "data", ")" ]
Compress things in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str)
[ "Compress", "things", "in", "a", "py2", "/", "3", "safe", "fashion", ">>>", "json_str", "=", "{", "test", ":", "1", "}", ">>>", "blob", "=", "zlib_compress", "(", "json_str", ")" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L710-L720
train
apache/incubator-superset
superset/utils/core.py
zlib_decompress_to_string
def zlib_decompress_to_string(blob): """ Decompress things to a string in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str) >>> got_str = zlib_decompress_to_string(blob) >>> got_str == json_str True """ if PY3K: if isinstance(blob, bytes): decompressed = zlib.decompress(blob) else: decompressed = zlib.decompress(bytes(blob, 'utf-8')) return decompressed.decode('utf-8') return zlib.decompress(blob)
python
def zlib_decompress_to_string(blob): """ Decompress things to a string in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str) >>> got_str = zlib_decompress_to_string(blob) >>> got_str == json_str True """ if PY3K: if isinstance(blob, bytes): decompressed = zlib.decompress(blob) else: decompressed = zlib.decompress(bytes(blob, 'utf-8')) return decompressed.decode('utf-8') return zlib.decompress(blob)
[ "def", "zlib_decompress_to_string", "(", "blob", ")", ":", "if", "PY3K", ":", "if", "isinstance", "(", "blob", ",", "bytes", ")", ":", "decompressed", "=", "zlib", ".", "decompress", "(", "blob", ")", "else", ":", "decompressed", "=", "zlib", ".", "decompress", "(", "bytes", "(", "blob", ",", "'utf-8'", ")", ")", "return", "decompressed", ".", "decode", "(", "'utf-8'", ")", "return", "zlib", ".", "decompress", "(", "blob", ")" ]
Decompress things to a string in a py2/3 safe fashion >>> json_str = '{"test": 1}' >>> blob = zlib_compress(json_str) >>> got_str = zlib_decompress_to_string(blob) >>> got_str == json_str True
[ "Decompress", "things", "to", "a", "string", "in", "a", "py2", "/", "3", "safe", "fashion", ">>>", "json_str", "=", "{", "test", ":", "1", "}", ">>>", "blob", "=", "zlib_compress", "(", "json_str", ")", ">>>", "got_str", "=", "zlib_decompress_to_string", "(", "blob", ")", ">>>", "got_str", "==", "json_str", "True" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L723-L738
train
apache/incubator-superset
superset/utils/core.py
user_label
def user_label(user: User) -> Optional[str]: """Given a user ORM FAB object, returns a label""" if user: if user.first_name and user.last_name: return user.first_name + ' ' + user.last_name else: return user.username return None
python
def user_label(user: User) -> Optional[str]: """Given a user ORM FAB object, returns a label""" if user: if user.first_name and user.last_name: return user.first_name + ' ' + user.last_name else: return user.username return None
[ "def", "user_label", "(", "user", ":", "User", ")", "->", "Optional", "[", "str", "]", ":", "if", "user", ":", "if", "user", ".", "first_name", "and", "user", ".", "last_name", ":", "return", "user", ".", "first_name", "+", "' '", "+", "user", ".", "last_name", "else", ":", "return", "user", ".", "username", "return", "None" ]
Given a user ORM FAB object, returns a label
[ "Given", "a", "user", "ORM", "FAB", "object", "returns", "a", "label" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L860-L868
train
apache/incubator-superset
superset/utils/core.py
get_since_until
def get_since_until(time_range: Optional[str] = None, since: Optional[str] = None, until: Optional[str] = None, time_shift: Optional[str] = None, relative_end: Optional[str] = None) -> Tuple[datetime, datetime]: """Return `since` and `until` date time tuple from string representations of time_range, since, until and time_shift. This functiom supports both reading the keys separately (from `since` and `until`), as well as the new `time_range` key. Valid formats are: - ISO 8601 - X days/years/hours/day/year/weeks - X days/years/hours/day/year/weeks ago - X days/years/hours/day/year/weeks from now - freeform Additionally, for `time_range` (these specify both `since` and `until`): - Last day - Last week - Last month - Last quarter - Last year - No filter - Last X seconds/minutes/hours/days/weeks/months/years - Next X seconds/minutes/hours/days/weeks/months/years """ separator = ' : ' relative_end = parse_human_datetime(relative_end if relative_end else 'today') common_time_frames = { 'Last day': (relative_end - relativedelta(days=1), relative_end), # noqa: T400 'Last week': (relative_end - relativedelta(weeks=1), relative_end), # noqa: T400 'Last month': (relative_end - relativedelta(months=1), relative_end), # noqa: E501, T400 'Last quarter': (relative_end - relativedelta(months=3), relative_end), # noqa: E501, T400 'Last year': (relative_end - relativedelta(years=1), relative_end), # noqa: T400 } if time_range: if separator in time_range: since, until = time_range.split(separator, 1) if since and since not in common_time_frames: since = add_ago_to_since(since) since = parse_human_datetime(since) until = parse_human_datetime(until) elif time_range in common_time_frames: since, until = common_time_frames[time_range] elif time_range == 'No filter': since = until = None else: rel, num, grain = time_range.split() if rel == 'Last': since = relative_end - relativedelta(**{grain: int(num)}) # noqa: T400 until = relative_end else: # rel == 'Next' since = relative_end until = relative_end + relativedelta(**{grain: int(num)}) # noqa: T400 else: since = since or '' if since: since = add_ago_to_since(since) since = parse_human_datetime(since) until = parse_human_datetime(until) if until else relative_end if time_shift: time_shift = parse_human_timedelta(time_shift) since = since if since is None else (since - time_shift) # noqa: T400 until = until if until is None else (until - time_shift) # noqa: T400 if since and until and since > until: raise ValueError(_('From date cannot be larger than to date')) return since, until
python
def get_since_until(time_range: Optional[str] = None, since: Optional[str] = None, until: Optional[str] = None, time_shift: Optional[str] = None, relative_end: Optional[str] = None) -> Tuple[datetime, datetime]: """Return `since` and `until` date time tuple from string representations of time_range, since, until and time_shift. This functiom supports both reading the keys separately (from `since` and `until`), as well as the new `time_range` key. Valid formats are: - ISO 8601 - X days/years/hours/day/year/weeks - X days/years/hours/day/year/weeks ago - X days/years/hours/day/year/weeks from now - freeform Additionally, for `time_range` (these specify both `since` and `until`): - Last day - Last week - Last month - Last quarter - Last year - No filter - Last X seconds/minutes/hours/days/weeks/months/years - Next X seconds/minutes/hours/days/weeks/months/years """ separator = ' : ' relative_end = parse_human_datetime(relative_end if relative_end else 'today') common_time_frames = { 'Last day': (relative_end - relativedelta(days=1), relative_end), # noqa: T400 'Last week': (relative_end - relativedelta(weeks=1), relative_end), # noqa: T400 'Last month': (relative_end - relativedelta(months=1), relative_end), # noqa: E501, T400 'Last quarter': (relative_end - relativedelta(months=3), relative_end), # noqa: E501, T400 'Last year': (relative_end - relativedelta(years=1), relative_end), # noqa: T400 } if time_range: if separator in time_range: since, until = time_range.split(separator, 1) if since and since not in common_time_frames: since = add_ago_to_since(since) since = parse_human_datetime(since) until = parse_human_datetime(until) elif time_range in common_time_frames: since, until = common_time_frames[time_range] elif time_range == 'No filter': since = until = None else: rel, num, grain = time_range.split() if rel == 'Last': since = relative_end - relativedelta(**{grain: int(num)}) # noqa: T400 until = relative_end else: # rel == 'Next' since = relative_end until = relative_end + relativedelta(**{grain: int(num)}) # noqa: T400 else: since = since or '' if since: since = add_ago_to_since(since) since = parse_human_datetime(since) until = parse_human_datetime(until) if until else relative_end if time_shift: time_shift = parse_human_timedelta(time_shift) since = since if since is None else (since - time_shift) # noqa: T400 until = until if until is None else (until - time_shift) # noqa: T400 if since and until and since > until: raise ValueError(_('From date cannot be larger than to date')) return since, until
[ "def", "get_since_until", "(", "time_range", ":", "Optional", "[", "str", "]", "=", "None", ",", "since", ":", "Optional", "[", "str", "]", "=", "None", ",", "until", ":", "Optional", "[", "str", "]", "=", "None", ",", "time_shift", ":", "Optional", "[", "str", "]", "=", "None", ",", "relative_end", ":", "Optional", "[", "str", "]", "=", "None", ")", "->", "Tuple", "[", "datetime", ",", "datetime", "]", ":", "separator", "=", "' : '", "relative_end", "=", "parse_human_datetime", "(", "relative_end", "if", "relative_end", "else", "'today'", ")", "common_time_frames", "=", "{", "'Last day'", ":", "(", "relative_end", "-", "relativedelta", "(", "days", "=", "1", ")", ",", "relative_end", ")", ",", "# noqa: T400", "'Last week'", ":", "(", "relative_end", "-", "relativedelta", "(", "weeks", "=", "1", ")", ",", "relative_end", ")", ",", "# noqa: T400", "'Last month'", ":", "(", "relative_end", "-", "relativedelta", "(", "months", "=", "1", ")", ",", "relative_end", ")", ",", "# noqa: E501, T400", "'Last quarter'", ":", "(", "relative_end", "-", "relativedelta", "(", "months", "=", "3", ")", ",", "relative_end", ")", ",", "# noqa: E501, T400", "'Last year'", ":", "(", "relative_end", "-", "relativedelta", "(", "years", "=", "1", ")", ",", "relative_end", ")", ",", "# noqa: T400", "}", "if", "time_range", ":", "if", "separator", "in", "time_range", ":", "since", ",", "until", "=", "time_range", ".", "split", "(", "separator", ",", "1", ")", "if", "since", "and", "since", "not", "in", "common_time_frames", ":", "since", "=", "add_ago_to_since", "(", "since", ")", "since", "=", "parse_human_datetime", "(", "since", ")", "until", "=", "parse_human_datetime", "(", "until", ")", "elif", "time_range", "in", "common_time_frames", ":", "since", ",", "until", "=", "common_time_frames", "[", "time_range", "]", "elif", "time_range", "==", "'No filter'", ":", "since", "=", "until", "=", "None", "else", ":", "rel", ",", "num", ",", "grain", "=", "time_range", ".", "split", "(", ")", "if", "rel", "==", "'Last'", ":", "since", "=", "relative_end", "-", "relativedelta", "(", "*", "*", "{", "grain", ":", "int", "(", "num", ")", "}", ")", "# noqa: T400", "until", "=", "relative_end", "else", ":", "# rel == 'Next'", "since", "=", "relative_end", "until", "=", "relative_end", "+", "relativedelta", "(", "*", "*", "{", "grain", ":", "int", "(", "num", ")", "}", ")", "# noqa: T400", "else", ":", "since", "=", "since", "or", "''", "if", "since", ":", "since", "=", "add_ago_to_since", "(", "since", ")", "since", "=", "parse_human_datetime", "(", "since", ")", "until", "=", "parse_human_datetime", "(", "until", ")", "if", "until", "else", "relative_end", "if", "time_shift", ":", "time_shift", "=", "parse_human_timedelta", "(", "time_shift", ")", "since", "=", "since", "if", "since", "is", "None", "else", "(", "since", "-", "time_shift", ")", "# noqa: T400", "until", "=", "until", "if", "until", "is", "None", "else", "(", "until", "-", "time_shift", ")", "# noqa: T400", "if", "since", "and", "until", "and", "since", ">", "until", ":", "raise", "ValueError", "(", "_", "(", "'From date cannot be larger than to date'", ")", ")", "return", "since", ",", "until" ]
Return `since` and `until` date time tuple from string representations of time_range, since, until and time_shift. This functiom supports both reading the keys separately (from `since` and `until`), as well as the new `time_range` key. Valid formats are: - ISO 8601 - X days/years/hours/day/year/weeks - X days/years/hours/day/year/weeks ago - X days/years/hours/day/year/weeks from now - freeform Additionally, for `time_range` (these specify both `since` and `until`): - Last day - Last week - Last month - Last quarter - Last year - No filter - Last X seconds/minutes/hours/days/weeks/months/years - Next X seconds/minutes/hours/days/weeks/months/years
[ "Return", "since", "and", "until", "date", "time", "tuple", "from", "string", "representations", "of", "time_range", "since", "until", "and", "time_shift", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L932-L1005
train
apache/incubator-superset
superset/utils/core.py
add_ago_to_since
def add_ago_to_since(since: str) -> str: """ Backwards compatibility hack. Without this slices with since: 7 days will be treated as 7 days in the future. :param str since: :returns: Since with ago added if necessary :rtype: str """ since_words = since.split(' ') grains = ['days', 'years', 'hours', 'day', 'year', 'weeks'] if (len(since_words) == 2 and since_words[1] in grains): since += ' ago' return since
python
def add_ago_to_since(since: str) -> str: """ Backwards compatibility hack. Without this slices with since: 7 days will be treated as 7 days in the future. :param str since: :returns: Since with ago added if necessary :rtype: str """ since_words = since.split(' ') grains = ['days', 'years', 'hours', 'day', 'year', 'weeks'] if (len(since_words) == 2 and since_words[1] in grains): since += ' ago' return since
[ "def", "add_ago_to_since", "(", "since", ":", "str", ")", "->", "str", ":", "since_words", "=", "since", ".", "split", "(", "' '", ")", "grains", "=", "[", "'days'", ",", "'years'", ",", "'hours'", ",", "'day'", ",", "'year'", ",", "'weeks'", "]", "if", "(", "len", "(", "since_words", ")", "==", "2", "and", "since_words", "[", "1", "]", "in", "grains", ")", ":", "since", "+=", "' ago'", "return", "since" ]
Backwards compatibility hack. Without this slices with since: 7 days will be treated as 7 days in the future. :param str since: :returns: Since with ago added if necessary :rtype: str
[ "Backwards", "compatibility", "hack", ".", "Without", "this", "slices", "with", "since", ":", "7", "days", "will", "be", "treated", "as", "7", "days", "in", "the", "future", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L1008-L1021
train
apache/incubator-superset
superset/utils/core.py
split_adhoc_filters_into_base_filters
def split_adhoc_filters_into_base_filters(fd): """ Mutates form data to restructure the adhoc filters in the form of the four base filters, `where`, `having`, `filters`, and `having_filters` which represent free form where sql, free form having sql, structured where clauses and structured having clauses. """ adhoc_filters = fd.get('adhoc_filters') if isinstance(adhoc_filters, list): simple_where_filters = [] simple_having_filters = [] sql_where_filters = [] sql_having_filters = [] for adhoc_filter in adhoc_filters: expression_type = adhoc_filter.get('expressionType') clause = adhoc_filter.get('clause') if expression_type == 'SIMPLE': if clause == 'WHERE': simple_where_filters.append({ 'col': adhoc_filter.get('subject'), 'op': adhoc_filter.get('operator'), 'val': adhoc_filter.get('comparator'), }) elif clause == 'HAVING': simple_having_filters.append({ 'col': adhoc_filter.get('subject'), 'op': adhoc_filter.get('operator'), 'val': adhoc_filter.get('comparator'), }) elif expression_type == 'SQL': if clause == 'WHERE': sql_where_filters.append(adhoc_filter.get('sqlExpression')) elif clause == 'HAVING': sql_having_filters.append(adhoc_filter.get('sqlExpression')) fd['where'] = ' AND '.join(['({})'.format(sql) for sql in sql_where_filters]) fd['having'] = ' AND '.join(['({})'.format(sql) for sql in sql_having_filters]) fd['having_filters'] = simple_having_filters fd['filters'] = simple_where_filters
python
def split_adhoc_filters_into_base_filters(fd): """ Mutates form data to restructure the adhoc filters in the form of the four base filters, `where`, `having`, `filters`, and `having_filters` which represent free form where sql, free form having sql, structured where clauses and structured having clauses. """ adhoc_filters = fd.get('adhoc_filters') if isinstance(adhoc_filters, list): simple_where_filters = [] simple_having_filters = [] sql_where_filters = [] sql_having_filters = [] for adhoc_filter in adhoc_filters: expression_type = adhoc_filter.get('expressionType') clause = adhoc_filter.get('clause') if expression_type == 'SIMPLE': if clause == 'WHERE': simple_where_filters.append({ 'col': adhoc_filter.get('subject'), 'op': adhoc_filter.get('operator'), 'val': adhoc_filter.get('comparator'), }) elif clause == 'HAVING': simple_having_filters.append({ 'col': adhoc_filter.get('subject'), 'op': adhoc_filter.get('operator'), 'val': adhoc_filter.get('comparator'), }) elif expression_type == 'SQL': if clause == 'WHERE': sql_where_filters.append(adhoc_filter.get('sqlExpression')) elif clause == 'HAVING': sql_having_filters.append(adhoc_filter.get('sqlExpression')) fd['where'] = ' AND '.join(['({})'.format(sql) for sql in sql_where_filters]) fd['having'] = ' AND '.join(['({})'.format(sql) for sql in sql_having_filters]) fd['having_filters'] = simple_having_filters fd['filters'] = simple_where_filters
[ "def", "split_adhoc_filters_into_base_filters", "(", "fd", ")", ":", "adhoc_filters", "=", "fd", ".", "get", "(", "'adhoc_filters'", ")", "if", "isinstance", "(", "adhoc_filters", ",", "list", ")", ":", "simple_where_filters", "=", "[", "]", "simple_having_filters", "=", "[", "]", "sql_where_filters", "=", "[", "]", "sql_having_filters", "=", "[", "]", "for", "adhoc_filter", "in", "adhoc_filters", ":", "expression_type", "=", "adhoc_filter", ".", "get", "(", "'expressionType'", ")", "clause", "=", "adhoc_filter", ".", "get", "(", "'clause'", ")", "if", "expression_type", "==", "'SIMPLE'", ":", "if", "clause", "==", "'WHERE'", ":", "simple_where_filters", ".", "append", "(", "{", "'col'", ":", "adhoc_filter", ".", "get", "(", "'subject'", ")", ",", "'op'", ":", "adhoc_filter", ".", "get", "(", "'operator'", ")", ",", "'val'", ":", "adhoc_filter", ".", "get", "(", "'comparator'", ")", ",", "}", ")", "elif", "clause", "==", "'HAVING'", ":", "simple_having_filters", ".", "append", "(", "{", "'col'", ":", "adhoc_filter", ".", "get", "(", "'subject'", ")", ",", "'op'", ":", "adhoc_filter", ".", "get", "(", "'operator'", ")", ",", "'val'", ":", "adhoc_filter", ".", "get", "(", "'comparator'", ")", ",", "}", ")", "elif", "expression_type", "==", "'SQL'", ":", "if", "clause", "==", "'WHERE'", ":", "sql_where_filters", ".", "append", "(", "adhoc_filter", ".", "get", "(", "'sqlExpression'", ")", ")", "elif", "clause", "==", "'HAVING'", ":", "sql_having_filters", ".", "append", "(", "adhoc_filter", ".", "get", "(", "'sqlExpression'", ")", ")", "fd", "[", "'where'", "]", "=", "' AND '", ".", "join", "(", "[", "'({})'", ".", "format", "(", "sql", ")", "for", "sql", "in", "sql_where_filters", "]", ")", "fd", "[", "'having'", "]", "=", "' AND '", ".", "join", "(", "[", "'({})'", ".", "format", "(", "sql", ")", "for", "sql", "in", "sql_having_filters", "]", ")", "fd", "[", "'having_filters'", "]", "=", "simple_having_filters", "fd", "[", "'filters'", "]", "=", "simple_where_filters" ]
Mutates form data to restructure the adhoc filters in the form of the four base filters, `where`, `having`, `filters`, and `having_filters` which represent free form where sql, free form having sql, structured where clauses and structured having clauses.
[ "Mutates", "form", "data", "to", "restructure", "the", "adhoc", "filters", "in", "the", "form", "of", "the", "four", "base", "filters", "where", "having", "filters", "and", "having_filters", "which", "represent", "free", "form", "where", "sql", "free", "form", "having", "sql", "structured", "where", "clauses", "and", "structured", "having", "clauses", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/utils/core.py#L1043-L1080
train
apache/incubator-superset
superset/data/energy.py
load_energy
def load_energy(): """Loads an energy related dataset to use with sankey and graphs""" tbl_name = 'energy_usage' data = get_example_data('energy.json.gz') pdf = pd.read_json(data) pdf.to_sql( tbl_name, db.engine, if_exists='replace', chunksize=500, dtype={ 'source': String(255), 'target': String(255), 'value': Float(), }, index=False) print('Creating table [wb_health_population] reference') tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first() if not tbl: tbl = TBL(table_name=tbl_name) tbl.description = 'Energy consumption' tbl.database = utils.get_or_create_main_db() if not any(col.metric_name == 'sum__value' for col in tbl.metrics): tbl.metrics.append(SqlMetric( metric_name='sum__value', expression='SUM(value)', )) db.session.merge(tbl) db.session.commit() tbl.fetch_metadata() slc = Slice( slice_name='Energy Sankey', viz_type='sankey', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "collapsed_fieldsets": "", "groupby": [ "source", "target" ], "having": "", "metric": "sum__value", "row_limit": "5000", "slice_name": "Energy Sankey", "viz_type": "sankey", "where": "" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc) slc = Slice( slice_name='Energy Force Layout', viz_type='directed_force', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "charge": "-500", "collapsed_fieldsets": "", "groupby": [ "source", "target" ], "having": "", "link_length": "200", "metric": "sum__value", "row_limit": "5000", "slice_name": "Force", "viz_type": "directed_force", "where": "" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc) slc = Slice( slice_name='Heatmap', viz_type='heatmap', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "all_columns_x": "source", "all_columns_y": "target", "canvas_image_rendering": "pixelated", "collapsed_fieldsets": "", "having": "", "linear_color_scheme": "blue_white_yellow", "metric": "sum__value", "normalize_across": "heatmap", "slice_name": "Heatmap", "viz_type": "heatmap", "where": "", "xscale_interval": "1", "yscale_interval": "1" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc)
python
def load_energy(): """Loads an energy related dataset to use with sankey and graphs""" tbl_name = 'energy_usage' data = get_example_data('energy.json.gz') pdf = pd.read_json(data) pdf.to_sql( tbl_name, db.engine, if_exists='replace', chunksize=500, dtype={ 'source': String(255), 'target': String(255), 'value': Float(), }, index=False) print('Creating table [wb_health_population] reference') tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first() if not tbl: tbl = TBL(table_name=tbl_name) tbl.description = 'Energy consumption' tbl.database = utils.get_or_create_main_db() if not any(col.metric_name == 'sum__value' for col in tbl.metrics): tbl.metrics.append(SqlMetric( metric_name='sum__value', expression='SUM(value)', )) db.session.merge(tbl) db.session.commit() tbl.fetch_metadata() slc = Slice( slice_name='Energy Sankey', viz_type='sankey', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "collapsed_fieldsets": "", "groupby": [ "source", "target" ], "having": "", "metric": "sum__value", "row_limit": "5000", "slice_name": "Energy Sankey", "viz_type": "sankey", "where": "" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc) slc = Slice( slice_name='Energy Force Layout', viz_type='directed_force', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "charge": "-500", "collapsed_fieldsets": "", "groupby": [ "source", "target" ], "having": "", "link_length": "200", "metric": "sum__value", "row_limit": "5000", "slice_name": "Force", "viz_type": "directed_force", "where": "" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc) slc = Slice( slice_name='Heatmap', viz_type='heatmap', datasource_type='table', datasource_id=tbl.id, params=textwrap.dedent("""\ { "all_columns_x": "source", "all_columns_y": "target", "canvas_image_rendering": "pixelated", "collapsed_fieldsets": "", "having": "", "linear_color_scheme": "blue_white_yellow", "metric": "sum__value", "normalize_across": "heatmap", "slice_name": "Heatmap", "viz_type": "heatmap", "where": "", "xscale_interval": "1", "yscale_interval": "1" } """), ) misc_dash_slices.add(slc.slice_name) merge_slice(slc)
[ "def", "load_energy", "(", ")", ":", "tbl_name", "=", "'energy_usage'", "data", "=", "get_example_data", "(", "'energy.json.gz'", ")", "pdf", "=", "pd", ".", "read_json", "(", "data", ")", "pdf", ".", "to_sql", "(", "tbl_name", ",", "db", ".", "engine", ",", "if_exists", "=", "'replace'", ",", "chunksize", "=", "500", ",", "dtype", "=", "{", "'source'", ":", "String", "(", "255", ")", ",", "'target'", ":", "String", "(", "255", ")", ",", "'value'", ":", "Float", "(", ")", ",", "}", ",", "index", "=", "False", ")", "print", "(", "'Creating table [wb_health_population] reference'", ")", "tbl", "=", "db", ".", "session", ".", "query", "(", "TBL", ")", ".", "filter_by", "(", "table_name", "=", "tbl_name", ")", ".", "first", "(", ")", "if", "not", "tbl", ":", "tbl", "=", "TBL", "(", "table_name", "=", "tbl_name", ")", "tbl", ".", "description", "=", "'Energy consumption'", "tbl", ".", "database", "=", "utils", ".", "get_or_create_main_db", "(", ")", "if", "not", "any", "(", "col", ".", "metric_name", "==", "'sum__value'", "for", "col", "in", "tbl", ".", "metrics", ")", ":", "tbl", ".", "metrics", ".", "append", "(", "SqlMetric", "(", "metric_name", "=", "'sum__value'", ",", "expression", "=", "'SUM(value)'", ",", ")", ")", "db", ".", "session", ".", "merge", "(", "tbl", ")", "db", ".", "session", ".", "commit", "(", ")", "tbl", ".", "fetch_metadata", "(", ")", "slc", "=", "Slice", "(", "slice_name", "=", "'Energy Sankey'", ",", "viz_type", "=", "'sankey'", ",", "datasource_type", "=", "'table'", ",", "datasource_id", "=", "tbl", ".", "id", ",", "params", "=", "textwrap", ".", "dedent", "(", "\"\"\"\\\n {\n \"collapsed_fieldsets\": \"\",\n \"groupby\": [\n \"source\",\n \"target\"\n ],\n \"having\": \"\",\n \"metric\": \"sum__value\",\n \"row_limit\": \"5000\",\n \"slice_name\": \"Energy Sankey\",\n \"viz_type\": \"sankey\",\n \"where\": \"\"\n }\n \"\"\"", ")", ",", ")", "misc_dash_slices", ".", "add", "(", "slc", ".", "slice_name", ")", "merge_slice", "(", "slc", ")", "slc", "=", "Slice", "(", "slice_name", "=", "'Energy Force Layout'", ",", "viz_type", "=", "'directed_force'", ",", "datasource_type", "=", "'table'", ",", "datasource_id", "=", "tbl", ".", "id", ",", "params", "=", "textwrap", ".", "dedent", "(", "\"\"\"\\\n {\n \"charge\": \"-500\",\n \"collapsed_fieldsets\": \"\",\n \"groupby\": [\n \"source\",\n \"target\"\n ],\n \"having\": \"\",\n \"link_length\": \"200\",\n \"metric\": \"sum__value\",\n \"row_limit\": \"5000\",\n \"slice_name\": \"Force\",\n \"viz_type\": \"directed_force\",\n \"where\": \"\"\n }\n \"\"\"", ")", ",", ")", "misc_dash_slices", ".", "add", "(", "slc", ".", "slice_name", ")", "merge_slice", "(", "slc", ")", "slc", "=", "Slice", "(", "slice_name", "=", "'Heatmap'", ",", "viz_type", "=", "'heatmap'", ",", "datasource_type", "=", "'table'", ",", "datasource_id", "=", "tbl", ".", "id", ",", "params", "=", "textwrap", ".", "dedent", "(", "\"\"\"\\\n {\n \"all_columns_x\": \"source\",\n \"all_columns_y\": \"target\",\n \"canvas_image_rendering\": \"pixelated\",\n \"collapsed_fieldsets\": \"\",\n \"having\": \"\",\n \"linear_color_scheme\": \"blue_white_yellow\",\n \"metric\": \"sum__value\",\n \"normalize_across\": \"heatmap\",\n \"slice_name\": \"Heatmap\",\n \"viz_type\": \"heatmap\",\n \"where\": \"\",\n \"xscale_interval\": \"1\",\n \"yscale_interval\": \"1\"\n }\n \"\"\"", ")", ",", ")", "misc_dash_slices", ".", "add", "(", "slc", ".", "slice_name", ")", "merge_slice", "(", "slc", ")" ]
Loads an energy related dataset to use with sankey and graphs
[ "Loads", "an", "energy", "related", "dataset", "to", "use", "with", "sankey", "and", "graphs" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/data/energy.py#L32-L140
train
apache/incubator-superset
superset/data/random_time_series.py
load_random_time_series_data
def load_random_time_series_data(): """Loading random time series data from a zip file in the repo""" data = get_example_data('random_time_series.json.gz') pdf = pd.read_json(data) pdf.ds = pd.to_datetime(pdf.ds, unit='s') pdf.to_sql( 'random_time_series', db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': DateTime, }, index=False) print('Done loading table!') print('-' * 80) print('Creating table [random_time_series] reference') obj = db.session.query(TBL).filter_by(table_name='random_time_series').first() if not obj: obj = TBL(table_name='random_time_series') obj.main_dttm_col = 'ds' obj.database = utils.get_or_create_main_db() db.session.merge(obj) db.session.commit() obj.fetch_metadata() tbl = obj slice_data = { 'granularity_sqla': 'day', 'row_limit': config.get('ROW_LIMIT'), 'since': '1 year ago', 'until': 'now', 'metric': 'count', 'where': '', 'viz_type': 'cal_heatmap', 'domain_granularity': 'month', 'subdomain_granularity': 'day', } print('Creating a slice') slc = Slice( slice_name='Calendar Heatmap', viz_type='cal_heatmap', datasource_type='table', datasource_id=tbl.id, params=get_slice_json(slice_data), ) merge_slice(slc)
python
def load_random_time_series_data(): """Loading random time series data from a zip file in the repo""" data = get_example_data('random_time_series.json.gz') pdf = pd.read_json(data) pdf.ds = pd.to_datetime(pdf.ds, unit='s') pdf.to_sql( 'random_time_series', db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': DateTime, }, index=False) print('Done loading table!') print('-' * 80) print('Creating table [random_time_series] reference') obj = db.session.query(TBL).filter_by(table_name='random_time_series').first() if not obj: obj = TBL(table_name='random_time_series') obj.main_dttm_col = 'ds' obj.database = utils.get_or_create_main_db() db.session.merge(obj) db.session.commit() obj.fetch_metadata() tbl = obj slice_data = { 'granularity_sqla': 'day', 'row_limit': config.get('ROW_LIMIT'), 'since': '1 year ago', 'until': 'now', 'metric': 'count', 'where': '', 'viz_type': 'cal_heatmap', 'domain_granularity': 'month', 'subdomain_granularity': 'day', } print('Creating a slice') slc = Slice( slice_name='Calendar Heatmap', viz_type='cal_heatmap', datasource_type='table', datasource_id=tbl.id, params=get_slice_json(slice_data), ) merge_slice(slc)
[ "def", "load_random_time_series_data", "(", ")", ":", "data", "=", "get_example_data", "(", "'random_time_series.json.gz'", ")", "pdf", "=", "pd", ".", "read_json", "(", "data", ")", "pdf", ".", "ds", "=", "pd", ".", "to_datetime", "(", "pdf", ".", "ds", ",", "unit", "=", "'s'", ")", "pdf", ".", "to_sql", "(", "'random_time_series'", ",", "db", ".", "engine", ",", "if_exists", "=", "'replace'", ",", "chunksize", "=", "500", ",", "dtype", "=", "{", "'ds'", ":", "DateTime", ",", "}", ",", "index", "=", "False", ")", "print", "(", "'Done loading table!'", ")", "print", "(", "'-'", "*", "80", ")", "print", "(", "'Creating table [random_time_series] reference'", ")", "obj", "=", "db", ".", "session", ".", "query", "(", "TBL", ")", ".", "filter_by", "(", "table_name", "=", "'random_time_series'", ")", ".", "first", "(", ")", "if", "not", "obj", ":", "obj", "=", "TBL", "(", "table_name", "=", "'random_time_series'", ")", "obj", ".", "main_dttm_col", "=", "'ds'", "obj", ".", "database", "=", "utils", ".", "get_or_create_main_db", "(", ")", "db", ".", "session", ".", "merge", "(", "obj", ")", "db", ".", "session", ".", "commit", "(", ")", "obj", ".", "fetch_metadata", "(", ")", "tbl", "=", "obj", "slice_data", "=", "{", "'granularity_sqla'", ":", "'day'", ",", "'row_limit'", ":", "config", ".", "get", "(", "'ROW_LIMIT'", ")", ",", "'since'", ":", "'1 year ago'", ",", "'until'", ":", "'now'", ",", "'metric'", ":", "'count'", ",", "'where'", ":", "''", ",", "'viz_type'", ":", "'cal_heatmap'", ",", "'domain_granularity'", ":", "'month'", ",", "'subdomain_granularity'", ":", "'day'", ",", "}", "print", "(", "'Creating a slice'", ")", "slc", "=", "Slice", "(", "slice_name", "=", "'Calendar Heatmap'", ",", "viz_type", "=", "'cal_heatmap'", ",", "datasource_type", "=", "'table'", ",", "datasource_id", "=", "tbl", ".", "id", ",", "params", "=", "get_slice_json", "(", "slice_data", ")", ",", ")", "merge_slice", "(", "slc", ")" ]
Loading random time series data from a zip file in the repo
[ "Loading", "random", "time", "series", "data", "from", "a", "zip", "file", "in", "the", "repo" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/data/random_time_series.py#L33-L81
train
apache/incubator-superset
superset/cli.py
runserver
def runserver(debug, console_log, use_reloader, address, port, timeout, workers, socket): """Starts a Superset web server.""" debug = debug or config.get('DEBUG') or console_log if debug: print(Fore.BLUE + '-=' * 20) print( Fore.YELLOW + 'Starting Superset server in ' + Fore.RED + 'DEBUG' + Fore.YELLOW + ' mode') print(Fore.BLUE + '-=' * 20) print(Style.RESET_ALL) if console_log: console_log_run(app, port, use_reloader) else: debug_run(app, port, use_reloader) else: logging.info( "The Gunicorn 'superset runserver' command is deprecated. Please " "use the 'gunicorn' command instead.") addr_str = f' unix:{socket} ' if socket else f' {address}:{port} ' cmd = ( 'gunicorn ' f'-w {workers} ' f'--timeout {timeout} ' f'-b {addr_str} ' '--limit-request-line 0 ' '--limit-request-field_size 0 ' 'superset:app' ) print(Fore.GREEN + 'Starting server with command: ') print(Fore.YELLOW + cmd) print(Style.RESET_ALL) Popen(cmd, shell=True).wait()
python
def runserver(debug, console_log, use_reloader, address, port, timeout, workers, socket): """Starts a Superset web server.""" debug = debug or config.get('DEBUG') or console_log if debug: print(Fore.BLUE + '-=' * 20) print( Fore.YELLOW + 'Starting Superset server in ' + Fore.RED + 'DEBUG' + Fore.YELLOW + ' mode') print(Fore.BLUE + '-=' * 20) print(Style.RESET_ALL) if console_log: console_log_run(app, port, use_reloader) else: debug_run(app, port, use_reloader) else: logging.info( "The Gunicorn 'superset runserver' command is deprecated. Please " "use the 'gunicorn' command instead.") addr_str = f' unix:{socket} ' if socket else f' {address}:{port} ' cmd = ( 'gunicorn ' f'-w {workers} ' f'--timeout {timeout} ' f'-b {addr_str} ' '--limit-request-line 0 ' '--limit-request-field_size 0 ' 'superset:app' ) print(Fore.GREEN + 'Starting server with command: ') print(Fore.YELLOW + cmd) print(Style.RESET_ALL) Popen(cmd, shell=True).wait()
[ "def", "runserver", "(", "debug", ",", "console_log", ",", "use_reloader", ",", "address", ",", "port", ",", "timeout", ",", "workers", ",", "socket", ")", ":", "debug", "=", "debug", "or", "config", ".", "get", "(", "'DEBUG'", ")", "or", "console_log", "if", "debug", ":", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "20", ")", "print", "(", "Fore", ".", "YELLOW", "+", "'Starting Superset server in '", "+", "Fore", ".", "RED", "+", "'DEBUG'", "+", "Fore", ".", "YELLOW", "+", "' mode'", ")", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "20", ")", "print", "(", "Style", ".", "RESET_ALL", ")", "if", "console_log", ":", "console_log_run", "(", "app", ",", "port", ",", "use_reloader", ")", "else", ":", "debug_run", "(", "app", ",", "port", ",", "use_reloader", ")", "else", ":", "logging", ".", "info", "(", "\"The Gunicorn 'superset runserver' command is deprecated. Please \"", "\"use the 'gunicorn' command instead.\"", ")", "addr_str", "=", "f' unix:{socket} '", "if", "socket", "else", "f' {address}:{port} '", "cmd", "=", "(", "'gunicorn '", "f'-w {workers} '", "f'--timeout {timeout} '", "f'-b {addr_str} '", "'--limit-request-line 0 '", "'--limit-request-field_size 0 '", "'superset:app'", ")", "print", "(", "Fore", ".", "GREEN", "+", "'Starting server with command: '", ")", "print", "(", "Fore", ".", "YELLOW", "+", "cmd", ")", "print", "(", "Style", ".", "RESET_ALL", ")", "Popen", "(", "cmd", ",", "shell", "=", "True", ")", ".", "wait", "(", ")" ]
Starts a Superset web server.
[ "Starts", "a", "Superset", "web", "server", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L112-L144
train
apache/incubator-superset
superset/cli.py
version
def version(verbose): """Prints the current version number""" print(Fore.BLUE + '-=' * 15) print(Fore.YELLOW + 'Superset ' + Fore.CYAN + '{version}'.format( version=config.get('VERSION_STRING'))) print(Fore.BLUE + '-=' * 15) if verbose: print('[DB] : ' + '{}'.format(db.engine)) print(Style.RESET_ALL)
python
def version(verbose): """Prints the current version number""" print(Fore.BLUE + '-=' * 15) print(Fore.YELLOW + 'Superset ' + Fore.CYAN + '{version}'.format( version=config.get('VERSION_STRING'))) print(Fore.BLUE + '-=' * 15) if verbose: print('[DB] : ' + '{}'.format(db.engine)) print(Style.RESET_ALL)
[ "def", "version", "(", "verbose", ")", ":", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "15", ")", "print", "(", "Fore", ".", "YELLOW", "+", "'Superset '", "+", "Fore", ".", "CYAN", "+", "'{version}'", ".", "format", "(", "version", "=", "config", ".", "get", "(", "'VERSION_STRING'", ")", ")", ")", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "15", ")", "if", "verbose", ":", "print", "(", "'[DB] : '", "+", "'{}'", ".", "format", "(", "db", ".", "engine", ")", ")", "print", "(", "Style", ".", "RESET_ALL", ")" ]
Prints the current version number
[ "Prints", "the", "current", "version", "number" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L149-L157
train
apache/incubator-superset
superset/cli.py
refresh_druid
def refresh_druid(datasource, merge): """Refresh druid datasources""" session = db.session() from superset.connectors.druid.models import DruidCluster for cluster in session.query(DruidCluster).all(): try: cluster.refresh_datasources(datasource_name=datasource, merge_flag=merge) except Exception as e: print( "Error while processing cluster '{}'\n{}".format( cluster, str(e))) logging.exception(e) cluster.metadata_last_refreshed = datetime.now() print( 'Refreshed metadata from cluster ' '[' + cluster.cluster_name + ']') session.commit()
python
def refresh_druid(datasource, merge): """Refresh druid datasources""" session = db.session() from superset.connectors.druid.models import DruidCluster for cluster in session.query(DruidCluster).all(): try: cluster.refresh_datasources(datasource_name=datasource, merge_flag=merge) except Exception as e: print( "Error while processing cluster '{}'\n{}".format( cluster, str(e))) logging.exception(e) cluster.metadata_last_refreshed = datetime.now() print( 'Refreshed metadata from cluster ' '[' + cluster.cluster_name + ']') session.commit()
[ "def", "refresh_druid", "(", "datasource", ",", "merge", ")", ":", "session", "=", "db", ".", "session", "(", ")", "from", "superset", ".", "connectors", ".", "druid", ".", "models", "import", "DruidCluster", "for", "cluster", "in", "session", ".", "query", "(", "DruidCluster", ")", ".", "all", "(", ")", ":", "try", ":", "cluster", ".", "refresh_datasources", "(", "datasource_name", "=", "datasource", ",", "merge_flag", "=", "merge", ")", "except", "Exception", "as", "e", ":", "print", "(", "\"Error while processing cluster '{}'\\n{}\"", ".", "format", "(", "cluster", ",", "str", "(", "e", ")", ")", ")", "logging", ".", "exception", "(", "e", ")", "cluster", ".", "metadata_last_refreshed", "=", "datetime", ".", "now", "(", ")", "print", "(", "'Refreshed metadata from cluster '", "'['", "+", "cluster", ".", "cluster_name", "+", "']'", ")", "session", ".", "commit", "(", ")" ]
Refresh druid datasources
[ "Refresh", "druid", "datasources" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L225-L242
train
apache/incubator-superset
superset/cli.py
import_dashboards
def import_dashboards(path, recursive): """Import dashboards from JSON""" p = Path(path) files = [] if p.is_file(): files.append(p) elif p.exists() and not recursive: files.extend(p.glob('*.json')) elif p.exists() and recursive: files.extend(p.rglob('*.json')) for f in files: logging.info('Importing dashboard from file %s', f) try: with f.open() as data_stream: dashboard_import_export.import_dashboards( db.session, data_stream) except Exception as e: logging.error('Error when importing dashboard from file %s', f) logging.error(e)
python
def import_dashboards(path, recursive): """Import dashboards from JSON""" p = Path(path) files = [] if p.is_file(): files.append(p) elif p.exists() and not recursive: files.extend(p.glob('*.json')) elif p.exists() and recursive: files.extend(p.rglob('*.json')) for f in files: logging.info('Importing dashboard from file %s', f) try: with f.open() as data_stream: dashboard_import_export.import_dashboards( db.session, data_stream) except Exception as e: logging.error('Error when importing dashboard from file %s', f) logging.error(e)
[ "def", "import_dashboards", "(", "path", ",", "recursive", ")", ":", "p", "=", "Path", "(", "path", ")", "files", "=", "[", "]", "if", "p", ".", "is_file", "(", ")", ":", "files", ".", "append", "(", "p", ")", "elif", "p", ".", "exists", "(", ")", "and", "not", "recursive", ":", "files", ".", "extend", "(", "p", ".", "glob", "(", "'*.json'", ")", ")", "elif", "p", ".", "exists", "(", ")", "and", "recursive", ":", "files", ".", "extend", "(", "p", ".", "rglob", "(", "'*.json'", ")", ")", "for", "f", "in", "files", ":", "logging", ".", "info", "(", "'Importing dashboard from file %s'", ",", "f", ")", "try", ":", "with", "f", ".", "open", "(", ")", "as", "data_stream", ":", "dashboard_import_export", ".", "import_dashboards", "(", "db", ".", "session", ",", "data_stream", ")", "except", "Exception", "as", "e", ":", "logging", ".", "error", "(", "'Error when importing dashboard from file %s'", ",", "f", ")", "logging", ".", "error", "(", "e", ")" ]
Import dashboards from JSON
[ "Import", "dashboards", "from", "JSON" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L253-L271
train
apache/incubator-superset
superset/cli.py
export_dashboards
def export_dashboards(print_stdout, dashboard_file): """Export dashboards to JSON""" data = dashboard_import_export.export_dashboards(db.session) if print_stdout or not dashboard_file: print(data) if dashboard_file: logging.info('Exporting dashboards to %s', dashboard_file) with open(dashboard_file, 'w') as data_stream: data_stream.write(data)
python
def export_dashboards(print_stdout, dashboard_file): """Export dashboards to JSON""" data = dashboard_import_export.export_dashboards(db.session) if print_stdout or not dashboard_file: print(data) if dashboard_file: logging.info('Exporting dashboards to %s', dashboard_file) with open(dashboard_file, 'w') as data_stream: data_stream.write(data)
[ "def", "export_dashboards", "(", "print_stdout", ",", "dashboard_file", ")", ":", "data", "=", "dashboard_import_export", ".", "export_dashboards", "(", "db", ".", "session", ")", "if", "print_stdout", "or", "not", "dashboard_file", ":", "print", "(", "data", ")", "if", "dashboard_file", ":", "logging", ".", "info", "(", "'Exporting dashboards to %s'", ",", "dashboard_file", ")", "with", "open", "(", "dashboard_file", ",", "'w'", ")", "as", "data_stream", ":", "data_stream", ".", "write", "(", "data", ")" ]
Export dashboards to JSON
[ "Export", "dashboards", "to", "JSON" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L281-L289
train
apache/incubator-superset
superset/cli.py
import_datasources
def import_datasources(path, sync, recursive): """Import datasources from YAML""" sync_array = sync.split(',') p = Path(path) files = [] if p.is_file(): files.append(p) elif p.exists() and not recursive: files.extend(p.glob('*.yaml')) files.extend(p.glob('*.yml')) elif p.exists() and recursive: files.extend(p.rglob('*.yaml')) files.extend(p.rglob('*.yml')) for f in files: logging.info('Importing datasources from file %s', f) try: with f.open() as data_stream: dict_import_export.import_from_dict( db.session, yaml.safe_load(data_stream), sync=sync_array) except Exception as e: logging.error('Error when importing datasources from file %s', f) logging.error(e)
python
def import_datasources(path, sync, recursive): """Import datasources from YAML""" sync_array = sync.split(',') p = Path(path) files = [] if p.is_file(): files.append(p) elif p.exists() and not recursive: files.extend(p.glob('*.yaml')) files.extend(p.glob('*.yml')) elif p.exists() and recursive: files.extend(p.rglob('*.yaml')) files.extend(p.rglob('*.yml')) for f in files: logging.info('Importing datasources from file %s', f) try: with f.open() as data_stream: dict_import_export.import_from_dict( db.session, yaml.safe_load(data_stream), sync=sync_array) except Exception as e: logging.error('Error when importing datasources from file %s', f) logging.error(e)
[ "def", "import_datasources", "(", "path", ",", "sync", ",", "recursive", ")", ":", "sync_array", "=", "sync", ".", "split", "(", "','", ")", "p", "=", "Path", "(", "path", ")", "files", "=", "[", "]", "if", "p", ".", "is_file", "(", ")", ":", "files", ".", "append", "(", "p", ")", "elif", "p", ".", "exists", "(", ")", "and", "not", "recursive", ":", "files", ".", "extend", "(", "p", ".", "glob", "(", "'*.yaml'", ")", ")", "files", ".", "extend", "(", "p", ".", "glob", "(", "'*.yml'", ")", ")", "elif", "p", ".", "exists", "(", ")", "and", "recursive", ":", "files", ".", "extend", "(", "p", ".", "rglob", "(", "'*.yaml'", ")", ")", "files", ".", "extend", "(", "p", ".", "rglob", "(", "'*.yml'", ")", ")", "for", "f", "in", "files", ":", "logging", ".", "info", "(", "'Importing datasources from file %s'", ",", "f", ")", "try", ":", "with", "f", ".", "open", "(", ")", "as", "data_stream", ":", "dict_import_export", ".", "import_from_dict", "(", "db", ".", "session", ",", "yaml", ".", "safe_load", "(", "data_stream", ")", ",", "sync", "=", "sync_array", ")", "except", "Exception", "as", "e", ":", "logging", ".", "error", "(", "'Error when importing datasources from file %s'", ",", "f", ")", "logging", ".", "error", "(", "e", ")" ]
Import datasources from YAML
[ "Import", "datasources", "from", "YAML" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L305-L328
train
apache/incubator-superset
superset/cli.py
export_datasources
def export_datasources(print_stdout, datasource_file, back_references, include_defaults): """Export datasources to YAML""" data = dict_import_export.export_to_dict( session=db.session, recursive=True, back_references=back_references, include_defaults=include_defaults) if print_stdout or not datasource_file: yaml.safe_dump(data, stdout, default_flow_style=False) if datasource_file: logging.info('Exporting datasources to %s', datasource_file) with open(datasource_file, 'w') as data_stream: yaml.safe_dump(data, data_stream, default_flow_style=False)
python
def export_datasources(print_stdout, datasource_file, back_references, include_defaults): """Export datasources to YAML""" data = dict_import_export.export_to_dict( session=db.session, recursive=True, back_references=back_references, include_defaults=include_defaults) if print_stdout or not datasource_file: yaml.safe_dump(data, stdout, default_flow_style=False) if datasource_file: logging.info('Exporting datasources to %s', datasource_file) with open(datasource_file, 'w') as data_stream: yaml.safe_dump(data, data_stream, default_flow_style=False)
[ "def", "export_datasources", "(", "print_stdout", ",", "datasource_file", ",", "back_references", ",", "include_defaults", ")", ":", "data", "=", "dict_import_export", ".", "export_to_dict", "(", "session", "=", "db", ".", "session", ",", "recursive", "=", "True", ",", "back_references", "=", "back_references", ",", "include_defaults", "=", "include_defaults", ")", "if", "print_stdout", "or", "not", "datasource_file", ":", "yaml", ".", "safe_dump", "(", "data", ",", "stdout", ",", "default_flow_style", "=", "False", ")", "if", "datasource_file", ":", "logging", ".", "info", "(", "'Exporting datasources to %s'", ",", "datasource_file", ")", "with", "open", "(", "datasource_file", ",", "'w'", ")", "as", "data_stream", ":", "yaml", ".", "safe_dump", "(", "data", ",", "data_stream", ",", "default_flow_style", "=", "False", ")" ]
Export datasources to YAML
[ "Export", "datasources", "to", "YAML" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L344-L357
train
apache/incubator-superset
superset/cli.py
export_datasource_schema
def export_datasource_schema(back_references): """Export datasource YAML schema to stdout""" data = dict_import_export.export_schema_to_dict( back_references=back_references) yaml.safe_dump(data, stdout, default_flow_style=False)
python
def export_datasource_schema(back_references): """Export datasource YAML schema to stdout""" data = dict_import_export.export_schema_to_dict( back_references=back_references) yaml.safe_dump(data, stdout, default_flow_style=False)
[ "def", "export_datasource_schema", "(", "back_references", ")", ":", "data", "=", "dict_import_export", ".", "export_schema_to_dict", "(", "back_references", "=", "back_references", ")", "yaml", ".", "safe_dump", "(", "data", ",", "stdout", ",", "default_flow_style", "=", "False", ")" ]
Export datasource YAML schema to stdout
[ "Export", "datasource", "YAML", "schema", "to", "stdout" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L364-L368
train
apache/incubator-superset
superset/cli.py
update_datasources_cache
def update_datasources_cache(): """Refresh sqllab datasources cache""" from superset.models.core import Database for database in db.session.query(Database).all(): if database.allow_multi_schema_metadata_fetch: print('Fetching {} datasources ...'.format(database.name)) try: database.all_table_names_in_database( force=True, cache=True, cache_timeout=24 * 60 * 60) database.all_view_names_in_database( force=True, cache=True, cache_timeout=24 * 60 * 60) except Exception as e: print('{}'.format(str(e)))
python
def update_datasources_cache(): """Refresh sqllab datasources cache""" from superset.models.core import Database for database in db.session.query(Database).all(): if database.allow_multi_schema_metadata_fetch: print('Fetching {} datasources ...'.format(database.name)) try: database.all_table_names_in_database( force=True, cache=True, cache_timeout=24 * 60 * 60) database.all_view_names_in_database( force=True, cache=True, cache_timeout=24 * 60 * 60) except Exception as e: print('{}'.format(str(e)))
[ "def", "update_datasources_cache", "(", ")", ":", "from", "superset", ".", "models", ".", "core", "import", "Database", "for", "database", "in", "db", ".", "session", ".", "query", "(", "Database", ")", ".", "all", "(", ")", ":", "if", "database", ".", "allow_multi_schema_metadata_fetch", ":", "print", "(", "'Fetching {} datasources ...'", ".", "format", "(", "database", ".", "name", ")", ")", "try", ":", "database", ".", "all_table_names_in_database", "(", "force", "=", "True", ",", "cache", "=", "True", ",", "cache_timeout", "=", "24", "*", "60", "*", "60", ")", "database", ".", "all_view_names_in_database", "(", "force", "=", "True", ",", "cache", "=", "True", ",", "cache_timeout", "=", "24", "*", "60", "*", "60", ")", "except", "Exception", "as", "e", ":", "print", "(", "'{}'", ".", "format", "(", "str", "(", "e", ")", ")", ")" ]
Refresh sqllab datasources cache
[ "Refresh", "sqllab", "datasources", "cache" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L372-L384
train
apache/incubator-superset
superset/cli.py
worker
def worker(workers): """Starts a Superset worker for async SQL query execution.""" logging.info( "The 'superset worker' command is deprecated. Please use the 'celery " "worker' command instead.") if workers: celery_app.conf.update(CELERYD_CONCURRENCY=workers) elif config.get('SUPERSET_CELERY_WORKERS'): celery_app.conf.update( CELERYD_CONCURRENCY=config.get('SUPERSET_CELERY_WORKERS')) worker = celery_app.Worker(optimization='fair') worker.start()
python
def worker(workers): """Starts a Superset worker for async SQL query execution.""" logging.info( "The 'superset worker' command is deprecated. Please use the 'celery " "worker' command instead.") if workers: celery_app.conf.update(CELERYD_CONCURRENCY=workers) elif config.get('SUPERSET_CELERY_WORKERS'): celery_app.conf.update( CELERYD_CONCURRENCY=config.get('SUPERSET_CELERY_WORKERS')) worker = celery_app.Worker(optimization='fair') worker.start()
[ "def", "worker", "(", "workers", ")", ":", "logging", ".", "info", "(", "\"The 'superset worker' command is deprecated. Please use the 'celery \"", "\"worker' command instead.\"", ")", "if", "workers", ":", "celery_app", ".", "conf", ".", "update", "(", "CELERYD_CONCURRENCY", "=", "workers", ")", "elif", "config", ".", "get", "(", "'SUPERSET_CELERY_WORKERS'", ")", ":", "celery_app", ".", "conf", ".", "update", "(", "CELERYD_CONCURRENCY", "=", "config", ".", "get", "(", "'SUPERSET_CELERY_WORKERS'", ")", ")", "worker", "=", "celery_app", ".", "Worker", "(", "optimization", "=", "'fair'", ")", "worker", ".", "start", "(", ")" ]
Starts a Superset worker for async SQL query execution.
[ "Starts", "a", "Superset", "worker", "for", "async", "SQL", "query", "execution", "." ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L392-L404
train
apache/incubator-superset
superset/cli.py
flower
def flower(port, address): """Runs a Celery Flower web server Celery Flower is a UI to monitor the Celery operation on a given broker""" BROKER_URL = celery_app.conf.BROKER_URL cmd = ( 'celery flower ' f'--broker={BROKER_URL} ' f'--port={port} ' f'--address={address} ' ) logging.info( "The 'superset flower' command is deprecated. Please use the 'celery " "flower' command instead.") print(Fore.GREEN + 'Starting a Celery Flower instance') print(Fore.BLUE + '-=' * 40) print(Fore.YELLOW + cmd) print(Fore.BLUE + '-=' * 40) Popen(cmd, shell=True).wait()
python
def flower(port, address): """Runs a Celery Flower web server Celery Flower is a UI to monitor the Celery operation on a given broker""" BROKER_URL = celery_app.conf.BROKER_URL cmd = ( 'celery flower ' f'--broker={BROKER_URL} ' f'--port={port} ' f'--address={address} ' ) logging.info( "The 'superset flower' command is deprecated. Please use the 'celery " "flower' command instead.") print(Fore.GREEN + 'Starting a Celery Flower instance') print(Fore.BLUE + '-=' * 40) print(Fore.YELLOW + cmd) print(Fore.BLUE + '-=' * 40) Popen(cmd, shell=True).wait()
[ "def", "flower", "(", "port", ",", "address", ")", ":", "BROKER_URL", "=", "celery_app", ".", "conf", ".", "BROKER_URL", "cmd", "=", "(", "'celery flower '", "f'--broker={BROKER_URL} '", "f'--port={port} '", "f'--address={address} '", ")", "logging", ".", "info", "(", "\"The 'superset flower' command is deprecated. Please use the 'celery \"", "\"flower' command instead.\"", ")", "print", "(", "Fore", ".", "GREEN", "+", "'Starting a Celery Flower instance'", ")", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "40", ")", "print", "(", "Fore", ".", "YELLOW", "+", "cmd", ")", "print", "(", "Fore", ".", "BLUE", "+", "'-='", "*", "40", ")", "Popen", "(", "cmd", ",", "shell", "=", "True", ")", ".", "wait", "(", ")" ]
Runs a Celery Flower web server Celery Flower is a UI to monitor the Celery operation on a given broker
[ "Runs", "a", "Celery", "Flower", "web", "server" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/cli.py#L416-L435
train
apache/incubator-superset
superset/data/flights.py
load_flights
def load_flights(): """Loading random time series data from a zip file in the repo""" tbl_name = 'flights' data = get_example_data('flight_data.csv.gz', make_bytes=True) pdf = pd.read_csv(data, encoding='latin-1') # Loading airports info to join and get lat/long airports_bytes = get_example_data('airports.csv.gz', make_bytes=True) airports = pd.read_csv(airports_bytes, encoding='latin-1') airports = airports.set_index('IATA_CODE') pdf['ds'] = pdf.YEAR.map(str) + '-0' + pdf.MONTH.map(str) + '-0' + pdf.DAY.map(str) pdf.ds = pd.to_datetime(pdf.ds) del pdf['YEAR'] del pdf['MONTH'] del pdf['DAY'] pdf = pdf.join(airports, on='ORIGIN_AIRPORT', rsuffix='_ORIG') pdf = pdf.join(airports, on='DESTINATION_AIRPORT', rsuffix='_DEST') pdf.to_sql( tbl_name, db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': DateTime, }, index=False) tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first() if not tbl: tbl = TBL(table_name=tbl_name) tbl.description = 'Random set of flights in the US' tbl.database = utils.get_or_create_main_db() db.session.merge(tbl) db.session.commit() tbl.fetch_metadata() print('Done loading table!')
python
def load_flights(): """Loading random time series data from a zip file in the repo""" tbl_name = 'flights' data = get_example_data('flight_data.csv.gz', make_bytes=True) pdf = pd.read_csv(data, encoding='latin-1') # Loading airports info to join and get lat/long airports_bytes = get_example_data('airports.csv.gz', make_bytes=True) airports = pd.read_csv(airports_bytes, encoding='latin-1') airports = airports.set_index('IATA_CODE') pdf['ds'] = pdf.YEAR.map(str) + '-0' + pdf.MONTH.map(str) + '-0' + pdf.DAY.map(str) pdf.ds = pd.to_datetime(pdf.ds) del pdf['YEAR'] del pdf['MONTH'] del pdf['DAY'] pdf = pdf.join(airports, on='ORIGIN_AIRPORT', rsuffix='_ORIG') pdf = pdf.join(airports, on='DESTINATION_AIRPORT', rsuffix='_DEST') pdf.to_sql( tbl_name, db.engine, if_exists='replace', chunksize=500, dtype={ 'ds': DateTime, }, index=False) tbl = db.session.query(TBL).filter_by(table_name=tbl_name).first() if not tbl: tbl = TBL(table_name=tbl_name) tbl.description = 'Random set of flights in the US' tbl.database = utils.get_or_create_main_db() db.session.merge(tbl) db.session.commit() tbl.fetch_metadata() print('Done loading table!')
[ "def", "load_flights", "(", ")", ":", "tbl_name", "=", "'flights'", "data", "=", "get_example_data", "(", "'flight_data.csv.gz'", ",", "make_bytes", "=", "True", ")", "pdf", "=", "pd", ".", "read_csv", "(", "data", ",", "encoding", "=", "'latin-1'", ")", "# Loading airports info to join and get lat/long", "airports_bytes", "=", "get_example_data", "(", "'airports.csv.gz'", ",", "make_bytes", "=", "True", ")", "airports", "=", "pd", ".", "read_csv", "(", "airports_bytes", ",", "encoding", "=", "'latin-1'", ")", "airports", "=", "airports", ".", "set_index", "(", "'IATA_CODE'", ")", "pdf", "[", "'ds'", "]", "=", "pdf", ".", "YEAR", ".", "map", "(", "str", ")", "+", "'-0'", "+", "pdf", ".", "MONTH", ".", "map", "(", "str", ")", "+", "'-0'", "+", "pdf", ".", "DAY", ".", "map", "(", "str", ")", "pdf", ".", "ds", "=", "pd", ".", "to_datetime", "(", "pdf", ".", "ds", ")", "del", "pdf", "[", "'YEAR'", "]", "del", "pdf", "[", "'MONTH'", "]", "del", "pdf", "[", "'DAY'", "]", "pdf", "=", "pdf", ".", "join", "(", "airports", ",", "on", "=", "'ORIGIN_AIRPORT'", ",", "rsuffix", "=", "'_ORIG'", ")", "pdf", "=", "pdf", ".", "join", "(", "airports", ",", "on", "=", "'DESTINATION_AIRPORT'", ",", "rsuffix", "=", "'_DEST'", ")", "pdf", ".", "to_sql", "(", "tbl_name", ",", "db", ".", "engine", ",", "if_exists", "=", "'replace'", ",", "chunksize", "=", "500", ",", "dtype", "=", "{", "'ds'", ":", "DateTime", ",", "}", ",", "index", "=", "False", ")", "tbl", "=", "db", ".", "session", ".", "query", "(", "TBL", ")", ".", "filter_by", "(", "table_name", "=", "tbl_name", ")", ".", "first", "(", ")", "if", "not", "tbl", ":", "tbl", "=", "TBL", "(", "table_name", "=", "tbl_name", ")", "tbl", ".", "description", "=", "'Random set of flights in the US'", "tbl", ".", "database", "=", "utils", ".", "get_or_create_main_db", "(", ")", "db", ".", "session", ".", "merge", "(", "tbl", ")", "db", ".", "session", ".", "commit", "(", ")", "tbl", ".", "fetch_metadata", "(", ")", "print", "(", "'Done loading table!'", ")" ]
Loading random time series data from a zip file in the repo
[ "Loading", "random", "time", "series", "data", "from", "a", "zip", "file", "in", "the", "repo" ]
ca2996c78f679260eb79c6008e276733df5fb653
https://github.com/apache/incubator-superset/blob/ca2996c78f679260eb79c6008e276733df5fb653/superset/data/flights.py#L25-L61
train