Skip to content

Commit

Permalink
Merge branch 'master' of github.com:datacite/lupo
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhallett committed Jan 9, 2020
2 parents 0db6b60 + a8500f5 commit be842b9
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ def index
dois_usage = total.positive? ? EventsQuery.new.usage(params[:doi]) : nil
# dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil
citations = total.positive? ? EventsQuery.new.citations(params[:doi]) : nil
citations_histogram = total.positive? ? EventsQuery.new.citations_histogram(params[:doi]) : nil
citations_histogram = total.positive? ? cached_citations_histogram_response(params[:doi]) : nil
references = total.positive? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.references.dois.buckets) : nil
relations = total.positive? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.relations.dois.buckets) : nil

views_histogram = total.positive? ? EventsQuery.new.views_histogram(params[:doi]) : nil
downloads_histogram = total.positive? ? EventsQuery.new.downloads_histogram(params[:doi]) : nil
views_histogram = total.positive? ? cached_views_histogram_response(params[:doi]) : nil
downloads_histogram = total.positive? ? cached_downloads_histogram_response(params[:doi]) : nil

# views = total.positive? ? EventsQuery.new.views(params[:doi]) : nil
# downloads = total.positive? ? EventsQuery.new.downloads(params[:doi]) : nil
Expand Down
1 change: 1 addition & 0 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Client < ActiveRecord::Base
alias_attribute :flipper_id, :symbol
alias_attribute :created_at, :created
alias_attribute :updated_at, :updated
alias_attribute :contact_email, :system_email
attr_readonly :symbol
delegate :symbol, to: :provider, prefix: true
delegate :consortium_id, to: :provider, allow_nil: true
Expand Down
60 changes: 60 additions & 0 deletions app/models/concerns/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,66 @@ def cached_alb_public_key(kid)
response.body.fetch("data", nil)
end
end

def cached_doi_citations_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_doi_citations_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.doi_citations(doi)
end
else
EventsQuery.new.doi_citations(doi)
end
end

def cached_doi_views_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_doi_views_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.doi_views(doi)
end
else
EventsQuery.new.doi_views(doi)
end
end

def cached_doi_downloads_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_doi_downloads_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.doi_downloads(doi)
end
else
EventsQuery.new.doi_downloads(doi)
end
end

def cached_citations_histogram_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_citations_histogram_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.citations_histogram(doi)
end
else
EventsQuery.new.citations_histogram(doi)
end
end

def cached_views_histogram_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_views_histogram_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.views_histogram(doi)
end
else
EventsQuery.new.views_histogram(doi)
end
end

def cached_downloads_histogram_response(doi)
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_downloads_histogram_response/#{doi}", expires_in: 24.hours) do
EventsQuery.new.downloads_histogram(doi)
end
else
EventsQuery.new.downloads_histogram(doi)
end
end
end

module ClassMethods
Expand Down
11 changes: 6 additions & 5 deletions app/queries/events_query.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class EventsQuery
include Cacheable
include Facetable

ACTIVE_RELATION_TYPES = [
Expand Down Expand Up @@ -29,7 +30,7 @@ def doi_citations(doi)
def citations(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_citations(item) }
{ id: item, count: cached_doi_citations_response(item) }
end
end

Expand All @@ -52,7 +53,7 @@ def doi_views(doi)
def views(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_views(item) }
{ id: item, count: cached_doi_views_response(item) }
end
end

Expand All @@ -74,7 +75,7 @@ def doi_downloads(doi)
def downloads(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_downloads(item) }
{ id: item, count: cached_doi_downloads_response(item) }
end
end

Expand All @@ -90,8 +91,8 @@ def usage(doi)
return {} unless doi.present?
doi.downcase.split(",").map do |item|
pid = Event.new.normalize_doi(item)
requests = EventsQuery.new.doi_downloads(item)
investigations = EventsQuery.new.doi_views(item)
requests = cached_doi_downloads_response(item)
investigations = cached_doi_views_response(item)
{ id: pid,
title: pid,
relationTypes: [
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/clients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"attributes" => {
"symbol" => provider.symbol + ".IMPERIAL",
"name" => "Imperial College",
"systemEmail" => "[email protected]",
"contactEmail" => "[email protected]",
"clientType" => "repository"
},
"relationships": {
Expand Down

0 comments on commit be842b9

Please sign in to comment.