Skip to content

Commit

Permalink
Merge pull request #336 from xronos-ch/csv_download
Browse files Browse the repository at this point in the history
Csv and JSON download
  • Loading branch information
joeroe authored Jul 26, 2024
2 parents 1fcb224 + 4e7bed9 commit 46a9070
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
14 changes: 12 additions & 2 deletions app/controllers/c14s_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def index
unless c14_params.blank?
@c14s = @c14s.where(c14_params)
end


# if params[:sample_attributes][:context_attributes][:site_id].present?
# @c14s = @c14s.joins(sample: { context: :site }).where(sample:{context:{sites:{id: params[:sample_attributes][:context_attributes][:site_id]}}})
# end

# order
if params.has_key?(:c14s_order_by)
order = { params[:c14s_order_by] => params.fetch(:c14s_order, "asc") }
Expand Down Expand Up @@ -159,7 +163,13 @@ def c14_params
:position_y,
:position_z,
:position_crs
]}
]},
sample: [
:context_id,
contexts: [
:site_id
]
]
)
end

Expand Down
15 changes: 15 additions & 0 deletions app/controllers/data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def reset_manual_table_selection

def index
@data = Data.new(filter_params, select_params)
@raw_filter_params = filter_params
logger.debug { "Parsed filters: #{@data.filters.inspect}" }
@sites = @data.xrons.select("sites.id", "sites.lng", "sites.lat", "sites.name").distinct

Expand All @@ -35,6 +36,20 @@ def index
)
) AS geojson from (" + @sites.to_sql+ ") AS subquery1) AS subquery2").to_sql)[0]['measurements'], adapter: nil, serializer: nil
}
format.csv {
query = "COPY (SELECT * FROM data_views WHERE id IN (" + @data.xrons.pluck(:id).join(', ').to_s + ") ) TO STDOUT WITH CSV HEADER"

connection = ActiveRecord::Base.connection.raw_connection
csv_data = ""

connection.copy_data(query) do
while row = connection.get_copy_data
csv_data << row
end
end

render plain: csv_data, content_type: 'text/csv', filename: "data_#{Date.today}.csv"
}
end
end

Expand Down
30 changes: 25 additions & 5 deletions app/views/data/_toolbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<%= render "filter_status" %>
</span>

<ul class="nav">
<!--
<ul class="nav ms-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle"
id="dataTools"
Expand All @@ -67,6 +67,7 @@
<li><a class="dropdown-item" href="#">Sum calibrate</a></li>
</ul>
</li>
-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle"
id="dataDownload"
Expand All @@ -77,11 +78,30 @@
<%= bs_icon "download" %>
Download
</a>
<ul class="dropdown-menu" aria-labelledby="dataDownload">
<li><a class="dropdown-item" href="#">CSV</a></li>
<li><a class="dropdown-item" href="#">XLSX</a></li>
<ul class="dropdown-menu" aria-labelledby="dataDownload" style="padding:0px;">
<li>
<div class="d-grid gap-2">

<%= link_to data_path(format: :csv, filter: @raw_filter_params),
class: "nav-link",
title: "CSV",
download: "data_#{Date.today}.csv" do %>
<%=bs_icon("bi bi-filetype-csv")%> CSV
<% end %>
</div>
</li>
<li>
<div class="d-grid gap-2">

<%= link_to data_path(format: :json, filter: @raw_filter_params),
class: "nav-link",
title: "JSON",
download: "data_#{Date.today}.json" do %>
<%=bs_icon("bi bi-filetype-json")%> JSON
<% end%>
</div>
</li>
</ul>
</li>
</ul>
-->
</div>

0 comments on commit 46a9070

Please sign in to comment.