Method: Sufia::UserStatImporter#import

Defined in:
lib/sufia/models/stats/user_stat_importer.rb

#importObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sufia/models/stats/user_stat_importer.rb', line 12

def import
  log_message('Begin import of User stats.')
  sorted_users.each do |user|
    start_date = date_since_last_cache(user)

    # this user has already been processed today continue without delay
    next if start_date.to_date >= Date.today

    stats = {}
    file_ids_for_user(user).each do |file_id|
      view_stats = rescue_and_retry("Retried FileViewStat on #{user} for file #{file_id} too many times.") { FileViewStat.statistics(file_id, start_date, user.id) }
      stats = tally_results(view_stats, :views, stats) unless view_stats.blank?
      delay

      dl_stats = rescue_and_retry("Retried FileDownloadStat on #{user} for file #{file_id} too many times.") { FileDownloadStat.statistics(file_id, start_date, user.id) }
      stats = tally_results(dl_stats, :downloads, stats) unless dl_stats.blank?
      delay
    end

    create_or_update_user_stats(stats, user)
  end
  log_message('User stats import complete.')
end