Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 11, 2020
2 parents bfe0d77 + 0a2ea0b commit 819dca8
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 61 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
105 changes: 52 additions & 53 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -140,7 +133,7 @@ def organizations
accountWebsite
region
focusArea
organisationType
sector
accountType
generalContactEmail
groupEmail
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -252,6 +245,9 @@ def repositories
accountDescription
accountWebsite
generalContactEmail
serviceContactEmail
serviceContactGivenName
serviceContactFamilyName
created
deleted
doisCountCurrentYear
Expand All @@ -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
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

0 comments on commit 819dca8

Please sign in to comment.