From 56f3cec55bcf8e2745f876a4d2463401d7e99e05 Mon Sep 17 00:00:00 2001 From: Kristian Garza Date: Thu, 18 Jul 2019 11:02:11 +0200 Subject: [PATCH] added extra serializer to ease logic --- app/controllers/events_controller.rb | 33 +++++++++++++--------- app/models/concerns/batch_loader_helper.rb | 13 ++++----- app/serializers/batch_serializer.rb | 17 +++++++++++ app/serializers/event_serializer.rb | 5 +++- 4 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 app/serializers/batch_serializer.rb diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index dd55a4cca..f237fa293 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -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 { @@ -174,21 +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] = if params["batchload"] == "true" || params["batchload"].nil? - logger.info "batchload" - DoiSerializer.new(load_doi(doi_names), {is_collection: true}).serializable_hash.dig(:data) - elsif params["batchload"] == "false" - logger.info "find_by_doi" - Doi.find_by_id(doi_names).results + ### 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 diff --git a/app/models/concerns/batch_loader_helper.rb b/app/models/concerns/batch_loader_helper.rb index 4f441bcdd..52cbe6c0f 100644 --- a/app/models/concerns/batch_loader_helper.rb +++ b/app/models/concerns/batch_loader_helper.rb @@ -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 diff --git a/app/serializers/batch_serializer.rb b/app/serializers/batch_serializer.rb new file mode 100644 index 000000000..09b3dafb0 --- /dev/null +++ b/app/serializers/batch_serializer.rb @@ -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 diff --git a/app/serializers/event_serializer.rb b/app/serializers/event_serializer.rb index 9ac725b34..cd6bb0ab6 100644 --- a/app/serializers/event_serializer.rb +++ b/app/serializers/event_serializer.rb @@ -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