From 6989adb76d8829562bedeaac76fa42cf173f52ba Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 9 Dec 2019 15:21:47 +0000 Subject: [PATCH] support scroll in old_events_controller. #371 --- app/controllers/old_events_controller.rb | 116 ++++++++++++++--------- app/models/event.rb | 8 -- 2 files changed, 70 insertions(+), 54 deletions(-) diff --git a/app/controllers/old_events_controller.rb b/app/controllers/old_events_controller.rb index e20616053..819d2ddd8 100644 --- a/app/controllers/old_events_controller.rb +++ b/app/controllers/old_events_controller.rb @@ -98,61 +98,85 @@ def index occurred_at: params[:occurred_at], publication_year: params[:publication_year], year_month: params[:year_month], + scroll_id: params[:scroll_id], page: page, sort: sort) end - total = response.results.total - total_for_pages = page[:cursor].nil? ? total.to_f : [total.to_f, 10000].min - total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0 + if page[:scroll].present? + results = response.results + total = response.total + else + total = response.results.total + total_for_pages = page[:cursor].nil? ? total.to_f : [total.to_f, 10000].min + total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0 + end - sources = total > 0 ? facet_by_source(response.response.aggregations.sources.buckets) : nil - prefixes = total > 0 ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil - citation_types = total > 0 ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil - relation_types = total > 0 ? facet_by_relation_type_v1(response.response.aggregations.relation_types.buckets) : nil - registrants = total > 0 && params[:extra] ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil - pairings = total > 0 && params[:extra] ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil + if page[:scroll].present? + options = {} + options[:meta] = { + total: total, + "scroll-id" => response.scroll_id, + }.compact + options[:links] = { + self: request.original_url, + next: results.size < page[:size] || page[:size] == 0 ? nil : request.base_url + "/events?" + { + "scroll-id" => response.scroll_id, + "page[scroll]" => "3m", + "page[size]" => page[:size] }.compact.to_query + }.compact + options[:is_collection] = true + + render json: OldEventSerializer.new(results, options).serialized_json, status: :ok + else + sources = total > 0 ? facet_by_source(response.aggregations.sources.buckets) : nil + prefixes = total > 0 ? facet_by_source(response.aggregations.prefixes.buckets) : nil + citation_types = total > 0 ? facet_by_citation_type(response.aggregations.citation_types.buckets) : nil + relation_types = total > 0 ? facet_by_relation_type_v1(response.aggregations.relation_types.buckets) : nil + registrants = total > 0 && params[:extra] ? facet_by_registrants(response.aggregations.registrants.buckets) : nil + pairings = total > 0 && params[:extra] ? facet_by_pairings(response.aggregations.pairings.buckets) : nil - results = response.results + results = response.results - options = {} - options[:meta] = { - total: total, - "total-pages" => total_pages, - page: page[:cursor].nil? && page[:number].present? ? page[:number] : nil, - sources: sources, - prefixes: prefixes, - "citation-types" => citation_types, - "relation-types" => relation_types, - pairings: pairings, - registrants: registrants - }.compact - - options[:links] = { - self: request.original_url, - next: results.size < page[:size] ? nil : request.base_url + "/events?" + { - "query" => params[:query], - "subj-id" => params[:subj_id], - "obj-id" => params[:obj_id], - "doi" => params[:doi], - "orcid" => params[:orcid], - "prefix" => params[:prefix], - "subtype" => params[:subtype], - "citation_type" => params[:citation_type], - "source-id" => params[:source_id], - "relation-type-id" => params[:relation_type_id], - "issn" => params[:issn], - "registrant-id" => params[:registrant_id], - "publication-year" => params[:publication_year], - "year-month" => params[:year_month], - "page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(results.to_a.last[:sort]).join(',')) : nil, - "page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil, - "page[size]" => page[:size] }.compact.to_query + options = {} + options[:meta] = { + total: total, + "total-pages" => total_pages, + page: page[:cursor].nil? && page[:number].present? ? page[:number] : nil, + sources: sources, + prefixes: prefixes, + "citation-types" => citation_types, + "relation-types" => relation_types, + pairings: pairings, + registrants: registrants }.compact - options[:include] = @include - options[:is_collection] = true - render json: OldEventSerializer.new(results, options).serialized_json, status: :ok + options[:links] = { + self: request.original_url, + next: results.size < page[:size] ? nil : request.base_url + "/events?" + { + "query" => params[:query], + "subj-id" => params[:subj_id], + "obj-id" => params[:obj_id], + "doi" => params[:doi], + "orcid" => params[:orcid], + "prefix" => params[:prefix], + "subtype" => params[:subtype], + "citation_type" => params[:citation_type], + "source-id" => params[:source_id], + "relation-type-id" => params[:relation_type_id], + "issn" => params[:issn], + "registrant-id" => params[:registrant_id], + "publication-year" => params[:publication_year], + "year-month" => params[:year_month], + "page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(results.to_a.last[:sort]).join(',')) : nil, + "page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil, + "page[size]" => page[:size] }.compact.to_query + }.compact + options[:include] = @include + options[:is_collection] = true + + render json: OldEventSerializer.new(results, options).serialized_json, status: :ok + end end def destroy diff --git a/app/models/event.rb b/app/models/event.rb index 9e256efb3..b33481850 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -207,12 +207,6 @@ def self.query_aggregations } } - sum_year_distribution = { - sum_bucket: { - buckets_path: "years>total_by_year" - } - } - { sources: { terms: { field: "source_id", size: 50, min_doc_count: 1 } }, prefixes: { terms: { field: "prefix", size: 50, min_doc_count: 1 } }, @@ -224,8 +218,6 @@ def self.query_aggregations } end - - def self.citation_count_aggregation { citations: {