Skip to content

Commit

Permalink
added elasticsearch tests. #205
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 19, 2019
1 parent 4b0ac81 commit b61a63b
Show file tree
Hide file tree
Showing 21 changed files with 369 additions and 366 deletions.
8 changes: 1 addition & 7 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ module Facetable
"apac" => "Asia Pacific",
"emea" => "EMEA" }

def client_year_facet(params, collection)
[{ id: params[:year],
title: params[:year],
count: collection.where('YEAR(datacentre.created) = ?', params[:year]).count }]
end

def facet_by_year(arr)
arr.map do |hsh|
{ "id" => hsh["key_as_string"][0..3],
Expand Down Expand Up @@ -94,7 +88,7 @@ def facet_by_provider(arr)
# generate hash with id and name for each provider in facet

ids = arr.map { |hsh| hsh["key"] }.join(",")
providers = Provider.find_by_ids(ids).results.reduce({}) do |sum, p|
providers = Provider.find_by_ids(ids, size: 1000).results.reduce({}) do |sum, p|
sum[p.symbol.downcase] = p.name
sum
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def index
providers = providers
.sort { |a, b| b[1] <=> a[1] }
.reduce([]) do |sum, i|
if provider = cached_providers.find { |m| m.id == i[0] }
if provider = Provider.where(symbol: i[0]).first
sum << { id: provider.symbol.downcase, title: provider.name, count: i[1] }
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def index
providers = providers
.sort { |a, b| b[1] <=> a[1] }
.reduce([]) do |sum, i|
if provider = cached_providers.find { |m| m.id == i[0] }
if provider = Provider.where(symbol: i[0]).first
sum << { id: provider.symbol.downcase, title: provider.name, count: i[1] }
end

Expand Down
6 changes: 3 additions & 3 deletions app/jobs/transfer_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def perform(doi_id, options={})

doi.__elasticsearch__.index_document

logger.info "[MySQL] Transferred DOI #{doi.doi}."
logger.info "[Transfer] Transferred DOI #{doi.doi}."
elsif doi.present?
logger.info "[MySQL] Error transferring DOI " + doi_id + ": no target client"
logger.info "[Transfer] Error transferring DOI " + doi_id + ": no target client"
else
logger.info "[MySQL] Error transferring DOI " + doi_id + ": not found"
logger.info "[Transfer] Error transferring DOI " + doi_id + ": not found"
end
end
end
6 changes: 3 additions & 3 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def provider_id
end

def provider_id=(value)
r = cached_provider_response(value)
r = Provider.where(symbol: value).first
return nil unless r.present?

write_attribute(:allocator, r.id)
Expand Down Expand Up @@ -182,7 +182,7 @@ def target_id=(value)

target = c.records.first

Doi.transfer(from_date: "2011-01-01", client_id: id, target_id: target.id)
Doi.transfer(client_id: symbol.downcase, target_id: target.id)
end

def index_all_dois
Expand All @@ -207,7 +207,7 @@ def password_input=(value)

# backwards compatibility
def member
cached_member_response(provider_id) if provider_id.present?
Provider.where(symbol: provider_id).first if provider_id.present?
end

def year
Expand Down
2 changes: 1 addition & 1 deletion app/models/client_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def client_id

# workaround for non-standard database column names and association
def client_id=(value)
r = cached_client_response(value)
r = ::Client.where(symbol: value).first
fail ActiveRecord::RecordNotFound unless r.present?

self.datacentre = r.id
Expand Down
111 changes: 1 addition & 110 deletions app/models/concerns/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@ module Cacheable
extend ActiveSupport::Concern

included do
def cached_doi_count(options={})
Rails.cache.fetch("cached_doi_count/#{id}", expires_in: 24.hours, force: options[:force]) do
return [] if Rails.env.test?

if self.class.name == "Provider" && symbol != "ADMIN"
collection = Doi.joins(:client).where("datacentre.allocator = ?", id)
elsif self.class.name == "Client"
collection = Doi.where(datacentre: id)
else
collection = Doi
end

years = collection.order("YEAR(dataset.created)").group("YEAR(dataset.created)").count
years = years.map { |k,v| { id: k, title: k, count: v } }
end
end

def cached_metadata_count(options={})
Rails.cache.fetch("cached_metadata_count/#{id}", expires_in: 6.hours, force: options[:force]) do
return [] if Rails.env.test?
Expand Down Expand Up @@ -50,16 +33,6 @@ def cached_media_count(options={})
end
end

def fetch_cached_metadata_version
if updated.present? && Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_metadata_version/#{doi}-#{updated.iso8601}") do
current_metadata ? current_metadata.metadata_version : 0
end
else
current_metadata ? current_metadata.metadata_version : 0
end
end

def cached_prefixes_totals params={}
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("cached_prefixes_totals/#{params}", expires_in: 24.hours) do
Expand Down Expand Up @@ -90,49 +63,16 @@ def cached_clients_totals params={}
end
end

def cached_client_response(id, options={})
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("client_response/#{id}", expires_in: 1.day) do
Client.where(symbol: id).first
end
else
Client.where(symbol: id).first
end
end

def cached_prefix_response(prefix, options={})
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("prefix_response/#{prefix}", expires_in: 7.days) do
Rails.cache.fetch("prefix_response/#{prefix}", expires_in: 24.hours) do
Prefix.where(prefix: prefix).first
end
else
Prefix.where(prefix: prefix).first
end
end

def cached_provider_response(id, options={})
if Rails.application.config.action_controller.perform_caching
Rails.cache.fetch("provider_response/#{id}", expires_in: 1.day) do
Provider.where(symbol: id).first
end
else
Provider.where(symbol: id).first
end
end

def cached_providers
Rails.cache.fetch("providers", expires_in: 1.day) do
Provider.all.select(:id, :symbol, :name, :created)
end
end

def cached_member_response(id)
Rails.cache.fetch("member_response/#{id}", expires_in: 12.hours) do
member = Member.where(id: id)
member.present? ? member[:data] : nil
end
end

def cached_resource_type_response(id)
Rails.cache.fetch("resource_type_response/#{id}", expires_in: 1.month) do
resource_type = ResourceType.where(id: id)
Expand All @@ -152,12 +92,6 @@ def cached_repository_response(id, options={})
end

module ClassMethods
def cached_providers
Rails.cache.fetch("providers", expires_in: 1.day) do
Provider.all.select(:id, :symbol, :name, :updated)
end
end

def cached_metadata_count
Rails.cache.fetch("cached_metadata_count", expires_in: 6.hours) do
return [] if Rails.env.test?
Expand All @@ -175,48 +109,5 @@ def cached_media_count
years = years.map { |k,v| { id: k, title: k, count: v } }
end
end

# def cached_clients
# Rails.cache.fetch("clients", expires_in: 1.day) do
# Client.all.select(:id, :symbol, :name, :updated)
# end
# end

def cached_providers_response(options={})
Rails.cache.fetch("provider_response", expires_in: 1.day) do
Providers.where(options)
end
end

def cached_provider_response(symbol)
Rails.cache.fetch("provider_response/#{symbol}", expires_in: 1.day) do
Provider.where(symbol: symbol).first
end
end

def cached_datasets_clients_join(options={})
Rails.cache.fetch("clients", expires_in: 1.day) do
Dataset.joins(:clients).where("client.symbol" => "dataset.allocator")
end
end

def cached_clients_response(options={})
Rails.cache.fetch("clients_response", expires_in: 1.day) do
# collection = cached_datasets_clients_joins
collection.each do |line|
dc = Client.find(line[:datacentre])
line[:client_id] = dc.uid
line[:client_name] = dc.name
end

collection.map{|doi| { id: doi[:id], client_id: doi[:client_id], name: doi[:client_name] }}.group_by { |d| d[:client_id] }.map{ |k, v| { id: k, title: v.first[:name], count: v.count} }
end
end

def cached_client_response(id, options={})
Rails.cache.fetch("client_response/#{id}", expires_in: 1.day) do
Client.where(symbol: id).first
end
end
end
end
1 change: 1 addition & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def find_by_ids(ids, options={})

def query(query, options={})
aggregations = query_aggregations
options[:page] ||= { size: 25, number: 1 }

# enable cursor-based pagination for DOIs
if self.name == "Doi" && options.dig(:page, :cursor).present?
Expand Down
Loading

0 comments on commit b61a63b

Please sign in to comment.