Skip to content

Commit

Permalink
More logging to csv endpoint (#3164)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch authored Jul 19, 2017
1 parent c34df3e commit d01e67a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ def sql_json(self):
@log_this
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)
Expand All @@ -2074,21 +2075,28 @@ def csv(self, client_id):
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, encoding='utf-8')
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, encoding='utf-8')
response = Response(csv, mimetype='text/csv')
response.headers['Content-Disposition'] = (
'attachment; filename={}.csv'.format(query.name))
logging.info("Ready to return response")
return response

@has_access
Expand Down

0 comments on commit d01e67a

Please sign in to comment.