2013-02-28 11:39:42 +08:00
|
|
|
class Report
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
attr_accessor :type, :data, :cache
|
|
|
|
|
|
|
|
def self.cache_expiry
|
|
|
|
3600 # In seconds
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
|
|
def initialize(type)
|
|
|
|
@type = type
|
|
|
|
@data = nil
|
2013-03-08 00:07:59 +08:00
|
|
|
@cache = true
|
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"),
|
|
|
|
data: self.data
|
|
|
|
}
|
|
|
|
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)
|
2013-03-08 00:07:59 +08:00
|
|
|
report.cache = false if opts[:cache] == false
|
2013-02-28 11:39:42 +08:00
|
|
|
send(report_method, report)
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_visits(report)
|
|
|
|
report.data = []
|
2013-03-08 00:07:59 +08:00
|
|
|
fetch report do
|
|
|
|
UserVisit.by_day(30.days.ago).each do |date, count|
|
|
|
|
report.data << {x: date, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_signups(report)
|
|
|
|
report.data = []
|
|
|
|
fetch report do
|
|
|
|
User.count_by_signup_date(30.days.ago).each do |date, count|
|
|
|
|
report.data << {x: date, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_topics(report)
|
|
|
|
report.data = []
|
|
|
|
fetch report do
|
|
|
|
Topic.count_per_day(30.days.ago).each do |date, count|
|
|
|
|
report.data << {x: date, y: count}
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
def self.report_posts(report)
|
|
|
|
report.data = []
|
|
|
|
fetch report do
|
|
|
|
Post.count_per_day(30.days.ago).each do |date, count|
|
|
|
|
report.data << {x: date, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-13 02:19:01 +08:00
|
|
|
def self.report_flags(report)
|
|
|
|
report.data = []
|
|
|
|
fetch report do
|
|
|
|
(0..30).to_a.reverse.each do |i|
|
|
|
|
if (count = PostAction.where('date(created_at) = ?', i.days.ago.to_date).where(post_action_type_id: PostActionType.flag_types.values).count) > 0
|
|
|
|
report.data << {x: i.days.ago.to_date.to_s, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-16 06:08:46 +08:00
|
|
|
def self.report_users_by_trust_level(report)
|
|
|
|
report.data = []
|
|
|
|
fetch report do
|
|
|
|
User.counts_by_trust_level.each do |level, count|
|
|
|
|
report.data << {x: level.to_i, y: count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-08 00:07:59 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.fetch(report)
|
|
|
|
unless report.cache and $redis
|
|
|
|
yield
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
data_set = "#{report.type}:data"
|
|
|
|
if $redis.exists(data_set)
|
|
|
|
$redis.get(data_set).split('|').each do |pair|
|
|
|
|
date, count = pair.split(',')
|
|
|
|
report.data << {x: date, y: count.to_i}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
$redis.setex data_set, cache_expiry, report.data.map { |item| "#{item[:x]},#{item[:y]}" }.join('|')
|
|
|
|
end
|
|
|
|
rescue Redis::BaseConnectionError
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|