From 0b1f2efe3eb272a84917d233dda3769cd19965c3 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 8 Jan 2020 12:56:25 +0100 Subject: [PATCH 1/2] support contact_email in clients api --- app/models/client.rb | 1 + spec/requests/clients_spec.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/client.rb b/app/models/client.rb index e77e91e4f..ecf0b43ee 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -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 diff --git a/spec/requests/clients_spec.rb b/spec/requests/clients_spec.rb index cc7eaa070..0f828f41e 100644 --- a/spec/requests/clients_spec.rb +++ b/spec/requests/clients_spec.rb @@ -10,7 +10,7 @@ "attributes" => { "symbol" => provider.symbol + ".IMPERIAL", "name" => "Imperial College", - "systemEmail" => "bob@example.com", + "contactEmail" => "bob@example.com", "clientType" => "repository" }, "relationships": { From b4788f8a1f2fa2edbac75d9042fd426494c4d4c1 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Thu, 9 Jan 2020 12:57:13 +0100 Subject: [PATCH 2/2] add caching for metrics queries --- app/controllers/events_controller.rb | 6 +-- app/models/concerns/cacheable.rb | 60 ++++++++++++++++++++++++++++ app/queries/events_query.rb | 11 ++--- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index e6881e75f..eacfbb58c 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -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 diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index d48f9cb1e..4f390d94c 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -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 diff --git a/app/queries/events_query.rb b/app/queries/events_query.rb index 0018fb7b4..b720e5c31 100644 --- a/app/queries/events_query.rb +++ b/app/queries/events_query.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class EventsQuery + include Cacheable include Facetable ACTIVE_RELATION_TYPES = [ @@ -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 @@ -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 @@ -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 @@ -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: [