Class: Trackguard::AnalyticsQuery

Inherits:
ApplicationService show all
Defined in:
app/services/trackguard/analytics_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

call

Constructor Details

#initialize(scope:, time_scope:, limit:) ⇒ AnalyticsQuery

Returns a new instance of AnalyticsQuery.



5
6
7
8
9
# File 'app/services/trackguard/analytics_query.rb', line 5

def initialize(scope:, time_scope:, limit:)
  @scope      = scope
  @time_scope = time_scope
  @limit      = limit
end

Instance Attribute Details

#recentObject (readonly)

Returns the value of attribute recent.



3
4
5
# File 'app/services/trackguard/analytics_query.rb', line 3

def recent
  @recent
end

#top_pagesObject (readonly)

Returns the value of attribute top_pages.



3
4
5
# File 'app/services/trackguard/analytics_query.rb', line 3

def top_pages
  @top_pages
end

#top_referrersObject (readonly)

Returns the value of attribute top_referrers.



3
4
5
# File 'app/services/trackguard/analytics_query.rb', line 3

def top_referrers
  @top_referrers
end

#top_sourcesObject (readonly)

Returns the value of attribute top_sources.



3
4
5
# File 'app/services/trackguard/analytics_query.rb', line 3

def top_sources
  @top_sources
end

#totalsObject (readonly)

Returns the value of attribute totals.



3
4
5
# File 'app/services/trackguard/analytics_query.rb', line 3

def totals
  @totals
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/trackguard/analytics_query.rb', line 11

def call
  @totals = {
    today: @scope.today.count,
    week: @scope.this_week.count,
    month: @scope.this_month.count
  }

  @top_pages     = @time_scope.group(:path).order("count_all DESC").limit(@limit).count
  @top_referrers = @time_scope.with_referrer.group(:referer).order("count_all DESC").limit(@limit).count
  @top_sources   = @time_scope.with_source.group(:source).order("count_all DESC").limit(@limit).count

  @recent = @scope.order(created_at: :desc).limit(20).includes(visitor: :whitelisted_ip)

  self
end