@@ -22,12 +22,21 @@ def person_ids_for_query
2222
2323 def load_activity_data ( scope : nil , year : nil )
2424 @activity_filters = parse_activity_filters
25- @activity_entries = build_activity_entries ( scope : scope , filters : @activity_filters )
25+ effective_scope = scope || default_recent_scope
26+ @activity_entries = build_activity_entries ( scope : effective_scope , filters : @activity_filters )
27+ @activity_summary = build_activity_summary ( scope : effective_scope , filters : @activity_filters )
28+ @activity_period ||= { type : :recent } if scope . nil?
2629 @contribution_years = contribution_years
2730 @contribution_year = year || select_contribution_year ( @contribution_years )
2831 @contribution_weeks , @contribution_month_spans = build_contribution_weeks ( @contribution_year , filters : @activity_filters )
2932 end
3033
34+ def default_recent_scope
35+ ids = person_ids_for_query
36+ start_date = 1 . month . ago . beginning_of_day
37+ Message . where ( sender_person_id : ids , created_at : start_date ..)
38+ end
39+
3140 def build_activity_entries ( scope : nil , filters : nil )
3241 ids = person_ids_for_query
3342 return [ ] if ids . blank?
@@ -73,6 +82,63 @@ def build_activity_entries(scope: nil, filters: nil)
7382 entries . first ( 20 )
7483 end
7584
85+ def build_activity_summary ( scope : nil , filters : nil )
86+ ids = person_ids_for_query
87+ return empty_activity_summary if ids . blank?
88+
89+ scope ||= Message . where ( sender_person_id : ids )
90+ messages = scope . includes ( :topic , :attachments )
91+
92+ return empty_activity_summary if messages . empty?
93+
94+ topic_ids = messages . map ( &:topic_id ) . uniq
95+ first_message_per_topic = Message . where ( topic_id : topic_ids ) . group ( :topic_id ) . minimum ( :id )
96+ first_patch_per_topic = Message . joins ( :attachments ) . where ( topic_id : topic_ids ) . group ( :topic_id ) . minimum ( :id )
97+ own_topic_ids = Topic . where ( id : topic_ids , creator_person_id : ids ) . pluck ( :id ) . to_set
98+
99+ filter_symbols = filters &.map ( &:to_sym ) &.to_set
100+
101+ summary = {
102+ total : 0 ,
103+ started_thread : 0 ,
104+ replied_own_thread : 0 ,
105+ replied_other_thread : 0 ,
106+ sent_first_patch : 0 ,
107+ sent_followup_patch : 0
108+ }
109+
110+ messages . each do |message |
111+ topic = message . topic
112+ next unless topic
113+
114+ activity_types = compute_activity_types (
115+ message : message ,
116+ topic : topic ,
117+ first_message_per_topic : first_message_per_topic ,
118+ first_patch_per_topic : first_patch_per_topic ,
119+ own_topic_ids : own_topic_ids
120+ )
121+
122+ if filter_symbols . blank? || ( activity_types . to_set & filter_symbols ) . any?
123+ summary [ :total ] += 1
124+ activity_types . each { |type | summary [ type ] += 1 }
125+ end
126+ end
127+
128+ summary
129+ end
130+
131+ def empty_activity_summary
132+ {
133+ total : 0 ,
134+ started_thread : 0 ,
135+ replied_own_thread : 0 ,
136+ replied_other_thread : 0 ,
137+ sent_first_patch : 0 ,
138+ sent_followup_patch : 0
139+ }
140+ end
141+
76142 def compute_activity_types ( message :, topic :, first_message_per_topic :, first_patch_per_topic :, own_topic_ids :)
77143 is_first_message = first_message_per_topic [ topic . id ] == message . id
78144 is_own_thread = own_topic_ids . include? ( topic . id )
0 commit comments