Class: Trackguard::Admin::AnalyticsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/trackguard/admin/analytics_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject

rubocop:disable Metrics/AbcSize



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/trackguard/admin/analytics_controller.rb', line 7

def show
  @total_today  = PageView.today.count
  @total_week   = PageView.this_week.count
  @total_month  = PageView.this_month.count

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

  @recent = visitor_filtered(
    time_scope(PageView.order(created_at: :desc).limit(20).includes(visitor: :whitelisted_ip))
  )

  render json: {
    totals: { today: @total_today, week: @total_week, month: @total_month },
    top_pages: @top_pages,
    top_referrers: @top_referrers,
    top_sources: @top_sources,
    recent: @recent.map do |pv|
      {
        path: pv.path,
        ip: pv.visitor&.ip,
        flagged_at: pv.visitor.flagged_at,
        flagged_by: pv.visitor.flagged_by,
        whitelisted: pv.visitor.whitelisted_ip&.active? || false,
        user_agent: pv.user_agent,
        session_id: pv.session_id,
        trace_id: pv.trace_id,
        referer: pv.referer,
        source: pv.source,
        created_at: pv.created_at
      }
    end
  }
end