Class: Sufia::UserStatImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sufia/models/stats/user_stat_importer.rb

Defined Under Namespace

Classes: UserRecord

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UserStatImporter

Returns a new instance of UserStatImporter.



5
6
7
8
9
10
# File 'lib/sufia/models/stats/user_stat_importer.rb', line 5

def initialize(options = {})
  @verbose = options[:verbose]
  @logging = options[:logging]
  @delay_secs = options[:delay_secs].to_f
  @number_of_retries = options[:number_of_retries].to_i
end

Instance Method Details

#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

#sorted_usersObject

Returns an array of users sorted by the date of their last stats update. Users that have not been recently updated will be at the top of the array.



39
40
41
42
43
44
45
# File 'lib/sufia/models/stats/user_stat_importer.rb', line 39

def sorted_users
  users = []
  ::User.find_each do |user|
    users.push(UserRecord.new(user.id, user.user_key, date_since_last_cache(user)))
  end
  users.sort { |a, b| a.last_stats_update <=> b.last_stats_update }
end