From 3bdba554aa8b4a5b72fe82997b751018f38c6d78 Mon Sep 17 00:00:00 2001 From: James Van Mil Date: Thu, 21 Dec 2017 11:12:07 -0500 Subject: [PATCH 1/3] (poorly) handle google analytics error --- .rubocop.yml | 1 + app/services/hyrax/user_stat_importer.rb | 28 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 app/services/hyrax/user_stat_importer.rb diff --git a/.rubocop.yml b/.rubocop.yml index 4d0d05840..2ce24c292 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -13,6 +13,7 @@ AllCops: - 'app/services/report.rb' - 'app/services/work_loader.rb' - 'app/services/hyrax/actor_factory.rb' + - 'app/services/hyrax/user_stat_importer.rb' - 'spec/services/hyrax/actor_factory_spec.rb' - 'db/**/*' - 'lib/tasks/batch.rake' diff --git a/app/services/hyrax/user_stat_importer.rb b/app/services/hyrax/user_stat_importer.rb new file mode 100644 index 000000000..bc213abce --- /dev/null +++ b/app/services/hyrax/user_stat_importer.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +require Hyrax::Engine.root.join('app/services/hyrax/user_stat_importer.rb') +module Hyrax + class UserStatImporter + private + + def process_files(stats, user, start_date) + file_ids_for_user(user).each do |file_id| + file = ::FileSet.find(file_id) + view_stats = rescue_and_retry("Retried FileViewStat on #{user} for file #{file_id} too many times.") { FileViewStat.statistics(file, start_date, user.id) } + stats = tally_results(view_stats, :views, stats) unless (view_stats.blank? || view_stats) + delay + dl_stats = rescue_and_retry("Retried FileDownloadStat on #{user} for file #{file_id} too many times.") { FileDownloadStat.statistics(file, start_date, user.id) } + stats = tally_results(dl_stats, :downloads, stats) unless (dl_stats.blank? || dl_stats) + delay + end + end + + def process_works(stats, user, start_date) + work_ids_for_user(user).each do |work_id| + work = Hyrax::WorkRelation.new.find(work_id) + work_stats = rescue_and_retry("Retried WorkViewStat on #{user} for work #{work_id} too many times.") { WorkViewStat.statistics(work, start_date, user.id) } + stats = tally_results(work_stats, :work_views, stats) unless (work_stats.blank? || work_stats) + delay + end + end + end +end From f7726661a025e2adfd306fca710241db29b6d48f Mon Sep 17 00:00:00 2001 From: James Van Mil Date: Fri, 22 Dec 2017 10:31:07 -0500 Subject: [PATCH 2/3] override stats rake task to add :delay_secs option --- lib/tasks/stats_tasks.rake | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/tasks/stats_tasks.rake diff --git a/lib/tasks/stats_tasks.rake b/lib/tasks/stats_tasks.rake new file mode 100644 index 000000000..ff19331f9 --- /dev/null +++ b/lib/tasks/stats_tasks.rake @@ -0,0 +1,10 @@ +# frozen_string_literal: true +namespace :hyrax do + namespace :stats do + desc "Cache work view, file view & file download stats for all users" + task user_stats: :environment do + importer = Hyrax::UserStatImporter.new(verbose: true, logging: true, delay_secs: 1) + importer.import + end + end +end From dd2f780d478d9f7f473139fd92a7f349e6e9389a Mon Sep 17 00:00:00 2001 From: Thomas Scherz Date: Wed, 14 Feb 2018 14:22:00 -0500 Subject: [PATCH 3/3] Adds required includes to user_stat_importer. --- app/services/hyrax/user_stat_importer.rb | 25 +++--------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/app/services/hyrax/user_stat_importer.rb b/app/services/hyrax/user_stat_importer.rb index bc213abce..919d41461 100644 --- a/app/services/hyrax/user_stat_importer.rb +++ b/app/services/hyrax/user_stat_importer.rb @@ -2,27 +2,8 @@ require Hyrax::Engine.root.join('app/services/hyrax/user_stat_importer.rb') module Hyrax class UserStatImporter - private - - def process_files(stats, user, start_date) - file_ids_for_user(user).each do |file_id| - file = ::FileSet.find(file_id) - view_stats = rescue_and_retry("Retried FileViewStat on #{user} for file #{file_id} too many times.") { FileViewStat.statistics(file, start_date, user.id) } - stats = tally_results(view_stats, :views, stats) unless (view_stats.blank? || view_stats) - delay - dl_stats = rescue_and_retry("Retried FileDownloadStat on #{user} for file #{file_id} too many times.") { FileDownloadStat.statistics(file, start_date, user.id) } - stats = tally_results(dl_stats, :downloads, stats) unless (dl_stats.blank? || dl_stats) - delay - end - end - - def process_works(stats, user, start_date) - work_ids_for_user(user).each do |work_id| - work = Hyrax::WorkRelation.new.find(work_id) - work_stats = rescue_and_retry("Retried WorkViewStat on #{user} for work #{work_id} too many times.") { WorkViewStat.statistics(work, start_date, user.id) } - stats = tally_results(work_stats, :work_views, stats) unless (work_stats.blank? || work_stats) - delay - end - end + require 'legato' + require 'hyrax/pageview' + require 'hyrax/download' end end