Skip to content

Commit

Permalink
Merge pull request #454 from datacite/fix_nested_objs
Browse files Browse the repository at this point in the history
Attributes `sub` and `obj` of the `event` index are being currently saved as kebab_case thus cannot be accessed
  • Loading branch information
kjgarza authored May 18, 2020
2 parents 48ff650 + 35cc7f1 commit 1bd76be
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/jobs/camelcase_nested_objects_by_id_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CamelcaseNestedObjectsByIdJob < ActiveJob::Base
queue_as :lupo_background

def perform(uuid, options = {})
Event.camelcase_nested_objects(uuid)
end
end
2 changes: 1 addition & 1 deletion app/jobs/event_registrant_update_by_id_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(id, options={})
registrant_id = get_crossref_member_id(item.obj_id)
end

obj = item.obj.merge("registrant_id" => registrant_id) unless registrant_id.nil?
obj = item.obj.merge("registrantId" => registrant_id) unless registrant_id.nil?
Rails.logger.info obj.inspect
item.update_attributes(obj: obj) if obj.present?
when "crossref"
Expand Down
44 changes: 41 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,40 @@ def self.subj_id_check(options = {})
end
end

def self.modify_nested_objects(options = {})
size = (options[:size] || 1000).to_i
cursor = [options[:from_id], options[:until_id]]

response = Event.query(nil, page: { size: 1, cursor: [] })
Rails.logger.info "[modify_nested_objects] #{response.results.total} events for source datacite-crossref."

# walk through results using cursor
if response.results.total.positive?
while response.results.results.length.positive?
response = Event.query(nil, page: { size: size, cursor: cursor })
break unless response.results.results.length.positive?

Rails.logger.info "[modify_nested_objects] modify_nested_objects #{response.results.results.length} events starting with _id #{response.results.to_a.first[:_id]}."
cursor = response.results.to_a.last[:sort]
Rails.logger.info "[modify_nested_objects] Cursor: #{cursor} "

ids = response.results.results.map(&:uuid).uniq
ids.each do |id|
CamelcaseNestedObjectsByIdJob.perform_later(id, options)
end
end
end
end

def self.camelcase_nested_objects(uuid)
event = Event.find_by(uuid: uuid)
if event.present?
subj = event.subj.transform_keys { |key| key.to_s.underscore.camelcase(:lower) }
obj = event.obj.transform_keys { |key| key.to_s.underscore.camelcase(:lower) }
event.update_attributes(subj: subj, obj: obj)
end
end

def self.label_state_event(event)
subj_prefix = event[:subj_id][/(10\.\d{4,5})/, 1]
unless Prefix.where(uid: subj_prefix).exists?
Expand Down Expand Up @@ -625,7 +659,7 @@ def uuid_format
end

def registrant_id
[subj["registrant_id"], obj["registrant_id"], subj["provider_id"], obj["provider_id"]].compact
[subj["registrantId"], obj["registrantId"], subj["providerId"], obj["providerId"]].compact
end

def subtype
Expand Down Expand Up @@ -681,8 +715,8 @@ def obj_cache_key

def citation_year
"" unless (INCLUDED_RELATION_TYPES + RELATIONS_RELATION_TYPES).include?(relation_type_id)
subj_publication = subj["date_published"] || (date_published(subj_id) || year_month)
obj_publication = obj["date_published"] || (date_published(obj_id) || year_month)
subj_publication = subj["datePublished"] || subj["date_published"] || (date_published(subj_id) || year_month)
obj_publication = obj["datePublished"] || obj["date_published"] || (date_published(obj_id) || year_month)
[subj_publication[0..3].to_i, obj_publication[0..3].to_i].max
end

Expand Down Expand Up @@ -742,6 +776,10 @@ def set_defaults
self.subj = subj.to_h.merge("id" => self.subj_id)
self.obj = obj.to_h.merge("id" => self.obj_id)

### makes keys camel case to match JSONAPI
self.subj.transform_keys! { |key| key.to_s.underscore.camelcase(:lower) }
self.obj.transform_keys! { |key| key.to_s.underscore.camelcase(:lower) }

self.total = 1 if total.blank?
self.relation_type_id = "references" if relation_type_id.blank?
self.occurred_at = Time.zone.now.utc if occurred_at.blank?
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ namespace :crossref_events do
end
end

namespace :modify_nested_objects do
desc 'changes casing of nested objects in the database'
task :check => :environment do
from_id = (ENV['FROM_ID'] || Event.minimum(:id)).to_i
until_id = (ENV['UNTIL_ID'] || Event.maximum(:id)).to_i

Event.modify_nested_objects(from_id: from_id, until_id: until_id)
end
end

namespace :datacite_crossref do
desc 'Import crossref dois for all events'
task :import_doi => :environment do
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@
source_id { "datacite_related" }
source_token { "datacite_related_123" }
sequence(:subj_id) { |n| "http://doi.org/10.5061/DRYAD.47SD5e/#{n}" }
subj { { "datePublished" => "2006-06-13T16:14:19Z" } }
subj { { "date_published" => "2006-06-13T16:14:19Z", "registrant_id" => "datacite.datacite" } }
obj_id { "http://doi.org/10.5061/DRYAD.47SD5/1" }
relation_type_id { "references" }
end
Expand Down
9 changes: 9 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@
# end
# end
end

describe "camelcase_nested_objects" do
subject { create(:event_for_datacite_related) }

it "should transform keys" do
Event.camelcase_nested_objects(subject.uuid)
expect(subject.subj.keys).to include("datePublished", "registrantId", "id")
end
end
end
28 changes: 28 additions & 0 deletions spec/requests/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,34 @@
expect(json.dig("meta", "registrants", 0, "id")).to eq("datacite.crossref.citations")
end
end

context "with nested attrtibutes" do
let(:uri) { "/events" }
let(:params) do
{ "data" => { "type" => "events",
"attributes" => {
"subjId" => "https://doi.org/10.18713/jimis-170117-1-2",
"subj" => { "@id":"https://doi.org/10.18713/jimis-170117-1-2", "@type":"ScholarlyArticle", "datePublished":"2017", "proxyIdentifiers":[], "registrantId":"datacite.inist.umr7300" },
"obj" => { "@id":"https://doi.org/10.1016/j.jastp.2013.05.001", "@type":"ScholarlyArticle", "datePublished":"2013-09", "proxyIdentifiers":["13646826"], "registrantId":"datacite.crossref.citations" },
"objId" => "https://doi.org/10.1016/j.jastp.2013.05.001",
"relationTypeId" => "references",
"sourceId" => "datacite-crossref",
"sourceToken" => "sourceToken" } } }
end

it "are correctly stored" do
post uri, params, headers


expect(last_response.status).to eq(201)
puts json.dig("data", "id")
event = Event.where(uuid: json.dig("data", "id")).first
puts event.inspect
expect(event[:obj].has_key?('datePublished')).to be_truthy
expect(event[:obj].has_key?('registrantId')).to be_truthy
expect(event[:obj].has_key?('proxyIdentifiers')).to be_truthy
end
end
end

context "upsert" do
Expand Down

0 comments on commit 1bd76be

Please sign in to comment.