mirror of
https://github.com/discourse/discourse-data-explorer.git
synced 2025-03-06 09:29:12 +00:00
Add 'download results as CSV'
This commit is contained in:
parent
d1a58e83a2
commit
1601f4f269
@ -100,8 +100,7 @@ const QueryResultComponent = Ember.Component.extend({
|
|||||||
});
|
});
|
||||||
}.property('content', 'columns.@each'),
|
}.property('content', 'columns.@each'),
|
||||||
|
|
||||||
actions: {
|
downloadResult(format) {
|
||||||
downloadResult() {
|
|
||||||
// Create a frame to submit the form in (?)
|
// Create a frame to submit the form in (?)
|
||||||
// to avoid leaving an about:blank behind
|
// to avoid leaving an about:blank behind
|
||||||
let windowName = randomIdShort();
|
let windowName = randomIdShort();
|
||||||
@ -112,7 +111,7 @@ const QueryResultComponent = Ember.Component.extend({
|
|||||||
let form = document.createElement("form");
|
let form = document.createElement("form");
|
||||||
form.setAttribute('id', 'query-download-result');
|
form.setAttribute('id', 'query-download-result');
|
||||||
form.setAttribute('method', 'post');
|
form.setAttribute('method', 'post');
|
||||||
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.json?download=1'));
|
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.' + format + '?download=1'));
|
||||||
form.setAttribute('target', windowName);
|
form.setAttribute('target', windowName);
|
||||||
form.setAttribute('style', 'display:none;');
|
form.setAttribute('style', 'display:none;');
|
||||||
|
|
||||||
@ -137,6 +136,14 @@ const QueryResultComponent = Ember.Component.extend({
|
|||||||
document.body.removeChild(form);
|
document.body.removeChild(form);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
actions: {
|
||||||
|
downloadResultJson() {
|
||||||
|
this.downloadResult('json');
|
||||||
|
},
|
||||||
|
downloadResultCsv() {
|
||||||
|
this.downloadResult('csv');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
<div class="result-info">
|
<div class="result-info">
|
||||||
{{d-button action="downloadResult" icon="download" label="explorer.download_json"}}
|
{{i18n "explorer.download"}}
|
||||||
|
{{d-button action="downloadResultJson" icon="download" label="explorer.download_json"}}
|
||||||
|
{{d-button action="downloadResultCsv" icon="download" label="explorer.download_csv"}}
|
||||||
<div class="result-about">
|
<div class="result-about">
|
||||||
{{duration}}
|
{{duration}}
|
||||||
</div>
|
</div>
|
||||||
|
@ -46,13 +46,15 @@ en:
|
|||||||
edit: "Edit"
|
edit: "Edit"
|
||||||
delete: "Delete"
|
delete: "Delete"
|
||||||
recover: "Undelete Query"
|
recover: "Undelete Query"
|
||||||
download_json: "Download Results"
|
download: "Download Results"
|
||||||
|
download_json: "JSON"
|
||||||
|
download_csv: "CSV"
|
||||||
others_dirty: "A query has unsaved changes that will be lost if you navigate away."
|
others_dirty: "A query has unsaved changes that will be lost if you navigate away."
|
||||||
run_time: "Query completed in {{value}} ms."
|
run_time: "Query completed in {{value}} ms."
|
||||||
column: "Column {{number}}"
|
column: "Column {{number}}"
|
||||||
explain_label: "Include query plan?"
|
explain_label: "Include query plan?"
|
||||||
save_params: "Set Defaults"
|
save_params: "Set Defaults"
|
||||||
reset_params: "Reset"
|
reset_params: "Reset"
|
||||||
https_warning: "Use of the Data Explorer on sites not protected by HTTPS is discouraged. Please be careful to not retrieve sensitive information over insecure links."
|
https_warning: "This site is not protected by HTTPS. Please be careful to not retrieve sensitive information over insecure links."
|
||||||
no_queries: "There are no queries. Why not "
|
no_queries: "There are no queries. Why not "
|
||||||
no_queries_hook: "create one?"
|
no_queries_hook: "create one?"
|
||||||
|
24
plugin.rb
24
plugin.rb
@ -917,8 +917,6 @@ SQL
|
|||||||
check_xhr unless params[:download]
|
check_xhr unless params[:download]
|
||||||
query = DataExplorer::Query.find(params[:id].to_i)
|
query = DataExplorer::Query.find(params[:id].to_i)
|
||||||
if params[:download]
|
if params[:download]
|
||||||
response.headers['Content-Disposition'] =
|
|
||||||
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
|
|
||||||
response.sending_file = true
|
response.sending_file = true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -952,6 +950,12 @@ SQL
|
|||||||
else
|
else
|
||||||
pg_result = result[:pg_result]
|
pg_result = result[:pg_result]
|
||||||
cols = pg_result.fields
|
cols = pg_result.fields
|
||||||
|
respond_to do |format|
|
||||||
|
format.json do
|
||||||
|
if params[:download]
|
||||||
|
response.headers['Content-Disposition'] =
|
||||||
|
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
|
||||||
|
end
|
||||||
json = {
|
json = {
|
||||||
success: true,
|
success: true,
|
||||||
errors: [],
|
errors: [],
|
||||||
@ -970,6 +974,22 @@ SQL
|
|||||||
|
|
||||||
render json: json
|
render json: json
|
||||||
end
|
end
|
||||||
|
format.csv do
|
||||||
|
response.headers['Content-Disposition'] =
|
||||||
|
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.csv"
|
||||||
|
|
||||||
|
require 'csv'
|
||||||
|
text = CSV.generate do |csv|
|
||||||
|
csv << cols
|
||||||
|
pg_result.values.each do |row|
|
||||||
|
csv << row
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
render text: text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user