Class: Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/statistics.rb

Constant Summary collapse

EU_COUNTRIES =
%w[
  AT
  BE
  BG
  CY
  CZ
  DE
  DK
  EE
  ES
  FI
  FR
  GR
  HR
  HU
  IE
  IT
  LT
  LU
  LV
  MT
  NL
  PL
  PT
  RO
  SE
  SI
  SK
]

Class Method Summary collapse

Class Method Details

.active_usersObject



34
35
36
37
38
39
40
# File 'lib/statistics.rb', line 34

def self.active_users
  {
    last_day: valid_users.where("last_seen_at > ?", 1.day.ago).count,
    "7_days": valid_users.where("last_seen_at > ?", 7.days.ago).count,
    "30_days": valid_users.where("last_seen_at > ?", 30.days.ago).count,
  }
end

.eu_visitorsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/statistics.rb', line 114

def self.eu_visitors
  periods = [[1.day.ago, :last_day], [7.days.ago, :"7_days"], [30.days.ago, :"30_days"]]

  periods
    .map do |(period, key)|
      logged_in_page_views =
        ApplicationRequest.request_type_count_for_period(:page_view_logged_in_browser, period)
      anon_page_views =
        ApplicationRequest.request_type_count_for_period(:page_view_anon_browser, period)

      all_logged_in_visitors = logged_in_visitors_count(period)
      eu_logged_in_visitors = eu_logged_in_visitors_count(period)

      next key, 0 if all_logged_in_visitors == 0 || eu_logged_in_visitors == 0
      next key, eu_logged_in_visitors if logged_in_page_views == 0

      avg_logged_in_page_view_per_user = logged_in_page_views / all_logged_in_visitors.to_f

      eu_logged_in_visitors_ratio = eu_logged_in_visitors / all_logged_in_visitors.to_f

      eu_anon_visitors =
        ((anon_page_views / avg_logged_in_page_view_per_user) * eu_logged_in_visitors_ratio).round
      eu_visitors = eu_logged_in_visitors + eu_anon_visitors
      [key, eu_visitors]
    end
    .to_h
end

.likesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/statistics.rb', line 42

def self.likes
  likes = UserAction.where(action_type: UserAction::LIKE)

  {
    last_day: likes.where("created_at > ?", 1.day.ago).count,
    "7_days": likes.where("created_at > ?", 7.days.ago).count,
    "30_days": likes.where("created_at > ?", 30.days.ago).count,
    count: likes.count,
  }
end

.participating_usersObject



82
83
84
85
86
87
88
# File 'lib/statistics.rb', line 82

def self.participating_users
  {
    last_day: participating_users_count(1.day.ago),
    "7_days": participating_users_count(7.days.ago),
    "30_days": participating_users_count(30.days.ago),
  }
end

.postsObject



53
54
55
56
57
58
59
60
# File 'lib/statistics.rb', line 53

def self.posts
  {
    last_day: Post.where("created_at > ?", 1.day.ago).count,
    "7_days": Post.where("created_at > ?", 7.days.ago).count,
    "30_days": Post.where("created_at > ?", 30.days.ago).count,
    count: Post.count,
  }
end

.topicsObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/statistics.rb', line 62

def self.topics
  topics = Topic.listable_topics

  {
    last_day: topics.where("created_at > ?", 1.day.ago).count,
    "7_days": topics.where("created_at > ?", 7.days.ago).count,
    "30_days": topics.where("created_at > ?", 30.days.ago).count,
    count: topics.count,
  }
end

.usersObject



73
74
75
76
77
78
79
80
# File 'lib/statistics.rb', line 73

def self.users
  {
    last_day: valid_users.where("created_at > ?", 1.day.ago).count,
    "7_days": valid_users.where("created_at > ?", 7.days.ago).count,
    "30_days": valid_users.where("created_at > ?", 30.days.ago).count,
    count: valid_users.count,
  }
end

.visitorsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/statistics.rb', line 90

def self.visitors
  periods = [[1.day.ago, :last_day], [7.days.ago, :"7_days"], [30.days.ago, :"30_days"]]

  periods
    .map do |(period, key)|
      anon_page_views =
        ApplicationRequest.request_type_count_for_period(:page_view_anon_browser, period)

      logged_in_visitors = logged_in_visitors_count(period)
      next key, anon_page_views if logged_in_visitors == 0

      logged_in_page_views =
        ApplicationRequest.request_type_count_for_period(:page_view_logged_in_browser, period)
      next key, anon_page_views + logged_in_visitors if logged_in_page_views == 0

      total_visitors = logged_in_visitors
      avg_logged_in_page_view_per_user = logged_in_page_views.to_f / logged_in_visitors
      anon_visitors = (anon_page_views / avg_logged_in_page_view_per_user).round
      total_visitors += anon_visitors
      [key, total_visitors]
    end
    .to_h
end