diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 2ae3a2edd..d78240e5b 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -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], diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index 40341042d..9b4cf2b27 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index ef15c6903..00f77c96c 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -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], diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 3e707de6e..b7318e300 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -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], diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 60a14e12a..43b3f7b4f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -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], diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 45f13763e..5a835b870 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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? @@ -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?