Skip to content

Commit

Permalink
show missing dois in repository export. #676
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Nov 26, 2020
1 parent 6270de1 commit 5f7ea1b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
13 changes: 11 additions & 2 deletions app/controllers/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,25 @@ def repositories
"doisCurrentYear",
"doisPreviousYear",
"doisTotal",
"doisDbTotal",
"doisMissing"
]

csv = headers.to_csv

# get doi counts from database
dois_by_client = DataciteDoi.group(:datacentre).count

clients.each do |client|
# Limit for salesforce default of max 80 chars
name =
+client.name.truncate(80)
# Clean the name to remove quotes, which can break csv parsers
name.gsub!(/["']/, "")

db_total = dois_by_client[client.id.to_i].to_i
es_total = client_totals[client.uid] ? client_totals[client.uid]["count"] : 0

row = {
accountName: name,
fabricaAccountId: client.symbol,
Expand Down Expand Up @@ -435,8 +443,9 @@ def repositories
else
0
end,
doisCountTotal:
client_totals[client.uid] ? client_totals[client.uid]["count"] : 0,
doisCountTotal: es_total,
doisDbTotal: db_total,
doisMissing: db_total - es_total,
}.values

csv += CSV.generate_line row
Expand Down
47 changes: 24 additions & 23 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -669,37 +669,38 @@ def self.export_doi_counts(query: nil)
dois_by_client = DataciteDoi.group(:datacentre).count
rows =
clients.reduce([]) do |sum, client|
db_total = dois_by_client.dig(client.id).to_i
db_total = dois_by_client[client.id.to_i].to_i
es_total =
client_totals[client.uid] ? client_totals[client.uid]["count"] : 0
# if (db_total - es_total) > 0
# Limit for salesforce default of max 80 chars
name = +client.name.truncate(80)
# Clean the name to remove quotes, which can break csv parsers
name.gsub!(/["']/, "")

row = {
accountName: name,
fabricaAccountId: client.symbol,
parentFabricaAccountId:
client.provider.present? ? client.provider.symbol : nil,
doisCountTotal: db_total,
doisMissing: db_total - es_total,
}.values

puts CSV.generate_line(row)

sum << CSV.generate_line(row)
# end
if (db_total - es_total) > 0
# Limit for salesforce default of max 80 chars
name = +client.name.truncate(80)
# Clean the name to remove quotes, which can break csv parsers
name.gsub!(/["']/, "")

row = {
accountName: name,
fabricaAccountId: client.symbol,
parentFabricaAccountId:
client.provider.present? ? client.provider.symbol : nil,
doisCountTotal: es_total,
doisDbTotal: db_total,
doisMissing: db_total - es_total,
}.values

puts CSV.generate_line(row)

sum << CSV.generate_line(row)
end

sum
end

csv = CSV::Table.new(rows, headers: headers)
csv = [headers] + rows

logger.warn "Found #{csv.count} repositories with missing DOIs."
logger.warn "Found #{csv.size} repositories with missing DOIs."

csv.to_csv
csv.join(",")
end

protected
Expand Down
8 changes: 6 additions & 2 deletions spec/requests/exports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@
expect(csv[1]).to start_with(
"University of Virginia Library,UVA.LIBRARY,UVA,true",
)
dois_total = csv[1].strip.split(",").last.to_i
dois_total = csv[1].strip.split(",")[15].to_i
expect(dois_total).to eq(3)
dois_missing = csv[1].strip.split(",")[17].to_i
expect(dois_missing).to eq(0)
end

it "returns repositories from date", vcr: false do
Expand All @@ -109,8 +111,10 @@
expect(csv[1]).to start_with(
"University of Virginia Library,UVA.LIBRARY,UVA,true",
)
dois_total = csv[1].strip.split(",").last.to_i
dois_total = csv[1].strip.split(",")[15].to_i
expect(dois_total).to eq(3)
dois_missing = csv[1].strip.split(",")[17].to_i
expect(dois_missing).to eq(0)
end
end

Expand Down

0 comments on commit 5f7ea1b

Please sign in to comment.