2013-02-28 11:39:42 +08:00
|
|
|
class Report
|
|
|
|
|
2013-03-31 02:07:25 +08:00
|
|
|
attr_accessor :type, :data, :total, :prev30Days
|
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
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_json
|
|
|
|
{
|
|
|
|
type: self.type,
|
|
|
|
title: I18n.t("reports.#{self.type}.title"),
|
|
|
|
xaxis: I18n.t("reports.#{self.type}.xaxis"),
|
|
|
|
yaxis: I18n.t("reports.#{self.type}.yaxis"),
|
2013-03-22 01:27:53 +08:00
|
|
|
data: self.data,
|
2013-03-31 02:07:25 +08:00
|
|
|
total: self.total,
|
|
|
|
prev30Days: self.prev30Days
|
2013-02-28 11:39:42 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.find(type, 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)
|
|
|
|
send(report_method, report)
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_visits(report)
|
2013-04-01 21:21:34 +08:00
|
|
|
basic_report_about report, UserVisit, :by_day
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_signups(report)
|
2013-04-01 21:21:34 +08:00
|
|
|
report_about report, User, :count_by_signup_date
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_topics(report)
|
2013-04-04 01:25:52 +08:00
|
|
|
report_about report, Topic, :listable_count_per_day
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_posts(report)
|
2013-04-04 01:25:52 +08:00
|
|
|
report_about report, Post, :public_posts_count_per_day
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_private_messages(report)
|
|
|
|
report_about report, Post, :private_messages_count_per_day
|
2013-04-01 21:21:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_emails(report)
|
|
|
|
report_about report, EmailLog
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_about(report, subject_class, report_method = :count_per_day)
|
|
|
|
basic_report_about report, subject_class, report_method
|
|
|
|
add_counts(report, subject_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.basic_report_about(report, subject_class, report_method)
|
2013-03-08 00:07:59 +08:00
|
|
|
report.data = []
|
2013-04-04 01:25:52 +08:00
|
|
|
subject_class.send(report_method, 30).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
|
|
|
|
report.prev30Days = subject_class.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
report.prev30Days = flagsQuery.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
|
2013-03-13 02:19:01 +08:00
|
|
|
end
|
|
|
|
|
2013-03-16 06:08:46 +08:00
|
|
|
def self.report_users_by_trust_level(report)
|
|
|
|
report.data = []
|
2013-03-21 01:54:32 +08:00
|
|
|
User.counts_by_trust_level.each do |level, count|
|
|
|
|
report.data << {x: level.to_i, y: count}
|
2013-03-16 06:08:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 01:53:00 +08:00
|
|
|
def self.report_likes(report)
|
|
|
|
report.data = []
|
2013-04-04 01:25:52 +08:00
|
|
|
PostAction.count_likes_per_day(30).each do |date, count|
|
2013-03-21 01:54:32 +08:00
|
|
|
report.data << {x: date, y: count}
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
2013-03-31 02:07:25 +08:00
|
|
|
likesQuery = PostAction.where(post_action_type_id: PostActionType.types[:like])
|
|
|
|
report.total = likesQuery.count
|
|
|
|
report.prev30Days = likesQuery.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|