Skip to content

Commit

Permalink
adds a bunch of code thats been in prod a long time
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorcy committed Sep 12, 2024
1 parent d88fbe1 commit 0551610
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ bin/spring
# env files
.env
coverage/

trove-lock
trove.pid
.DS_Store
67 changes: 67 additions & 0 deletions app/services/hyrax/statistics/depositors/summary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Gather information about the depositors who have contributed to the repository
module Hyrax
module Statistics
module Depositors
class Summary
include Blacklight::SearchHelper

# @api public
# @param [Time] start_date optionally specify the start date to gather the stats from
# @param [Time] end_date optionally specify the end date to gather the stats from
# @return [Array<Hash>] With keys of: :key, :deposits, and :user
def self.depositors(start_date:, end_date:)
new(start_date, end_date).depositors
end

# @param [Time] start_date optionally specify the start date to gather the stats from
# @param [Time] end_date optionally specify the end date to gather the stats from
def initialize(start_date, end_date)
@start_dt = start_date
@end_dt = end_date
end

attr_accessor :start_dt, :end_dt

def depositors
# step through the array by twos to get each pair
results.map do |key, deposits|
user = ::User.find_by_user_key(key)
raise "Unable to find user '#{key}'\nResults was: #{results.inspect}" unless user
{ key: key, deposits: deposits, user: user }
end
end

private

delegate :blacklight_config, to: CatalogController
delegate :depositor_field, to: DepositSearchBuilder

# results come from Solr in an array where the first item is the user and
# the second item is the count
# [ abc123, 55, ccczzz, 205 ]
# @return [#each] an enumerable object of tuples (user and count)
def results
facet_results = repository.search(query)
facet_results.facet_fields[depositor_field].each_slice(2)
end

def search_builder
DepositSearchBuilder.new([:include_depositor_facet, :filter_models], self)
end

# TODO: This can probably be pushed into the DepositSearchBuilder
def query
search_builder.merge(q: date_query, fq: 'displays_in_sim:trove').query
end

def date_query
query_service.build_date_query(start_dt, end_dt) if start_dt.present?
end

def query_service
Hyrax::Statistics::QueryService.new
end
end
end
end
end
73 changes: 73 additions & 0 deletions app/services/hyrax/statistics/term_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Hyrax
module Statistics
# An abstract class for running a query against the Solr terms component
# you must implement `index_key` in the concrete class.
#
# WARNING: you must use a term that isn't parsed (i.e. use _sim instead of _tesim)
class TermQuery
# @api public
# @param [Integer] limit - Limit the number of responses
# @return [Array<Hyrax::Statistics::TermQuery::Result>]
def self.query(limit: 5)
new(limit).query
end

def initialize(limit = 5)
@limit = limit
end

def query
term = index_key
# Grab JSON response (looks like {"terms": {"depositor_tesim": {"mjg36": 3}}} for depositor)
json = solr_connection.get 'terms', params: { 'terms.fl' => term,
'terms.sort' => 'count',
'terms.limit' => @limit,
fq: 'displays_in_sim:trove',
wt: 'json',
'json.nl' => 'map',
omitHeader: 'true' }
unless json
Rails.logger.error "Unable to reach TermsComponent via Solr connection. Is it enabled in your solr config?"
return []
end

Result.build(json['terms'][term])
end

class Result
# @param [Array<Array>] rows list of of tuples (label, value)
def self.build(rows)
rows.map { |row| Result.new(*row) }
end

def initialize(label, value)
@data = { label: label, data: value }
end

def label
@data[:label]
end

def value
@data[:data]
end

# This enables hash equivalence
def ==(other)
other == @data
end

# Allows us to create a Flot charts pie-graph
def as_json(opts)
@data.as_json(opts)
end
end

private

def solr_connection
ActiveFedora::SolrService.instance.conn
end
end
end
end
1 change: 1 addition & 0 deletions config/initializers/ssl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

0 comments on commit 0551610

Please sign in to comment.