Module: Persona::Query
- Defined in:
- lib/persona/query.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#action_count(action) ⇒ Object
—- Counts —————————————————————.
-
#action_share(action) ⇒ Object
What % of activity is a specific action?.
- #actions_between(from, to) ⇒ Object
- #activity_log(limit = 10) ⇒ Object
-
#daily_activity(days = 30) ⇒ Object
Actions grouped by day for the last N days.
- #days_since_last_activity ⇒ Object
-
#ever_did?(action) ⇒ Boolean
—- Presence ————————————————————-.
- #first_action ⇒ Object
- #first_active_at ⇒ Object
-
#inactive_since?(days = Persona.configuration.inactivity_threshold_days) ⇒ Boolean
—- Inactivity ———————————————————–.
-
#last_action ⇒ Object
—- Recency ————————————————————–.
- #last_active_at ⇒ Object
- #least_frequent_action ⇒ Object
-
#most_frequent_action ⇒ Object
—- Frequency ————————————————————.
- #never_did?(action) ⇒ Boolean
-
#peak_hour ⇒ Object
Peak hour (0-23) when this record is most active.
-
#persona_summary ⇒ Object
—- Summaries ————————————————————.
- #top_actions(limit = 3) ⇒ Object
- #total_events ⇒ Object
Class Method Details
.included(base) ⇒ Object
125 126 127 |
# File 'lib/persona/query.rb', line 125 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#action_count(action) ⇒ Object
—- Counts —————————————————————
5 6 7 |
# File 'lib/persona/query.rb', line 5 def action_count(action) persona_events.for_action(action).count end |
#action_share(action) ⇒ Object
What % of activity is a specific action?
108 109 110 111 112 |
# File 'lib/persona/query.rb', line 108 def action_share(action) total = total_events return 0.0 if total.zero? (action_count(action).to_f / total * 100).round(1) end |
#actions_between(from, to) ⇒ Object
75 76 77 78 |
# File 'lib/persona/query.rb', line 75 def actions_between(from, to) persona_events.since(from).before(to).group(:action).count .transform_keys(&:to_sym) end |
#activity_log(limit = 10) ⇒ Object
80 81 82 83 84 |
# File 'lib/persona/query.rb', line 80 def activity_log(limit = 10) persona_events.recent(limit).map do |e| { action: e.action.to_sym, at: e.created_at, metadata: e. } end end |
#daily_activity(days = 30) ⇒ Object
Actions grouped by day for the last N days
89 90 91 92 93 94 95 |
# File 'lib/persona/query.rb', line 89 def daily_activity(days = 30) persona_events .since(days.days.ago) .group("DATE(created_at)") .order("DATE(created_at) ASC") .count end |
#days_since_last_activity ⇒ Object
53 54 55 56 |
# File 'lib/persona/query.rb', line 53 def days_since_last_activity return nil if last_active_at.nil? ((Time.current - last_active_at) / 1.day).to_i end |
#ever_did?(action) ⇒ Boolean
—- Presence ————————————————————-
60 61 62 |
# File 'lib/persona/query.rb', line 60 def ever_did?(action) persona_events.for_action(action).exists? end |
#first_action ⇒ Object
38 39 40 |
# File 'lib/persona/query.rb', line 38 def first_action persona_events.oldest.first&.action&.to_sym end |
#first_active_at ⇒ Object
42 43 44 |
# File 'lib/persona/query.rb', line 42 def first_active_at persona_events.oldest.first&.created_at end |
#inactive_since?(days = Persona.configuration.inactivity_threshold_days) ⇒ Boolean
—- Inactivity ———————————————————–
48 49 50 51 |
# File 'lib/persona/query.rb', line 48 def inactive_since?(days = Persona.configuration.inactivity_threshold_days) return true if last_active_at.nil? last_active_at < days.days.ago end |
#last_action ⇒ Object
—- Recency ————————————————————–
30 31 32 |
# File 'lib/persona/query.rb', line 30 def last_action persona_events.recent(1).first&.action&.to_sym end |
#last_active_at ⇒ Object
34 35 36 |
# File 'lib/persona/query.rb', line 34 def last_active_at persona_events.recent(1).first&.created_at end |
#least_frequent_action ⇒ Object
19 20 21 |
# File 'lib/persona/query.rb', line 19 def least_frequent_action persona_events.group(:action).order("count_all ASC").count.keys.first&.to_sym end |
#most_frequent_action ⇒ Object
—- Frequency ————————————————————
15 16 17 |
# File 'lib/persona/query.rb', line 15 def most_frequent_action persona_events.group(:action).order("count_all DESC").count.keys.first&.to_sym end |
#never_did?(action) ⇒ Boolean
64 65 66 |
# File 'lib/persona/query.rb', line 64 def never_did?(action) !ever_did?(action) end |
#peak_hour ⇒ Object
Peak hour (0-23) when this record is most active
98 99 100 101 102 103 104 105 |
# File 'lib/persona/query.rb', line 98 def peak_hour persona_events .group("EXTRACT(HOUR FROM created_at)::int") .order("count_all DESC") .count .keys .first end |
#persona_summary ⇒ Object
—- Summaries ————————————————————
70 71 72 73 |
# File 'lib/persona/query.rb', line 70 def persona_summary persona_events.group(:action).order("count_all DESC").count .transform_keys(&:to_sym) end |
#top_actions(limit = 3) ⇒ Object
23 24 25 26 |
# File 'lib/persona/query.rb', line 23 def top_actions(limit = 3) persona_events.group(:action).order("count_all DESC").limit(limit).count .transform_keys(&:to_sym) end |
#total_events ⇒ Object
9 10 11 |
# File 'lib/persona/query.rb', line 9 def total_events persona_events.count end |