diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index d2012d1b7..836554449 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -127,38 +127,6 @@ def organizations page_num += 1 end - # Loop through all clients - clients = [] - - page = { size: 1000, number: 1} - response = Client.query(nil, page: page, include_deleted: true) - clients = clients + response.records.to_a - - total = response.results.total - total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0 - - # keep going for all pages - page_num = 2 - while page_num <= total_pages - page = { size: 1000, number: page_num } - response = Client.query(nil, page: page, include_deleted: true) - clients = clients + response.records.to_a - page_num += 1 - end - - # Get doi counts via DOIS query and combine next to clients. - response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1}, totals_agg: "client") - - client_totals = {} - totals_buckets = response.response.aggregations.clients_totals.buckets - totals_buckets.each do |totals| - client_totals[totals["key"]] = { - "count" => totals["doc_count"], - "this_year" => totals.this_year.buckets[0]["doc_count"], - "last_year" => totals.last_year.buckets[0]["doc_count"] - } - end - respond_to do |format| format.csv do headers = %W( @@ -187,9 +155,6 @@ def organizations rorId created deleted - doisCountCurrentYear - doisCountPreviousYear - doisCountTotal ) csv = headers.to_csv @@ -220,15 +185,82 @@ def organizations twitter: provider.twitter_handle, rorId: provider.ror_id, created: provider.created, - deleted: provider.deleted_at, - doisCountCurrentYear: nil, - doisCountPreviousYear: nil, - doisCountTotal: nil + deleted: provider.deleted_at }.values csv += CSV.generate_line row end + send_data csv, filename: "organizations-#{Date.today}.csv" + end + end + + rescue 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 + end + end + + + def repositories + authorize! :export, :repositories + begin + # Loop through all clients + clients = [] + + page = { size: 1000, number: 1} + response = Client.query(nil, page: page, include_deleted: true) + clients = clients + response.records.to_a + + total = response.results.total + total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0 + + # keep going for all pages + page_num = 2 + while page_num <= total_pages + page = { size: 1000, number: page_num } + response = Client.query(nil, page: page, include_deleted: true) + clients = clients + response.records.to_a + page_num += 1 + end + + # Get doi counts via DOIS query and combine next to clients. + response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1}, totals_agg: "client") + + client_totals = {} + totals_buckets = response.response.aggregations.clients_totals.buckets + totals_buckets.each do |totals| + client_totals[totals["key"]] = { + "count" => totals["doc_count"], + "this_year" => totals.this_year.buckets[0]["doc_count"], + "last_year" => totals.last_year.buckets[0]["doc_count"] + } + end + + respond_to do |format| + format.csv do + headers = %W( + accountName + fabricaAccountId + parentFabricaAccountId + salesForceId + parentSalesForceId + isActive + accountDescription + accountWebsite + generalContactEmail + created + deleted + doisCountCurrentYear + doisCountPreviousYear + doisCountTotal + ) + + csv = headers.to_csv + clients.each do |client| client_id = client.symbol.downcase row = { @@ -240,21 +272,7 @@ def organizations isActive: client.is_active == "\x01", accountDescription: client.description, accountWebsite: client.url, - region: nil, - focusArea: nil, - organisztionType: nil, - accountType: 'repository', generalContactEmail: client.system_email, - groupEmail: nil, - billingStreet: nil, - billingPostalCode: nil, - billingCity: nil, - billingDepartment: nil, - billingOrganization: nil, - billingState: nil, - billingCountry: nil, - twitter: nil, - rorId: nil, created: client.created, deleted: client.deleted_at, doisCountCurrentYear: client_totals[client_id] ? client_totals[client_id]["this_year"] : nil, @@ -265,7 +283,7 @@ def organizations csv += CSV.generate_line row end - send_data csv, filename: "organizations-#{Date.today}.csv" + send_data csv, filename: "repositories-#{Date.today}.csv" end end diff --git a/app/models/ability.rb b/app/models/ability.rb index 2b70f4055..2bf18646a 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -16,6 +16,7 @@ def initialize(user) end can :export, :contacts can :export, :organizations + can :export, :repositories elsif user.role_id == "staff_user" can :read, :all elsif user.role_id == "provider_admin" && user.provider_id.present? diff --git a/config/routes.rb b/config/routes.rb index 89a4e095a..c251e1d26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,6 +59,7 @@ # Reporting get 'export/organizations', :to => 'export#organizations', defaults: { format: :csv } + get 'export/repositories', :to => 'export#repositories', defaults: { format: :csv } get 'export/contacts', :to => 'export#contacts', defaults: { format: :csv }