Class: SupplejackApi::SiteActivity

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, SupplejackApi::Sortable::Query
Defined in:
app/models/supplejack_api/site_activity.rb

Constant Summary collapse

IMPLICIT_FIELDS =
%w(_type _id created_at updated_at).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activitiesObject



51
52
53
# File 'app/models/supplejack_api/site_activity.rb', line 51

def self.activities
  %w(date search user_sets records source_clicks total) - IMPLICIT_FIELDS
end

.generate_activity(time = Time.now) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/supplejack_api/site_activity.rb', line 28

def self.generate_activity(time = Time.now)
  site_activity_date = time.to_date
  user_activities = SupplejackApi::UserActivity.gt(created_at: time - 12.hours).lte(created_at: time)

  attributes = { user_sets: 0, search: 0, records: 0 }

  user_activities.each do |user_activity|
    [:user_sets, :search, :records].each do |field|
      attributes[field] += user_activity.send(field)['total'] if user_activity.send(field)
    end
  end

  # The date stored is yesterday's date since the activity corresponds to the day before.
  attributes[:date] = site_activity_date

  site_activity = new(attributes)
  site_activity.source_clicks = SupplejackApi::SourceActivity.get_source_clicks || 0
  SupplejackApi::SourceActivity.reset
  site_activity.calculate_total
  site_activity.save
  site_activity
end

Instance Method Details

#calculate_totalObject



55
56
57
# File 'app/models/supplejack_api/site_activity.rb', line 55

def calculate_total
  self.total = user_sets + records + search + source_clicks
end