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 Jul 18, 2019
2 parents e636f57 + 87ef3d3 commit 4911c08
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
29 changes: 20 additions & 9 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def index
"page[size]" => page[:size] }.compact.to_query
}.compact

options[:include] = [] if @include.include? :dois

options[:is_collection] = true

bmr = Benchmark.ms {
Expand All @@ -174,15 +174,26 @@ def index
### Unfotunately fast_json fetches relations by item and one cannot pass and the includes
### We obtain all the events' dois, we batchload them and serilize them
### Then we serlize all the events and we merged them both together
events_serialized = EventSerializer.new(results, options).serializable_hash


if @include.include? :dois
doi_names = (results.map { |event| event.doi}).join(",")
events_serialized[:included] = DoiSerializer.new(Doi.find_by_id(doi_names).results, {is_collection: true}).serializable_hash.dig(:data)
### TODO: remove after benchmark
if @include.include?(:dois) && params["batchload"].present?
if params["batchload"].start_with?("batchload")
options[:include] = @include
render json: BatchSerializer.new(results, options).serialized_json, status: :ok
elsif params["batchload"].start_with?("singlecall")
options[:include] = []
doi_names = (results.map { |event| event.doi}).join(",")
# doi_names = "10.18711/0jdfnq2c,10.14288/1.0043659,10.25620/iciber.issn.1476-4687"
events_serialized = EventSerializer.new(results, options).serializable_hash
events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), {is_collection: true}).serializable_hash.dig(:data)

render json: events_serialized, status: :ok
end
else
options[:include] = @include
params['batchload'] = "Normal"
render json: EventSerializer.new(results, options).serialized_json, status: :ok
end

render json: events_serialized, status: :ok

}

if bmr > 3000
Expand Down
13 changes: 5 additions & 8 deletions app/models/concerns/batch_loader_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
module BatchLoaderHelper
extend ActiveSupport::Concern


included do
### TODO: remove after benchmark
class_methods do
def load_doi(object)
logger = Logger.new(STDOUT)
BatchLoader.for(object).batch do |dois, loader|
dois = object ### keep this
logger.info "Requesting " + object
BatchLoader.for(object.uuid).batch do |dois, loader|
dois = object.doi
results = Doi.find_by_id(dois).results
loader.call(object, results)
loader.call(object.uuid, results)
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions app/serializers/batch_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

class BatchSerializer
include FastJsonapi::ObjectSerializer
include BatchLoaderHelper

set_key_transform :camel_lower
set_type :events
set_id :uuid

attributes :subj_id, :obj_id, :source_id, :relation_type_id, :total, :message_action, :source_token, :license, :occurred_at, :timestamp

has_many :dois, record_type: :dois, serializer: DoiSerializer, id_method_name: :doi do |object|
load_doi(object)
end

attribute :timestamp, &:updated_at
end
5 changes: 4 additions & 1 deletion app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class EventSerializer

attributes :subj_id, :obj_id, :source_id, :relation_type_id, :total, :message_action, :source_token, :license, :occurred_at, :timestamp

has_many :dois, record_type: :dois, id_method_name: :doi

has_many :dois, record_type: :dois, serializer: DoiSerializer, id_method_name: :uid do |object|
Doi.find_by_id(object.doi).results
end

attribute :timestamp, &:updated_at
end

0 comments on commit 4911c08

Please sign in to comment.