Skip to content

Commit

Permalink
support until-date query parameter for export
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Mar 10, 2020
1 parent c2ad883 commit 09b41e0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def index
response = Client.query(params[:query],
year: params[:year],
from_date: params[:from_date],
until_date: params[:until_date],
provider_id: params[:provider_id],
re3data_id: params[:re3data_id],
opendoar_id: params[:opendoar_id],
Expand Down
46 changes: 29 additions & 17 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def contacts
begin
# Loop through all providers
page = { size: 1000, number: 1}
response = Provider.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Provider.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
providers = response.results.to_a

total = response.results.total
Expand All @@ -34,7 +34,7 @@ def contacts
page_num = 2
while page_num <= total_pages
page = { size: 1000, number: page_num }
response = Provider.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Provider.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
providers = providers + response.results.to_a
page_num += 1
end
Expand Down Expand Up @@ -101,13 +101,17 @@ def contacts
]
end

send_data csv, filename: "contacts-#{params.fetch(:type, "all")}-#{Date.today}.csv"
if params[:until_date]
filename = "contacts-#{params.fetch(:type, "all")}-#{params[:until_date]}.csv"
else
filename = "contacts-#{params.fetch(:type, "all")}-#{Date.today}.csv"
end

send_data csv, filename: filename
rescue StandardError, Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
render json: { "errors" => { "title" => exception.message }}.to_json, status: :bad_request
end
end

Expand All @@ -117,7 +121,7 @@ def organizations
begin
# Loop through all providers
page = { size: 1000, number: 1 }
response = Provider.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Provider.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
providers = response.results.to_a

total = response.results.total
Expand All @@ -127,7 +131,7 @@ def organizations
page_num = 2
while page_num <= total_pages
page = { size: 1000, number: page_num }
response = Provider.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Provider.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
providers = providers + response.results.to_a
page_num += 1
end
Expand Down Expand Up @@ -192,13 +196,17 @@ def organizations
csv += CSV.generate_line row
end

send_data csv, filename: "organizations-#{Date.today}.csv"
if params[:until_date]
filename = "organizations-#{params[:until_date]}.csv"
else
filename = "organizations-#{Date.today}.csv"
end

send_data csv, filename: filename
rescue StandardError, Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => message }}.to_json, status: :bad_request
render json: { "errors" => { "title" => exception.message }}.to_json, status: :bad_request
end
end

Expand All @@ -207,7 +215,7 @@ def repositories
begin
# Loop through all clients
page = { size: 1000, number: 1 }
response = Client.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Client.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
clients = response.results.to_a

total = response.results.total
Expand All @@ -217,7 +225,7 @@ def repositories
page_num = 2
while page_num <= total_pages
page = { size: 1000, number: page_num }
response = Client.query(nil, page: page, from_date: params[:from_date], include_deleted: true, exclude_registration_agencies: true)
response = Client.query(nil, page: page, from_date: params[:from_date], until_date: params[:until_date], include_deleted: true, exclude_registration_agencies: true)
clients = clients + response.results.to_a
page_num += 1
end
Expand Down Expand Up @@ -286,12 +294,16 @@ def repositories
csv += CSV.generate_line row
end

send_data csv, filename: "repositories-#{Date.today}.csv"
if params[:until_date]
filename = "repositories-#{params[:until_date]}.csv"
else
filename = "repositories-#{Date.today}.csv"
end

send_data csv, filename: filename
rescue StandardError, Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

# message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")

render json: { "errors" => { "title" => exception.message }}.to_json, status: :bad_request
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def index
else
response = Provider.query(params[:query],
year: params[:year],
from_date: params[:from_date],
until_date: params[:until_date],
region: params[:region],
consortium_id: params[:provider_id],
organization_type: params[:organization_type],
Expand Down
1 change: 1 addition & 0 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def index
exclude_registration_agencies: params[:exclude_registration_agencies],
year: params[:year],
from_date: params[:from_date],
until_date: params[:until_date],
region: params[:region],
consortium_id: params[:consortium_id],
member_type: params[:member_type],
Expand Down
1 change: 1 addition & 0 deletions app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def index
response = Client.query(params[:query],
year: params[:year],
from_date: params[:from_date],
until_date: params[:until_date],
provider_id: params[:provider_id],
consortium_id: params[:consortium_id],
re3data_id: params[:re3data_id],
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def query(query, options={})
if self.name == "Provider"
must << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
must << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
must << { term: { region: options[:region].upcase }} if options[:region].present?
must << { term: { "consortium_id.raw" => options[:consortium_id] }} if options[:consortium_id].present?
must << { term: { member_type: options[:member_type] }} if options[:member_type].present?
Expand All @@ -227,6 +228,7 @@ def query(query, options={})
elsif self.name == "Client"
must << { range: { created: { gte: "#{options[:year].split(",").min}||/y", lte: "#{options[:year].split(",").max}||/y", format: "yyyy" }}} if options[:year].present?
must << { range: { updated: { gte: "#{options[:from_date]}||/d" }}} if options[:from_date].present?
must << { range: { updated: { lte: "#{options[:until_date]}||/d" }}} if options[:until_date].present?
must << { terms: { "software.raw" => options[:software].split(",") }} if options[:software].present?
must << { terms: { certificate: options[:certificate].split(",") }} if options[:certificate].present?
must << { terms: { repository_type: options[:repository_type].split(",") }} if options[:repository_type].present?
Expand Down

0 comments on commit 09b41e0

Please sign in to comment.