2013-04-17 04:56:18 +08:00
|
|
|
require_dependency 'topic_subtype'
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
class Report
|
|
|
|
|
2014-11-05 06:08:39 +08:00
|
|
|
attr_accessor :type, :data, :total, :prev30Days, :start_date, :end_date
|
|
|
|
|
|
|
|
def self.default_days
|
|
|
|
30
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
|
|
def initialize(type)
|
|
|
|
@type = type
|
|
|
|
@data = nil
|
2013-03-22 01:27:53 +08:00
|
|
|
@total = nil
|
2013-03-31 02:07:25 +08:00
|
|
|
@prev30Days = nil
|
2014-11-05 06:08:39 +08:00
|
|
|
@start_date ||= 1.month.ago
|
|
|
|
@end_date ||= Time.now
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2013-11-30 13:43:44 +08:00
|
|
|
def as_json(options = nil)
|
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"),
|
|
|
|
data: data,
|
|
|
|
total: total,
|
|
|
|
start_date: start_date,
|
|
|
|
end_date: end_date,
|
2013-03-31 02:07:25 +08:00
|
|
|
prev30Days: self.prev30Days
|
2013-02-28 11:39:42 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-11-05 06:08:39 +08:00
|
|
|
def self.find(type, opts=nil)
|
|
|
|
opts ||= {}
|
2013-02-28 11:39:42 +08:00
|
|
|
report_method = :"report_#{type}"
|
|
|
|
return nil unless respond_to?(report_method)
|
|
|
|
|
|
|
|
# Load the report
|
|
|
|
report = Report.new(type)
|
2014-11-05 06:08:39 +08:00
|
|
|
|
2014-11-06 02:11:23 +08:00
|
|
|
report.start_date = opts[:start_date] if opts[:start_date]
|
|
|
|
report.end_date = opts[:end_date] if opts[:end_date]
|
2013-02-28 11:39:42 +08:00
|
|
|
send(report_method, report)
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_visits(report)
|
2014-11-05 06:08:39 +08:00
|
|
|
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_signups(report)
|
2013-09-06 12:07:23 +08:00
|
|
|
report_about report, User.real, :count_by_signup_date
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_topics(report)
|
2014-11-05 06:08:39 +08:00
|
|
|
basic_report_about report, Topic, :listable_count_per_day, report.start_date, report.end_date
|
2013-04-06 02:09:16 +08:00
|
|
|
report.total = Topic.listable_topics.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = Topic.listable_topics.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_posts(report)
|
2014-11-05 06:08:39 +08:00
|
|
|
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date
|
2013-04-06 02:09:16 +08:00
|
|
|
report.total = Post.public_posts.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = Post.public_posts.where('posts.created_at > ? and posts.created_at < ?', report.start_date - 30.days, report.start_date).count
|
2013-04-04 01:25:52 +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
|
2013-04-01 21:21:34 +08:00
|
|
|
add_counts(report, subject_class)
|
|
|
|
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 = []
|
2014-11-05 06:08:39 +08:00
|
|
|
subject_class.send(report_method, *args).each do |date, count|
|
2013-03-21 01:54:32 +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
|
|
|
|
|
|
|
|
def self.add_counts(report, subject_class)
|
|
|
|
report.total = subject_class.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = subject_class.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
|
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 = []
|
2013-09-06 12:07:23 +08:00
|
|
|
User.real.group('trust_level').count.each do |level, count|
|
2013-04-19 02:27:22 +08:00
|
|
|
report.data << {x: level.to_i, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-10 05:22:54 +08:00
|
|
|
def self.report_starred(report)
|
2014-11-05 06:08:39 +08:00
|
|
|
basic_report_about report, Topic, :starred_counts_per_day, default_days
|
2013-04-19 02:27:22 +08:00
|
|
|
query = TopicUser.where(starred: true)
|
|
|
|
report.total = query.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = query.where('starred_at > ? and starred_at < ?', report.start_date - 30.days, report.start_date).count
|
2013-04-19 02:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Post action counts:
|
|
|
|
|
2013-03-13 02:19:01 +08:00
|
|
|
def self.report_flags(report)
|
|
|
|
report.data = []
|
2013-03-21 01:54:32 +08:00
|
|
|
(0..30).to_a.reverse.each do |i|
|
2013-04-04 10:29:54 +08:00
|
|
|
count = PostAction.where('date(created_at) = ?', i.days.ago.utc.to_date)
|
|
|
|
.where(post_action_type_id: PostActionType.flag_types.values)
|
|
|
|
.count
|
|
|
|
if count > 0
|
|
|
|
report.data << {x: i.days.ago.utc.to_date.to_s, y: count}
|
2013-03-13 02:19:01 +08:00
|
|
|
end
|
|
|
|
end
|
2013-03-31 02:07:25 +08:00
|
|
|
flagsQuery = PostAction.where(post_action_type_id: PostActionType.flag_types.values)
|
|
|
|
report.total = flagsQuery.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = flagsQuery.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
|
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 = []
|
2014-07-29 01:17:37 +08:00
|
|
|
PostAction.count_per_day_for_type(post_action_type).each do |date, count|
|
|
|
|
report.data << { x: date, y: count }
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
2013-05-01 00:52:12 +08:00
|
|
|
query = PostAction.unscoped.where(post_action_type_id: post_action_type)
|
2013-04-19 02:27:22 +08:00
|
|
|
report.total = query.count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = query.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
|
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)
|
2014-11-05 06:08:39 +08:00
|
|
|
basic_report_about report, Post, :private_messages_count_per_day, default_days, topic_subtype
|
2013-04-17 04:56:18 +08:00
|
|
|
report.total = Post.private_posts.with_topic_subtype(topic_subtype).count
|
2014-11-05 06:08:39 +08:00
|
|
|
report.prev30Days = Post.private_posts.with_topic_subtype(topic_subtype).where('posts.created_at > ? and posts.created_at < ?', report.start_date - 30.days, report.start_date).count
|
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
|
|
|
|
|
|
|
|
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
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|