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/controllers/export_controller.rb b/app/controllers/export_controller.rb index 5dcea1c49..7ca3866d5 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -1,6 +1,8 @@ class ExportController < ApplicationController include ActionController::MimeResponds + EXPORT_DATE_FORMAT = "%d/%m/%YT%H:%M:%S.%3NUTC%:z" + before_action :authenticate_user_with_basic_auth! def contacts @@ -38,56 +40,47 @@ def contacts csv = headers.to_csv + # Use a hashmap for the contacts to avoid duplicated + contacts = Hash.new + + add_contact = Proc.new { |contacts, email, id, firstname, lastname, type| + if email + unless contacts.has_key?(email) + contacts[email] = { + 'fabricaAccountId' => id, + 'firstName' => firstname, + 'lastName' => lastname, + } + end + + if contacts[email].has_key?('type') + contacts[email]['type'] += ";" + type + else + contacts[email]['type'] = type + end + end + } + providers.each do |provider| + add_contact.call(contacts, provider.technical_contact_email, provider.symbol, provider.technical_contact_given_name, provider.technical_contact_family_name, 'technical') + add_contact.call(contacts, provider.secondary_technical_contact_email, provider.symbol, provider.secondary_technical_contact_given_name, provider.secondary_technical_contact_family_name, 'secondaryTechnical') + add_contact.call(contacts, provider.service_contact_email, provider.symbol, provider.service_contact_given_name, provider.service_contact_family_name, 'service') + add_contact.call(contacts, provider.secondary_service_contact_email, provider.symbol, provider.secondary_service_contact_given_name, provider.secondary_service_contact_family_name, 'secondaryService') + add_contact.call(contacts, provider.voting_contact_email, provider.symbol, provider.voting_contact_given_name, provider.voting_contact_family_name, 'voting') + add_contact.call(contacts, provider.billing_contact_email, provider.symbol, provider.billing_contact_given_name, provider.billing_contact_family_name, 'billing') + add_contact.call(contacts, provider.secondary_billing_contact_email, provider.symbol, provider.secondary_billing_contact_given_name, provider.secondary_billing_contact_family_name, 'secondaryBilling') + + end + + contacts.each do |email, contact| + csv += CSV.generate_line [ - provider.symbol, - provider.technical_contact_email, - provider.technical_contact_given_name, - provider.technical_contact_family_name, - 'technical' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.secondary_technical_contact_email, - provider.secondary_technical_contact_given_name, - provider.secondary_technical_contact_family_name, - 'secondaryTechnical' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.service_contact_email, - provider.service_contact_given_name, - provider.service_contact_family_name, - 'service' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.secondary_service_contact_email, - provider.secondary_service_contact_given_name, - provider.secondary_service_contact_family_name, - 'secondaryService' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.voting_contact_email, - provider.voting_contact_given_name, - provider.voting_contact_family_name, - 'voting' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.billing_contact_email, - provider.billing_contact_given_name, - provider.billing_contact_family_name, - 'billing' - ] - csv += CSV.generate_line [ - provider.symbol, - provider.secondary_billing_contact_email, - provider.secondary_billing_contact_given_name, - provider.secondary_billing_contact_family_name, - 'secondaryBilling' + contact['fabricaAccountId'], + email, + contact['firstName'], + contact['lastName'], + contact['type'], ] end @@ -140,7 +133,7 @@ def organizations accountWebsite region focusArea - organisationType + sector accountType generalContactEmail groupEmail @@ -171,7 +164,7 @@ def organizations accountWebsite: provider.website, region: provider.region_human_name, focusArea: provider.focus_area, - organizationType: provider.organization_type, + sector: provider.organization_type, accountType: provider.member_type_label, generalContactEmail: provider.system_email, groupEmail: provider.group_email, @@ -184,8 +177,8 @@ def organizations billingCountry: provider.billing_country, twitter: provider.twitter_handle, rorId: provider.ror_id, - created: provider.created, - deleted: provider.deleted_at + created: provider.created.present? ? provider.created.strftime(EXPORT_DATE_FORMAT) : nil, + deleted: provider.deleted_at.present? ? provider.deleted_at.strftime(EXPORT_DATE_FORMAT) : nil, }.values csv += CSV.generate_line row @@ -252,6 +245,9 @@ def repositories accountDescription accountWebsite generalContactEmail + serviceContactEmail + serviceContactGivenName + serviceContactFamilyName created deleted doisCountCurrentYear @@ -273,8 +269,11 @@ def repositories accountDescription: client.description, accountWebsite: client.url, generalContactEmail: client.system_email, - created: client.created, - deleted: client.deleted_at, + serviceContactEmail: client.service_contact_email, + serviceContactGivenName: client.service_contact_given_name, + serviceContactFamilyName: client.service_contact_family_name, + created: client.created.present? ? client.created.strftime(EXPORT_DATE_FORMAT) : nil, + deleted: client.deleted_at.present? ? client.deleted_at.strftime(EXPORT_DATE_FORMAT) : nil, doisCountCurrentYear: client_totals[client_id] ? client_totals[client_id]["this_year"] : nil, doisCountPreviousYear: client_totals[client_id] ? client_totals[client_id]["last_year"] : nil, doisCountTotal: client_totals[client_id] ? client_totals[client_id]["count"] : 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: [