2013-04-17 04:56:18 +08:00
|
|
|
require_dependency 'topic_subtype'
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
class Report
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
attr_accessor :type, :data, :total, :prev30Days, :start_date,
|
2018-05-11 11:30:21 +08:00
|
|
|
:end_date, :category_id, :group_id, :labels, :async,
|
2018-05-18 04:44:33 +08:00
|
|
|
:prev_period, :facets, :limit, :processing, :average, :percent,
|
|
|
|
:higher_is_better
|
2014-11-05 06:08:39 +08:00
|
|
|
|
|
|
|
def self.default_days
|
|
|
|
30
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
|
|
def initialize(type)
|
|
|
|
@type = type
|
2018-02-02 04:50:41 +08:00
|
|
|
@start_date ||= Report.default_days.days.ago.beginning_of_day
|
2015-02-07 06:20:11 +08:00
|
|
|
@end_date ||= Time.zone.now.end_of_day
|
2018-05-18 04:44:33 +08:00
|
|
|
@average = false
|
|
|
|
@percent = false
|
|
|
|
@higher_is_better = true
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
def self.cache_key(report)
|
2018-05-11 11:30:21 +08:00
|
|
|
(+"reports:") <<
|
|
|
|
[
|
|
|
|
report.type,
|
|
|
|
report.category_id,
|
|
|
|
report.start_date.to_date.strftime("%Y%m%d"),
|
|
|
|
report.end_date.to_date.strftime("%Y%m%d"),
|
|
|
|
report.group_id,
|
2018-05-15 13:08:23 +08:00
|
|
|
report.facets,
|
|
|
|
report.limit
|
2018-05-11 11:30:21 +08:00
|
|
|
].map(&:to_s).join(':')
|
2018-05-03 21:41:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache
|
|
|
|
Discourse.cache.keys("reports:*").each do |key|
|
|
|
|
Discourse.cache.redis.del(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def as_json(options = nil)
|
2018-05-14 22:34:56 +08:00
|
|
|
description = I18n.t("reports.#{type}.description", default: "")
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
{
|
2014-11-05 06:08:39 +08:00
|
|
|
type: type,
|
|
|
|
title: I18n.t("reports.#{type}.title"),
|
|
|
|
xaxis: I18n.t("reports.#{type}.xaxis"),
|
|
|
|
yaxis: I18n.t("reports.#{type}.yaxis"),
|
2018-05-14 22:34:56 +08:00
|
|
|
description: description.presence ? description : nil,
|
2014-11-05 06:08:39 +08:00
|
|
|
data: data,
|
2018-05-14 09:12:52 +08:00
|
|
|
start_date: start_date&.iso8601,
|
|
|
|
end_date: end_date&.iso8601,
|
2015-06-24 21:19:39 +08:00
|
|
|
category_id: category_id,
|
2016-02-03 10:29:51 +08:00
|
|
|
group_id: group_id,
|
2018-04-20 00:19:21 +08:00
|
|
|
prev30Days: self.prev30Days,
|
2018-05-11 11:30:21 +08:00
|
|
|
report_key: Report.cache_key(self),
|
2018-05-16 02:12:03 +08:00
|
|
|
labels: labels,
|
|
|
|
processing: self.processing,
|
|
|
|
average: self.average,
|
2018-05-18 04:44:33 +08:00
|
|
|
percent: self.percent,
|
|
|
|
higher_is_better: self.higher_is_better
|
2018-03-16 05:10:45 +08:00
|
|
|
}.tap do |json|
|
2018-05-11 11:30:21 +08:00
|
|
|
json[:total] = total if total
|
|
|
|
json[:prev_period] = prev_period if prev_period
|
|
|
|
json[:prev30Days] = self.prev30Days if self.prev30Days
|
2018-05-15 13:08:23 +08:00
|
|
|
json[:limit] = self.limit if self.limit
|
2018-05-11 11:30:21 +08:00
|
|
|
|
2018-03-16 05:10:45 +08:00
|
|
|
if type == 'page_view_crawler_reqs'
|
|
|
|
json[:related_report] = Report.find('web_crawlers', start_date: start_date, end_date: end_date)&.as_json
|
|
|
|
end
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2015-06-25 08:42:08 +08:00
|
|
|
def Report.add_report(name, &block)
|
|
|
|
singleton_class.instance_eval { define_method("report_#{name}", &block) }
|
|
|
|
end
|
|
|
|
|
2018-05-16 14:05:03 +08:00
|
|
|
def self._get(type, opts = nil)
|
2014-11-05 06:08:39 +08:00
|
|
|
opts ||= {}
|
2015-10-20 04:30:34 +08:00
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
# Load the report
|
|
|
|
report = Report.new(type)
|
2018-05-03 23:39:37 +08:00
|
|
|
report.start_date = opts[:start_date] if opts[:start_date]
|
|
|
|
report.end_date = opts[:end_date] if opts[:end_date]
|
2015-06-24 21:19:39 +08:00
|
|
|
report.category_id = opts[:category_id] if opts[:category_id]
|
2016-02-03 10:29:51 +08:00
|
|
|
report.group_id = opts[:group_id] if opts[:group_id]
|
2018-05-11 11:30:21 +08:00
|
|
|
report.facets = opts[:facets] || [:total, :prev30Days]
|
2018-05-15 13:08:23 +08:00
|
|
|
report.limit = opts[:limit] if opts[:limit]
|
2018-05-16 02:12:03 +08:00
|
|
|
report.processing = false
|
2018-05-18 04:44:33 +08:00
|
|
|
report.average = opts[:average] if opts[:average]
|
|
|
|
report.percent = opts[:percent] if opts[:percent]
|
|
|
|
report.higher_is_better = opts[:higher_is_better] if opts[:higher_is_better]
|
2018-05-16 14:05:03 +08:00
|
|
|
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_cached(type, opts = nil)
|
|
|
|
report = _get(type, opts)
|
|
|
|
Discourse.cache.read(cache_key(report))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache(report, duration)
|
|
|
|
Discourse.cache.write(Report.cache_key(report), report.as_json, force: true, expires_in: duration)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find(type, opts = nil)
|
|
|
|
report = _get(type, opts)
|
2015-02-05 13:08:52 +08:00
|
|
|
report_method = :"report_#{type}"
|
|
|
|
|
|
|
|
if respond_to?(report_method)
|
2018-05-16 14:05:03 +08:00
|
|
|
send(report_method, report)
|
2015-02-05 13:08:52 +08:00
|
|
|
elsif type =~ /_reqs$/
|
|
|
|
req_report(report, type.split(/_reqs$/)[0].to_sym)
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2015-06-24 21:19:39 +08:00
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
report
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.req_report(report, filter = nil)
|
2015-02-06 11:39:04 +08:00
|
|
|
data =
|
|
|
|
if filter == :page_view_total
|
|
|
|
ApplicationRequest.where(req_type: [
|
2017-07-28 09:20:09 +08:00
|
|
|
ApplicationRequest.req_types.reject { |k, v| k =~ /mobile/ }.map { |k, v| v if k =~ /page_view/ }.compact
|
2015-09-23 13:24:30 +08:00
|
|
|
].flatten)
|
2015-02-06 11:39:04 +08:00
|
|
|
else
|
|
|
|
ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
|
|
|
|
end
|
2015-02-05 08:18:11 +08:00
|
|
|
|
|
|
|
report.data = []
|
2018-05-03 21:41:41 +08:00
|
|
|
data.where('date >= ? AND date <= ?', report.start_date, report.end_date)
|
2017-07-28 09:20:09 +08:00
|
|
|
.order(date: :asc)
|
|
|
|
.group(:date)
|
|
|
|
.sum(:count)
|
|
|
|
.each do |date, count|
|
2015-06-24 21:19:39 +08:00
|
|
|
report.data << { x: date, y: count }
|
2015-02-05 08:18:11 +08:00
|
|
|
end
|
|
|
|
|
2018-02-02 04:50:41 +08:00
|
|
|
report.total = data.sum(:count)
|
|
|
|
|
|
|
|
report.prev30Days = data.where(
|
|
|
|
'date >= ? AND date < ?',
|
2018-05-03 21:41:41 +08:00
|
|
|
(report.start_date - 31.days), report.start_date
|
2018-02-02 04:50:41 +08:00
|
|
|
).sum(:count)
|
2015-02-05 08:18:11 +08:00
|
|
|
end
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
def self.report_visits(report)
|
2016-02-03 10:29:51 +08:00
|
|
|
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date, report.group_id
|
|
|
|
|
2015-06-16 01:16:23 +08:00
|
|
|
add_counts report, UserVisit, 'visited_at'
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
2015-07-08 00:31:07 +08:00
|
|
|
def self.report_mobile_visits(report)
|
|
|
|
basic_report_about report, UserVisit, :mobile_by_day, report.start_date, report.end_date
|
|
|
|
report.total = UserVisit.where(mobile: true).count
|
|
|
|
report.prev30Days = UserVisit.where(mobile: true).where("visited_at >= ? and visited_at < ?", report.start_date - 30.days, report.start_date).count
|
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_signups(report)
|
2016-02-03 10:29:51 +08:00
|
|
|
if report.group_id
|
|
|
|
basic_report_about report, User.real, :count_by_signup_date, report.start_date, report.end_date, report.group_id
|
|
|
|
add_counts report, User.real, 'users.created_at'
|
|
|
|
else
|
2018-05-03 23:39:37 +08:00
|
|
|
|
2016-02-03 10:29:51 +08:00
|
|
|
report_about report, User.real, :count_by_signup_date
|
|
|
|
end
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
2018-04-26 20:49:41 +08:00
|
|
|
def self.report_new_contributors(report)
|
2018-05-03 21:41:41 +08:00
|
|
|
report.data = []
|
|
|
|
|
|
|
|
data = User.real.count_by_first_post(report.start_date, report.end_date)
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev30Days)
|
|
|
|
prev30DaysData = User.real.count_by_first_post(report.start_date - 30.days, report.start_date)
|
|
|
|
report.prev30Days = prev30DaysData.sum { |k, v| v }
|
|
|
|
end
|
|
|
|
|
|
|
|
if report.facets.include?(:total)
|
|
|
|
report.total = User.real.count_by_first_post
|
|
|
|
end
|
2018-05-03 21:41:41 +08:00
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev_period)
|
|
|
|
prev_period_data = User.real.count_by_first_post(report.start_date - (report.end_date - report.start_date), report.start_date)
|
|
|
|
report.prev_period = prev_period_data.sum { |k, v| v }
|
|
|
|
end
|
2018-05-03 21:41:41 +08:00
|
|
|
|
|
|
|
data.each do |key, value|
|
|
|
|
report.data << { x: key, y: value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_daily_engaged_users(report)
|
2018-05-16 02:12:03 +08:00
|
|
|
report.average = true
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
report.data = []
|
|
|
|
|
|
|
|
data = UserAction.count_daily_engaged_users(report.start_date, report.end_date)
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev30Days)
|
|
|
|
prev30DaysData = UserAction.count_daily_engaged_users(report.start_date - 30.days, report.start_date)
|
|
|
|
report.prev30Days = prev30DaysData.sum { |k, v| v }
|
|
|
|
end
|
|
|
|
|
|
|
|
if report.facets.include?(:total)
|
|
|
|
report.total = UserAction.count_daily_engaged_users
|
|
|
|
end
|
2018-05-03 21:41:41 +08:00
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev_period)
|
|
|
|
prev_data = UserAction.count_daily_engaged_users(report.start_date - (report.end_date - report.start_date), report.start_date)
|
2018-05-15 08:17:17 +08:00
|
|
|
|
|
|
|
prev = prev_data.sum { |k, v| v }
|
|
|
|
if prev > 0
|
|
|
|
prev = prev / ((report.end_date - report.start_date) / 1.day)
|
|
|
|
end
|
|
|
|
report.prev_period = prev
|
2018-05-11 11:30:21 +08:00
|
|
|
end
|
2018-05-03 21:41:41 +08:00
|
|
|
|
|
|
|
data.each do |key, value|
|
|
|
|
report.data << { x: key, y: value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_dau_by_mau(report)
|
2018-05-16 02:12:03 +08:00
|
|
|
report.average = true
|
|
|
|
report.percent = true
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
data_points = UserVisit.count_by_active_users(report.start_date, report.end_date)
|
|
|
|
|
|
|
|
report.data = []
|
|
|
|
|
|
|
|
compute_dau_by_mau = Proc.new { |data_point|
|
|
|
|
if data_point["mau"] == 0
|
|
|
|
0
|
|
|
|
else
|
2018-05-11 11:30:21 +08:00
|
|
|
((data_point["dau"].to_f / data_point["mau"].to_f) * 100).ceil(2)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
dau_avg = Proc.new { |start_date, end_date|
|
|
|
|
data_points = UserVisit.count_by_active_users(start_date, end_date)
|
|
|
|
if !data_points.empty?
|
|
|
|
sum = data_points.sum { |data_point| compute_dau_by_mau.call(data_point) }
|
|
|
|
(sum.to_f / data_points.count.to_f).ceil(2)
|
2018-05-03 21:41:41 +08:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
data_points.each do |data_point|
|
|
|
|
report.data << { x: data_point["date"], y: compute_dau_by_mau.call(data_point) }
|
|
|
|
end
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev_period)
|
|
|
|
report.prev_period = dau_avg.call(report.start_date - (report.end_date - report.start_date), report.start_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
if report.facets.include?(:prev30Days)
|
|
|
|
report.prev30Days = dau_avg.call(report.start_date - 30.days, report.start_date)
|
2018-05-03 21:41:41 +08:00
|
|
|
end
|
2018-04-26 20:49:41 +08:00
|
|
|
end
|
|
|
|
|
2015-09-15 01:30:06 +08:00
|
|
|
def self.report_profile_views(report)
|
2018-05-03 21:41:41 +08:00
|
|
|
start_date = report.start_date
|
|
|
|
end_date = report.end_date
|
2016-02-03 10:29:51 +08:00
|
|
|
basic_report_about report, UserProfileView, :profile_views_by_day, start_date, end_date, report.group_id
|
|
|
|
|
2015-09-15 01:30:06 +08:00
|
|
|
report.total = UserProfile.sum(:views)
|
|
|
|
report.prev30Days = UserProfileView.where("viewed_at >= ? AND viewed_at < ?", start_date - 30.days, start_date + 1).count
|
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_topics(report)
|
2015-06-24 21:19:39 +08:00
|
|
|
basic_report_about report, Topic, :listable_count_per_day, report.start_date, report.end_date, report.category_id
|
|
|
|
countable = Topic.listable_topics
|
|
|
|
countable = countable.where(category_id: report.category_id) if report.category_id
|
|
|
|
add_counts report, countable, 'topics.created_at'
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_posts(report)
|
2015-06-24 21:19:39 +08:00
|
|
|
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date, report.category_id
|
2017-02-22 03:45:34 +08:00
|
|
|
countable = Post.public_posts.where(post_type: Post.types[:regular])
|
2015-06-24 21:19:39 +08:00
|
|
|
countable = countable.joins(:topic).where("topics.category_id = ?", report.category_id) if report.category_id
|
|
|
|
add_counts report, countable, 'posts.created_at'
|
2013-04-04 01:25:52 +08:00
|
|
|
end
|
|
|
|
|
2015-06-23 01:46:51 +08:00
|
|
|
def self.report_time_to_first_response(report)
|
2018-05-18 04:44:33 +08:00
|
|
|
report.higher_is_better = false
|
2015-06-23 01:46:51 +08:00
|
|
|
report.data = []
|
2017-07-28 09:20:09 +08:00
|
|
|
Topic.time_to_first_response_per_day(report.start_date, report.end_date, category_id: report.category_id).each do |r|
|
2015-06-23 01:46:51 +08:00
|
|
|
report.data << { x: Date.parse(r["date"]), y: r["hours"].to_f.round(2) }
|
|
|
|
end
|
2015-06-24 21:19:39 +08:00
|
|
|
report.total = Topic.time_to_first_response_total(category_id: report.category_id)
|
|
|
|
report.prev30Days = Topic.time_to_first_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: report.category_id)
|
2015-06-23 01:46:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_topics_with_no_response(report)
|
2015-06-26 06:45:11 +08:00
|
|
|
report.data = []
|
|
|
|
Topic.with_no_response_per_day(report.start_date, report.end_date, report.category_id).each do |r|
|
2015-07-01 03:42:38 +08:00
|
|
|
report.data << { x: Date.parse(r["date"]), y: r["count"].to_i }
|
2015-06-26 06:45:11 +08:00
|
|
|
end
|
2015-06-24 21:19:39 +08:00
|
|
|
report.total = Topic.with_no_response_total(category_id: report.category_id)
|
|
|
|
report.prev30Days = Topic.with_no_response_total(start_date: report.start_date - 30.days, end_date: report.start_date, category_id: report.category_id)
|
2015-06-23 01:46:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-01 21:21:34 +08:00
|
|
|
def self.report_emails(report)
|
|
|
|
report_about report, EmailLog
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_about(report, subject_class, report_method = :count_per_day)
|
2014-11-06 02:11:23 +08:00
|
|
|
basic_report_about report, subject_class, report_method, report.start_date, report.end_date
|
2014-12-30 22:06:15 +08:00
|
|
|
add_counts report, subject_class
|
2013-04-01 21:21:34 +08:00
|
|
|
end
|
|
|
|
|
2013-04-17 04:56:18 +08:00
|
|
|
def self.basic_report_about(report, subject_class, report_method, *args)
|
2013-03-08 00:07:59 +08:00
|
|
|
report.data = []
|
2018-05-03 23:39:37 +08:00
|
|
|
|
2014-11-05 06:08:39 +08:00
|
|
|
subject_class.send(report_method, *args).each do |date, count|
|
2015-06-24 21:19:39 +08:00
|
|
|
report.data << { x: date, y: count }
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
2013-04-01 21:21:34 +08:00
|
|
|
end
|
|
|
|
|
2014-12-30 22:06:15 +08:00
|
|
|
def self.add_counts(report, subject_class, query_column = 'created_at')
|
2018-05-11 11:30:21 +08:00
|
|
|
if report.facets.include?(:prev_period)
|
|
|
|
report.prev_period = subject_class
|
|
|
|
.where("#{query_column} >= ? and #{query_column} < ?",
|
|
|
|
(report.start_date - (report.end_date - report.start_date)),
|
|
|
|
report.start_date).count
|
|
|
|
end
|
|
|
|
|
|
|
|
if report.facets.include?(:total)
|
|
|
|
report.total = subject_class.count
|
|
|
|
end
|
|
|
|
|
|
|
|
if report.facets.include?(:prev30Days)
|
|
|
|
report.prev30Days = subject_class
|
|
|
|
.where("#{query_column} >= ? and #{query_column} < ?",
|
|
|
|
report.start_date - 30.days,
|
|
|
|
report.start_date).count
|
|
|
|
end
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
def self.report_users_by_trust_level(report)
|
|
|
|
report.data = []
|
2018-05-22 22:47:23 +08:00
|
|
|
|
2018-05-11 14:32:12 +08:00
|
|
|
User.real.group('trust_level').count.sort.each do |level, count|
|
2018-05-22 22:47:23 +08:00
|
|
|
key = TrustLevel.levels[level.to_i]
|
|
|
|
url = Proc.new { |key| "/admin/users/list/#{key}" }
|
|
|
|
report.data << { url: url.call(key), key: key, x: level.to_i, y: count }
|
2013-04-19 02:27:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Post action counts:
|
2013-03-13 02:19:01 +08:00
|
|
|
def self.report_flags(report)
|
2015-06-24 21:19:39 +08:00
|
|
|
basic_report_about report, PostAction, :flag_count_by_date, report.start_date, report.end_date, report.category_id
|
2017-10-18 01:31:45 +08:00
|
|
|
countable = PostAction.where(post_action_type_id: PostActionType.flag_types_without_custom.values)
|
2015-06-24 21:19:39 +08:00
|
|
|
countable = countable.joins(post: :topic).where("topics.category_id = ?", report.category_id) if report.category_id
|
|
|
|
add_counts report, countable, 'post_actions.created_at'
|
2013-03-13 02:19:01 +08:00
|
|
|
end
|
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
def self.report_likes(report)
|
|
|
|
post_action_report report, PostActionType.types[:like]
|
2013-03-16 06:08:46 +08:00
|
|
|
end
|
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
def self.report_bookmarks(report)
|
|
|
|
post_action_report report, PostActionType.types[:bookmark]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.post_action_report(report, post_action_type)
|
2013-03-18 01:53:00 +08:00
|
|
|
report.data = []
|
2015-10-20 04:30:34 +08:00
|
|
|
PostAction.count_per_day_for_type(post_action_type, category_id: report.category_id, start_date: report.start_date, end_date: report.end_date).each do |date, count|
|
2014-07-29 01:17:37 +08:00
|
|
|
report.data << { x: date, y: count }
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
2015-06-24 21:19:39 +08:00
|
|
|
countable = PostAction.unscoped.where(post_action_type_id: post_action_type)
|
|
|
|
countable = countable.joins(post: :topic).where("topics.category_id = ?", report.category_id) if report.category_id
|
|
|
|
add_counts report, countable, 'post_actions.created_at'
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
2013-04-17 04:56:18 +08:00
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
# Private messages counts:
|
|
|
|
|
2013-04-17 04:56:18 +08:00
|
|
|
def self.private_messages_report(report, topic_subtype)
|
2018-03-27 16:30:08 +08:00
|
|
|
basic_report_about report, Topic, :private_message_topics_count_per_day, report.start_date, report.end_date, topic_subtype
|
|
|
|
add_counts report, Topic.private_messages.with_subtype(topic_subtype), 'topics.created_at'
|
2013-04-17 04:56:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_user_to_user_private_messages(report)
|
|
|
|
private_messages_report report, TopicSubtype.user_to_user
|
|
|
|
end
|
|
|
|
|
2018-03-27 16:30:08 +08:00
|
|
|
def self.report_user_to_user_private_messages_with_replies(report)
|
|
|
|
topic_subtype = TopicSubtype.user_to_user
|
|
|
|
basic_report_about report, Post, :private_messages_count_per_day, report.start_date, report.end_date, topic_subtype
|
|
|
|
add_counts report, Post.private_posts.with_topic_subtype(topic_subtype), 'posts.created_at'
|
|
|
|
end
|
|
|
|
|
2013-04-17 04:56:18 +08:00
|
|
|
def self.report_system_private_messages(report)
|
|
|
|
private_messages_report report, TopicSubtype.system_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_moderator_warning_private_messages(report)
|
|
|
|
private_messages_report report, TopicSubtype.moderator_warning
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_notify_moderators_private_messages(report)
|
|
|
|
private_messages_report report, TopicSubtype.notify_moderators
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_notify_user_private_messages(report)
|
|
|
|
private_messages_report report, TopicSubtype.notify_user
|
|
|
|
end
|
2018-03-16 05:10:45 +08:00
|
|
|
|
|
|
|
def self.report_web_crawlers(report)
|
|
|
|
report.data = WebCrawlerRequest.where('date >= ? and date <= ?', report.start_date, report.end_date)
|
|
|
|
.limit(200)
|
|
|
|
.order('sum_count DESC')
|
|
|
|
.group(:user_agent).sum(:count)
|
|
|
|
.map { |ua, count| { x: ua, y: count } }
|
|
|
|
end
|
2018-04-16 19:03:43 +08:00
|
|
|
|
2018-04-19 03:30:41 +08:00
|
|
|
def self.report_users_by_type(report)
|
2018-04-16 19:03:43 +08:00
|
|
|
report.data = []
|
|
|
|
|
2018-05-22 22:47:23 +08:00
|
|
|
label = Proc.new { |x| I18n.t("reports.users_by_type.xaxis_labels.#{x}") }
|
|
|
|
url = Proc.new { |key| "/admin/users/list/#{key}" }
|
2018-04-16 19:03:43 +08:00
|
|
|
|
2018-04-19 03:30:41 +08:00
|
|
|
admins = User.real.admins.count
|
2018-05-22 22:47:23 +08:00
|
|
|
report.data << { url: url.call("admins"), icon: "shield", key: "admins", x: label.call("admin"), y: admins } if admins > 0
|
2018-04-16 19:03:43 +08:00
|
|
|
|
2018-04-19 03:30:41 +08:00
|
|
|
moderators = User.real.moderators.count
|
2018-05-22 22:47:23 +08:00
|
|
|
report.data << { url: url.call("moderators"), icon: "shield", key: "moderators", x: label.call("moderator"), y: moderators } if moderators > 0
|
2018-04-16 19:03:43 +08:00
|
|
|
|
2018-04-17 17:01:06 +08:00
|
|
|
suspended = User.real.suspended.count
|
2018-05-22 22:47:23 +08:00
|
|
|
report.data << { url: url.call("suspended"), icon: "ban", key: "suspended", x: label.call("suspended"), y: suspended } if suspended > 0
|
2018-04-16 19:03:43 +08:00
|
|
|
|
2018-04-17 17:01:06 +08:00
|
|
|
silenced = User.real.silenced.count
|
2018-05-22 22:47:23 +08:00
|
|
|
report.data << { url: url.call("silenced"), icon: "ban", key: "silenced", x: label.call("silenced"), y: silenced } if silenced > 0
|
2018-04-16 19:03:43 +08:00
|
|
|
end
|
2018-04-20 00:19:21 +08:00
|
|
|
|
2018-05-15 13:08:23 +08:00
|
|
|
def self.report_top_referred_topics(report)
|
|
|
|
report.labels = [I18n.t("reports.top_referred_topics.xaxis"),
|
|
|
|
I18n.t("reports.top_referred_topics.num_clicks")]
|
|
|
|
result = IncomingLinksReport.find(:top_referred_topics, start_date: 7.days.ago, limit: report.limit)
|
|
|
|
report.data = result.data
|
|
|
|
end
|
|
|
|
|
2018-04-20 00:19:21 +08:00
|
|
|
def self.report_trending_search(report)
|
|
|
|
report.data = []
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
select_sql = <<~SQL
|
2018-05-15 13:08:23 +08:00
|
|
|
lower(term) term,
|
2018-05-11 11:30:21 +08:00
|
|
|
COUNT(*) AS searches,
|
|
|
|
SUM(CASE
|
2018-04-20 00:19:21 +08:00
|
|
|
WHEN search_result_id IS NOT NULL THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END) AS click_through,
|
2018-05-11 11:30:21 +08:00
|
|
|
COUNT(DISTINCT ip_address) AS unique_searches
|
|
|
|
SQL
|
|
|
|
|
|
|
|
trends = SearchLog.select(select_sql)
|
2018-04-20 00:19:21 +08:00
|
|
|
.where('created_at > ? AND created_at <= ?', report.start_date, report.end_date)
|
2018-05-15 13:08:23 +08:00
|
|
|
.group('lower(term)')
|
2018-05-11 11:30:21 +08:00
|
|
|
.order('unique_searches DESC, click_through ASC, term ASC')
|
2018-05-15 13:08:23 +08:00
|
|
|
.limit(report.limit || 20).to_a
|
2018-04-20 00:19:21 +08:00
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
report.labels = [:term, :searches, :click_through].map { |key|
|
2018-04-20 00:26:30 +08:00
|
|
|
I18n.t("reports.trending_search.labels.#{key}")
|
|
|
|
}
|
2018-04-20 00:19:21 +08:00
|
|
|
|
|
|
|
trends.each do |trend|
|
2018-05-11 11:30:21 +08:00
|
|
|
ctr =
|
2018-05-14 10:01:57 +08:00
|
|
|
if trend.click_through == 0 || trend.searches == 0
|
2018-05-11 11:30:21 +08:00
|
|
|
0
|
|
|
|
else
|
|
|
|
trend.click_through.to_f / trend.searches.to_f
|
|
|
|
end
|
|
|
|
|
2018-05-15 13:08:23 +08:00
|
|
|
report.data << {
|
|
|
|
term: trend.term,
|
|
|
|
unique_searches: trend.unique_searches,
|
|
|
|
ctr: (ctr * 100).ceil(1).to_s + "%"
|
|
|
|
}
|
2018-04-20 00:19:21 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|