-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.rb
270 lines (233 loc) · 10 KB
/
plugin.rb
1
2
3
4
5
6
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# name: report-job
# about: Weekly email report for various metrics
# version: 0.0.1
# authors: Garrett Scholtes
# url: https://github.com/kCura/DiscourseEmailPlugin
enabled_site_setting :weekly_email_report_enabled
PATH = File.dirname(path)
after_initialize do
require_dependency 'email/sender'
require_dependency 'email/message_builder'
if SiteSetting.weekly_email_report_interval < 1
warn "WARNING: weekly_email_report_interval setting is less than 1 day"
end
module ::WeeklyJobHelpers
def self.report_time
items = SiteSetting.weekly_email_report_time_of_day.split(":")
if items.size == 2
hours = items.first.to_i
minutes = items.last.to_i
if (0...24).include? hours and (0...60).include? minutes
else
warn "WARNING: weekly_email_report_time_of_day has invalid format"
return default_report_time
end
else
warn "WARNING: weekly_email_report_time_of_day has invalid format"
return default_report_time
end
return hours.hours + minutes.minutes
end
def self.default_report_time
items = SiteSetting.defaults[:weekly_email_report_time_of_day].split(":")
hours = items.first.to_i
minutes = items.last.to_i
return hours.hours + minutes.minutes
end
def self.weekday
case SiteSetting.weekly_email_report_day_of_week.downcase.strip
when "sunday"
0
when "monday"
1
when "tuesday"
2
when "wednesday"
3
when "thursday"
4
when "friday"
5
when "saturday"
6
else
warn "WARNING: invalid weekday for weekly_email_report_day_of_week"
0
end
end
def self.is_correct_weekday
Date.today.wday == weekday
end
end
class ::WeeklyReportMailer < ::ActionMailer::Base
default from: SiteSetting.notification_email
def send_report_to(recipient, metrics, time)
@metrics = metrics
@hostroot = "#{SiteSetting.weekly_email_report_domain}"
mail(to: recipient, subject: weekly_subject(time)) { |f|
f.text { render file: File.join(view_path, "send_report_to.text.erb") }
f.html { render file: File.join(view_path, "send_report_to.html.erb") }
}
end
private
def weekly_subject(time)
"[#{time.to_date}] #{SiteSetting.weekly_email_report_subject}"
end
def view_path
# Hack. Rails will not look in the plugin's app/views directory
# for views. This is a workaround.
File.join(PATH, "app", "views", "weekly_report_mailer")
end
end
class ::Jobs::MetricsReportJob < Jobs::Scheduled
daily at: ::WeeklyJobHelpers.report_time
include ActionView::Helpers::DateHelper
def execute(args)
if (SiteSetting.weekly_email_report_enabled? and ::WeeklyJobHelpers.is_correct_weekday) or args[:force]
@latest_midnight = Time.now.midnight
@interval = SiteSetting.weekly_email_report_interval.days
topic_list = new_topics
metrics = {
new_topics: topic_list,
no_response: no_response(topic_list),
average_response_time: average_response_time(topic_list),
solved: solved(topic_list),
top_posters: top_posters(SiteSetting.weekly_email_report_top_user_count),
top_creators: top_topic_creators(SiteSetting.weekly_email_report_top_user_count),
table: gather_category_statistics(topic_list)
}
recipients = SiteSetting.weekly_email_report_recipients.split(",")
recipients.map! { |recipient|
recipient.strip
}
recipients.each { |recipient|
WeeklyReportMailer.send_report_to(recipient, metrics, @latest_midnight).deliver_now
}
end
end
private
def new_topics
Topic.where(created_at: (@latest_midnight - @interval)..@latest_midnight, subtype: nil)
end
def no_response(topics)
topics.where(posts_count: (0..1))
end
def average_response_time(tops, category=nil)
if category
topics = tops.where("posts_count > 1").where(category: category)
else
topics = tops.where("posts_count > 1")
end
avg = 0
topics_size = topics.size
if topics_size > 0
avg = topics.inject(0) { |acc, topic|
# Second post is the first reply (first post is the topic itself -- not a reply)
post_time = Post.where(topic: topic).order(:created_at).limit(2)[1].created_at.to_i
topic_time = topic.created_at.to_i
acc + post_time - topic_time
} / topics_size
end
distance_of_time_in_words(avg)
end
def solved(topics)
TopicCustomField.where(created_at: (@latest_midnight - @interval)..@latest_midnight)
.where(name: "accepted_answer_post_id")
.where("topic_id IN (?)", topics.select(:id))
.size
end
def top_posters(limit)
posts = Post.where(created_at: (@latest_midnight - @interval)..@latest_midnight)
posts.map { |p|
p.username
}.each_with_object(Hash.new(0)) { |username, occurrences|
occurrences[username] += 1
}.sort_by { |username, count|
-count
}.map { |username, count|
{username: username, posts: count}
}.select { |user|
user[:username] != "system"
}[0,limit]
end
def top_topic_creators(limit)
topics = Topic.where(created_at: (@latest_midnight - @interval)..@latest_midnight)
topics.map { |t|
User.find(t.user_id).username
}.each_with_object(Hash.new(0)) { |username, occurrences|
occurrences[username] += 1
}.sort_by { |username, count|
-count
}.map { |username, count|
{username: username, topics: count}
}.select { |user|
user[:username] != "system"
}[0,limit]
end
def gather_category_statistics(tops)
table = []
Category.select(:id, :name, :slug, :color, :parent_category_id).each { |category|
parent = nil
if not category.parent_category_id.nil?
parent = Category.find(category.parent_category_id)
end
samples = tops.where(category: category)
response_percent = ""
percent_compliant = ""
art = ""
if samples.length > 0
art = average_response_time(tops, category)
selected = samples.where("posts_count > 1")
response_percent = sprintf("%0.1f %", 100*selected.length.to_f / samples.length)
compliant = selected.select { |topic|
# Second post is the first reply (first post is the topic itself -- not a reply)
post_time = Post.where(topic: topic).order(:created_at).limit(2)[1].created_at.to_i
topic_time = topic.created_at.to_i
post_time - topic_time < 60 * SiteSetting.weekly_email_report_compliance
}
percent_compliant = compliant.length.to_f / samples.length
percent_compliant = sprintf("%0.1f %", 100*percent_compliant)
end
table << {
category: category,
avg_response_time: art,
percent_response_compliant: percent_compliant,
response_percent: response_percent,
sample_size: samples.length,
color: category.color,
parent: parent
}
}
# Sorts by parent categories, and then by subcategories under each parent
table.sort_by { |row|
name = ""
if row[:parent]
name = row[:parent].name.downcase
end
name + row[:category].name.downcase
}
end
end
# ---------- Send an email immediately when the plugin loads ----------------
# ---------- otherwise we might have to wait a week to get a report ------------
::Jobs::MetricsReportJob.new.execute({force: true})
# ---------------------------------------------------------------------------
module ::DiscourseReportJob
class Engine < ::Rails::Engine
isolate_namespace DiscourseReportJob
end
end
require_dependency 'application_controller'
class DiscourseReportJob::ReporterController < ::ApplicationController
def send(*args)
::Jobs::MetricsReportJob.new.execute({force: true})
end
end
DiscourseReportJob::Engine.routes.draw do
put 'trigger' => 'reporter#send'
end
add_admin_route 'report_job.title', 'report-job'
Discourse::Application.routes.append do
mount ::DiscourseReportJob::Engine, at: '/admin/plugins/report-job', constraints: StaffConstraint.new
end
end