From a146474fad6f565591d7939aeed7896c36fcbe2a Mon Sep 17 00:00:00 2001 From: Ben Pennell Date: Wed, 6 Nov 2024 16:09:34 -0500 Subject: [PATCH] Change work stats to pulling from local database --- .../hyrax/stats_controller_override.rb | 34 ++++++------------- .../hyrax/stats_controller_spec.rb | 16 ++++++--- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/overrides/controllers/hyrax/stats_controller_override.rb b/app/overrides/controllers/hyrax/stats_controller_override.rb index 2c099224b..86ede763b 100644 --- a/app/overrides/controllers/hyrax/stats_controller_override.rb +++ b/app/overrides/controllers/hyrax/stats_controller_override.rb @@ -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 diff --git a/spec/controllers/hyrax/stats_controller_spec.rb b/spec/controllers/hyrax/stats_controller_spec.rb index 8cfff7b8d..2faa439db 100644 --- a/spec/controllers/hyrax/stats_controller_spec.rb +++ b/spec/controllers/hyrax/stats_controller_spec.rb @@ -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) @@ -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