Skip to content

Commit

Permalink
Working csv export for all reports
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerldixon committed Sep 1, 2016
1 parent 657bb52 commit e896181
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ MAILER_ADDRESS=xxxxxxxxxx
MAILER_USERNAME=xxxxxxxxxx
MAILER_PASSWORD=xxxxxxxxxx
MAILER_HOST=xxxxxxxxxx

# Sidekiq admin
SIDEKIQ_ADMIN_USERNAME=xxxxxxxxxxxxx
SIDEKIQ_ADMIN_PASSWORD=xxxxxxxxxxxxx
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source 'https://rubygems.org'

# Frameworks
gem 'rails', '4.2.6'
gem 'sinatra', :require => false


# DB
gem 'pg', '~> 0.18.4'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ DEPENDENCIES
select2-rails!
sidekiq (~> 4.1.4)
simplecov
sinatra
slack-notifier (~> 1.5.1)
uglifier (>= 1.3.0)
web-console (~> 2.0)
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
require 'sidekiq'
require 'sidekiq/web'

Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
[user, password] == [ENV['SIDEKIQ_ADMIN_USERNAME'], ENV['SIDEKIQ_ADMIN_PASSWORD']]
end

Sidekiq.configure_server do |config|
config.redis = {url: $redis.client.options[:url]}
end
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
resources :users, controller: "users"
end

require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'

get "/search", to: "reports#search", as: "search_reports"
get "/export", to: "reports#export", as: "export_reports"
get "/reports/:id/validate", to: "reports#validate", as: "validate_report"
Expand Down
34 changes: 16 additions & 18 deletions lib/modules/csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ def self.build(reports)
"Confiscation?", "Arrests Made?", "Prosecution?", "Prosecution Successful?", "Punishment Successful?",
"Other Illegal Activities", "Man-made disturbances"]

filepath = "#{Rails.root.to_s}/tmp/csv_export_#{DateTime.now.to_i}"
filepath = "#{Rails.root.to_s}/tmp/csv_export_#{DateTime.now.to_i}.csv"

CSV.open(filepath, "wb") do |csv|
csv << column_names
reports.each do |report|
# Make the first row for the report, no apes
row = make_report_row(report)
csv << row
csv << self.make_report_row(report)

# For each ape in live, dead and parts, make a row
statuses = ['live', 'dead', 'parts']
statuses.each do |status|
apes['answers'][status].each do |ape|
row = make_report_row(report, ape, status.to_sym)
csv << row
['live', 'dead', 'parts'].each do |status|
if report.data.dig('answers', status).present? # If any live/dead/parts
report.data['answers'][status].each do |ape|
csv << self.make_report_row(report, ape, status.to_sym)
end
end
end
end
Expand All @@ -36,7 +35,7 @@ def self.build(reports)
filepath
end

def make_report_row(report, ape=nil, status=nil)
def self.make_report_row(report, ape=nil, status=nil)
# If an ape is passed in, it create a row with the details of this ape,
# if not, it leaves these details blank and just creates a report row

Expand All @@ -47,16 +46,16 @@ def make_report_row(report, ape=nil, status=nil)
report&.user&.agency&.name,
report.data.dig('answers', 'own_organisation', 'selected'),
report.created_at,
report.dig('answers', 'country_of_discovery', 'selected'),
report.data('answers', 'region_of_discovery', 'selected'),
report.data.dig('answers', 'country_of_discovery', 'selected'),
report.data.dig('answers', 'region_of_discovery', 'selected'),
report.answer_to("date_of_discovery")&.strftime("%d/%m/%Y"),
report.data.dig('answers', 'location_coords', 'selected'),
report.data.dig('answers', 'location_coords', 'selected')&.values&.join(", "),
report.data.dig('answers', 'type_of_location', 'selected')
]

if ape.present? && (status == :live or status == :dead)
ape_data = [
status.titleize,
status.to_s.titleize,
ape.dig("genus_#{status}", 'selected'),
ape.dig("species_subspecies_#{status}", 'selected'),
ape.dig("intended_use_#{status}", 'selected'),
Expand All @@ -71,8 +70,7 @@ def make_report_row(report, ape=nil, status=nil)
]

# Add empty fields for body parts
ape_data += Array.new(7, "")
end
ape_data += Array.new(7, nil)
else
#"Bone Qty",
#"Foot/Hand Qty",
Expand All @@ -81,7 +79,7 @@ def make_report_row(report, ape=nil, status=nil)
#"Meat Kg",
#"Skin Qty",
#"Skull Qty"
ape_data = Array.new(19, "")
ape_data = Array.new(19, nil)
end

report_data_2 = [
Expand All @@ -90,8 +88,8 @@ def make_report_row(report, ape=nil, status=nil)
report.data.dig('answers', 'prosecution', 'selected'),
report.data.dig('answers', 'prosecution_successful', 'selected'),
report.data.dig('answers', 'punishment', 'selected'),
report.data.dig('answers', 'illegal_activities', 'selected'),
report.data.dig('answers', 'proximity', 'selected')
report.data.dig('answers', 'illegal_activities', 'selected')&.join(", "),
report.data.dig('answers', 'proximity', 'selected')&.join(", ")
]

# Join the segments to form the full row
Expand Down

0 comments on commit e896181

Please sign in to comment.