Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 13, 2019
2 parents 5f26597 + 627228e commit 8a2efc0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 54 deletions.
126 changes: 72 additions & 54 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -187,9 +155,6 @@ def organizations
rorId
created
deleted
doisCountCurrentYear
doisCountPreviousYear
doisCountTotal
)

csv = headers.to_csv
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }


Expand Down

0 comments on commit 8a2efc0

Please sign in to comment.