Skip to content

Commit

Permalink
Change work stats to pulling from local database
Browse files Browse the repository at this point in the history
  • Loading branch information
bbpennel committed Nov 6, 2024
1 parent 36bad63 commit a146474
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
34 changes: 10 additions & 24 deletions app/overrides/controllers/hyrax/stats_controller_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,21 @@
def work
# [hyc-override] different parameters and switched to using monthly instead of daily events
@document = ::SolrDocument.find(params[:id])
# [hyc-override] Execute all of the matomo requests in parallel
# [hyc-override] Execute all of the stats requests in parallel
threads = []
threads << Thread.new do
@pageviews = Hyrax::Analytics.monthly_events_for_id(@document.id, 'work-view')
end
# [hyc-override] Pull DownloadIR stats from the first 100 filesets in the work
work = ActiveFedora::Base.find(params[:id])
fileset_ids = work.members.first(100).map(&:id)
combined_results = nil
mutex = Mutex.new

fileset_ids.each do |fileset_id|
threads << Thread.new do
events = Hyrax::Analytics.monthly_events_for_id(fileset_id, 'DownloadIR')
mutex.synchronize do
if combined_results.nil?
combined_results = events
else
# Merge incoming event counts into the combined result.
# Results values are lists containing 2 elements, the date and the event count.
events.results.each_with_index do |entry, index|
next if entry.nil?
combined_results.results[index][1] += entry[1]
end
end
end
end
# [hyc-override] Retrieve download stats from local database
threads << Thread.new do
@downloads = work_stats_as_events(@document.id)
end
threads.each(&:join)
@downloads = combined_results
end

def work_stats_as_events(work_id)
# retrieve the download stats from the local database and turn it into a hash of date => nb_events
HycDownloadStat.with_work_id(work_id, Hyrax.config.analytics_start_date, Time.zone.today)
.to_h { |stat| [stat.date.strftime('%Y-%m'), [{ 'nb_events' => stat.download_count }]] }
end
end
16 changes: 12 additions & 4 deletions spec/controllers/hyrax/stats_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
end
spec_fileset_ids = work.members.map(&:id)

spec_fileset_ids.each_with_index do |fileset_id, index|
expect(Hyrax::Analytics).to receive(:api_params).with('Events.getName', 'month', anything, { flat: 1,
label: "#{fileset_id} - DownloadIR"}).and_return(spec_downloads_hash)
end
generate_hyc_stats_for_range(work.id, spec_fileset_ids[0], spec_downloads)
generate_hyc_stats_for_range(work.id, spec_fileset_ids[1], spec_downloads)
# spec_fileset_ids.each_with_index do |fileset_id, index|
# expect(Hyrax::Analytics).to receive(:api_params).with('Events.getName', 'month', anything, { flat: 1,
# label: "#{fileset_id} - DownloadIR"}).and_return(spec_downloads_hash)
# end

expect(Hyrax::Analytics).to receive(:api_params).with('Events.getName', 'month', anything, { flat: 1,
label: "#{work.id} - work-view"}).and_return(spec_page_views_hash)
Expand All @@ -54,4 +56,10 @@
end
end

def generate_hyc_stats_for_range(work_id, fileset_id, expected_downloads)
expected_downloads.each do |pair|
FactoryBot.create(:hyc_download_stat, work_id: work_id, fileset_id: fileset_id, date: pair[0], download_count: pair[1])
end
end

end

0 comments on commit a146474

Please sign in to comment.