Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo into schema…
Browse files Browse the repository at this point in the history
…-4.5-r3
  • Loading branch information
codycooperross committed Dec 12, 2023
2 parents 5da319a + 6d7767e commit 3fdca4a
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gem "aws-sdk-sqs", "~> 1.3"
gem "base32-url", "~> 0.3"
gem "batch-loader", "~> 1.4", ">= 1.4.1"
gem "bcrypt", "~> 3.1.7"
gem "bolognese", "~> 2.0.2"
gem "bolognese", "~> 2.0.3"
gem "bootsnap", "~> 1.4", ">= 1.4.4", require: false
gem "cancancan", "~> 3.0"
gem "commonmarker", "~> 0.23.4"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ GEM
latex-decode (~> 0.0)
binding_of_caller (1.0.0)
debug_inspector (>= 0.0.1)
bolognese (2.0.2)
bolognese (2.0.3)
activesupport (>= 4.2.5)
benchmark_methods (~> 0.7)
bibtex-ruby (>= 5.1.0)
Expand Down Expand Up @@ -656,7 +656,7 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
better_errors
binding_of_caller
bolognese (~> 2.0.2)
bolognese (~> 2.0.3)
bootsnap (~> 1.4, >= 1.4.4)
bullet (~> 6.1)
byebug
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ def show

protected
def set_activity
response = Activity.find_by_id(params[:id])
@activity = response.results.first
@activity = Activity.find_by_request_uuid!(params[:id])
fail ActiveRecord::RecordNotFound if @activity.blank?
end
end
16 changes: 15 additions & 1 deletion app/models/doi/indexer/related_doi_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

module Doi::Indexer
class RelatedDoiIndexer
REQUIRED_KEYS = %w[
relatedIdentifier
relatedIdentifierType
relationType
]

def initialize(related_identifiers)
@related_identifiers = Array.wrap(related_identifiers)
@related_dois = nil
end

def is_related_doi?(related)
related.is_a?(Hash) &&
REQUIRED_KEYS.all? { |k| related.key?(k) } &&
related.fetch("relatedIdentifierType", nil) == "DOI"
end

def related_dois
@related_dois ||= @related_identifiers.select { |r| r.fetch("relatedIdentifierType", nil) == "DOI" }
@related_dois ||= @related_identifiers.select do |r|
is_related_doi?(r)
end
end

def related_grouped_by_id
Expand Down
72 changes: 33 additions & 39 deletions app/serializers/activity_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,49 @@ class ActivitySerializer
:version

attribute :changes do |object, params|
changes = object._source.changes
pub = changes.publisher
pub_obj = changes.publisher_obj
# Determine the source and action based on the object's type
if object.is_a? Activity
changes = object.audited_changes
action = object.action
else
changes = object._source.changes
action = object._source.action
end

ret = changes
# Extract publisher and publisher_obj from changes
pub = changes&.dig("publisher")
pub_obj = changes&.dig("publisher_obj")

if object._source.action == "update"
if pub || pub_obj
if params[:publisher] == "true"
if !pub_obj
changes.publisher =
[
{ "name": pub[0] },
{ "name": pub[1] },
]
else
changes.publisher = changes.publisher_obj
end
# Customize publisher information based on params[:publisher]
if pub || pub_obj
if params&.dig(:publisher) == "true"
if pub_obj
changes["publisher"] = pub_obj
else
if !pub
changes.publisher = [ pub_obj[0].name, pub_obj[1].name ]
end
changes["publisher"] =
action == "update" ? [
pub[0] ? { "name": pub[0] } : nil,
pub[1] ? { "name": pub[1] } : nil
] : { "name": pub }
end

ret = changes
end
elsif object._source.action == "create"
if pub || pub_obj
if params[:publisher] == "true"
if !pub_obj
changes.publisher = { "name": pub }
else
changes.publisher = changes.publisher_obj
end
else
if pub_obj
changes["publisher"] =
action == "update" ? [
pub_obj[0] ? pub_obj[0]["name"] : nil,
pub_obj[1] ? pub_obj[1]["name"] : nil
] : pub_obj["name"]
else
if !pub
changes.publisher = pub_obj.name
end
changes["publisher"] = pub
end

ret = changes
end
end

if pub_obj
changes.delete("publisher_obj")
end
# Remove the not needed "publisher_obj" key
changes.delete("publisher_obj")

ret
# Return the modified changes
changes
end

attribute "prov:wasDerivedFrom", &:was_derived_from
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

resources :heartbeat, only: %i[index]

resources :activities, only: %i[index show]
resources :activities, only: %i[index show], constraints: { id: /.+/ }

resources :clients, constraints: { id: /.+/ } do
resources :prefixes, constraints: { id: /.+/ }
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/activity.rake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace :activity do
puts Activity.monitor_reindex
end

desc "Create alias for activities"
task create_alias: :environment do
puts Activity.create_alias(index: ENV["INDEX"], alias: ENV["ALIAS"])
end

desc "Import all activities"
task import: :environment do
from_id = (ENV["FROM_ID"] || 1).to_i
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/elasticsearch.rake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace :elasticsearch do
Rake::Task["event:create_alias"].invoke

Rake::Task["activity:create_index"].invoke
Rake::Task["activity:create_alias"].invoke

Rake::Task["contact:create_index"].invoke
Rake::Task["contact:create_alias"].invoke
Expand Down
118 changes: 118 additions & 0 deletions spec/models/doi/related_doi_indexer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

# frozen_string_literal: true

require "rails_helper"

RSpec.describe Doi::Indexer::RelatedDoiIndexer do
describe "related_dois with different input" do
let(:good_related_identifier) do
{
"relatedIdentifier": "10.1234/5678",
"relatedIdentifierType": "DOI",
"relationType": "IsPartOf",
}.with_indifferent_access
end

it "handles nil input" do
expect(described_class.new(nil).related_dois).to eq([])
end

it "handles empty_string input" do
expect(described_class.new("").related_dois).to eq([])
end

it "handles a list with empty_string as input" do
expect(described_class.new([""]).related_dois).to eq([])
end


it "handles array of hashes with all required keys" do
expect(described_class.new([good_related_identifier]).related_dois).to eq(
[good_related_identifier])
end

it "handles single hash with all required keys" do
expect(described_class.new(good_related_identifier).related_dois).to eq(
[good_related_identifier])
end

it "exclude DOIs with if a required key is missing" do
expect(described_class.new(
good_related_identifier.except("relatedIdentifier")
).related_dois).to eq([])
expect(described_class.new(
good_related_identifier.except("relatedIdentifierType")
).related_dois).to eq([])
expect(described_class.new(
good_related_identifier.except("relationType")
).related_dois).to eq([])
end
end

describe "relation_type grouping" do
let(:related_identifiers) do
[
{
"relatedIdentifier": "10.1234/5678",
"relatedIdentifierType": "DOI",
"relationType": "IsPartOf",
"resourceTypeGeneral": "Dataset",
}.with_indifferent_access,
{
"relatedIdentifier": "10.1234/9999",
"relatedIdentifierType": "DOI",
"relationType": "HasVersion",
"resourceTypeGeneral": "Text",
}.with_indifferent_access,
{
"relatedIdentifier": "10.1234/9999",
"relatedIdentifierType": "DOI",
"relationType": "References",
"resourceTypeGeneral": "Text",
}.with_indifferent_access
]
end

it "can accept an array of valid identifiers" do
expect(described_class.new(related_identifiers).related_dois).to eq(related_identifiers)
end

it "groups related_dois by relatedIdentifier" do
expect(described_class.new(related_identifiers).relation_types_gouped_by_id).to eq(
{
"10.1234/5678" => ["is_part_of"],
"10.1234/9999" => ["has_version", "references"],
}
)
end

it "groups related_dois by relatedIdentifier" do
expect(described_class.new(related_identifiers).related_grouped_by_id).to eq(
{
"10.1234/5678" => [
{
"relatedIdentifier" => "10.1234/5678",
"relatedIdentifierType" => "DOI",
"relationType" => "IsPartOf",
"resourceTypeGeneral" => "Dataset",
}
],
"10.1234/9999" => [
{
"relatedIdentifier" => "10.1234/9999",
"relatedIdentifierType" => "DOI",
"relationType" => "HasVersion",
"resourceTypeGeneral" => "Text",
},
{
"relatedIdentifier" => "10.1234/9999",
"relatedIdentifierType" => "DOI",
"relationType" => "References",
"resourceTypeGeneral" => "Text",
}
],
}
)
end
end
end

0 comments on commit 3fdca4a

Please sign in to comment.