2013-04-16 16:56:18 -04:00
|
|
|
require_dependency 'topic_subtype'
|
|
|
|
|
2013-02-27 22:39:42 -05:00
|
|
|
class Report
|
|
|
|
|
2016-02-02 21:29:51 -05:00
|
|
|
attr_accessor :type, :data, :total, :prev30Days, :start_date, :end_date, :category_id, :group_id
|
2014-11-04 17:08:39 -05:00
|
|
|
|
|
|
|
def self.default_days
|
|
|
|
30
|
|
|
|
end
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
|
|
def initialize(type)
|
|
|
|
@type = type
|
2015-02-06 17:20:11 -05:00
|
|
|
@start_date ||= 1.month.ago.beginning_of_day
|
|
|
|
@end_date ||= Time.zone.now.end_of_day
|
2013-02-27 22:39:42 -05:00
|
|
|
end
|
|
|
|
|
2015-06-24 09:19:39 -04:00
|
|
|
def as_json(options=nil)
|
2013-02-27 22:39:42 -05:00
|
|
|
{
|
2014-11-04 17:08:39 -05: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,
|
2015-06-24 09:19:39 -04:00
|
|
|
category_id: category_id,
|
2016-02-02 21:29:51 -05:00
|
|
|
group_id: group_id,
|
2013-03-30 14:07:25 -04:00
|
|
|
prev30Days: self.prev30Days
|
2013-02-27 22:39:42 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-24 20:42:08 -04:00
|
|
|
def Report.add_report(name, &block)
|
|
|
|
singleton_class.instance_eval { define_method("report_#{name}", &block) }
|
|
|
|
end
|
|
|
|
|
2014-11-04 17:08:39 -05:00
|
|
|
def self.find(type, opts=nil)
|
|
|
|
opts ||= {}
|
2015-10-19 16:30:34 -04:00
|
|
|
|
2013-02-27 22:39:42 -05:00
|
|
|
# Load the report
|
|
|
|
report = Report.new(type)
|
2014-11-05 13:11:23 -05:00
|
|
|
report.start_date = opts[:start_date] if opts[:start_date]
|
|
|
|
report.end_date = opts[:end_date] if opts[:end_date]
|
2015-06-24 09:19:39 -04:00
|
|
|
report.category_id = opts[:category_id] if opts[:category_id]
|
2016-02-02 21:29:51 -05:00
|
|
|
report.group_id = opts[:group_id] if opts[:group_id]
|
2015-02-05 00:08:52 -05:00
|
|
|
report_method = :"report_#{type}"
|
|
|
|
|
|
|
|
if respond_to?(report_method)
|
|
|
|
send(report_method, report)
|
|
|
|
elsif type =~ /_reqs$/
|
|
|
|
req_report(report, type.split(/_reqs$/)[0].to_sym)
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2015-06-24 09:19:39 -04:00
|
|
|
|
2013-02-27 22:39:42 -05:00
|
|
|
report
|
|
|
|
end
|
|
|
|
|
2015-02-04 19:18:11 -05:00
|
|
|
def self.req_report(report, filter=nil)
|
2015-02-05 22:39:04 -05:00
|
|
|
data =
|
|
|
|
if filter == :page_view_total
|
|
|
|
ApplicationRequest.where(req_type: [
|
2015-07-03 17:02:57 -04:00
|
|
|
ApplicationRequest.req_types.reject{|k,v| k =~ /mobile/}.map{|k,v| v if k =~ /page_view/}.compact
|
2015-09-23 01:24:30 -04:00
|
|
|
].flatten)
|
2015-02-05 22:39:04 -05:00
|
|
|
else
|
|
|
|
ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
|
|
|
|
end
|
2015-02-04 19:18:11 -05:00
|
|
|
|
|
|
|
report.data = []
|
2016-05-09 03:46:09 -04:00
|
|
|
data.where('date >= ? AND date <= ?', report.start_date.to_date, report.end_date.to_date)
|
2015-07-06 14:57:11 -04:00
|
|
|
.order(date: :asc)
|
2015-04-12 13:46:13 -04:00
|
|
|
.group(:date)
|
2015-02-04 19:18:11 -05:00
|
|
|
.sum(:count)
|
|
|
|
.each do |date, count|
|
2015-06-24 09:19:39 -04:00
|
|
|
report.data << { x: date, y: count }
|
2015-02-04 19:18:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
report.total = data.sum(:count)
|
2016-05-09 03:46:09 -04:00
|
|
|
report.prev30Days = data.where('date >= ? AND date <= ?',
|
2015-07-06 14:57:11 -04:00
|
|
|
(report.start_date - 31.days).to_date,
|
|
|
|
(report.end_date - 31.days).to_date )
|
|
|
|
.sum(:count)
|
2015-02-04 19:18:11 -05:00
|
|
|
end
|
|
|
|
|
2013-02-27 22:39:42 -05:00
|
|
|
def self.report_visits(report)
|
2016-02-02 21:29:51 -05:00
|
|
|
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date, report.group_id
|
|
|
|
|
2015-06-15 13:16:23 -04:00
|
|
|
add_counts report, UserVisit, 'visited_at'
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2015-07-07 12:31:07 -04: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-07 11:07:59 -05:00
|
|
|
def self.report_signups(report)
|
2016-02-02 21:29:51 -05: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
|
|
|
|
report_about report, User.real, :count_by_signup_date
|
|
|
|
end
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2015-09-14 13:30:06 -04:00
|
|
|
def self.report_profile_views(report)
|
|
|
|
start_date = report.start_date.to_date
|
|
|
|
end_date = report.end_date.to_date
|
2016-02-02 21:29:51 -05:00
|
|
|
basic_report_about report, UserProfileView, :profile_views_by_day, start_date, end_date, report.group_id
|
|
|
|
|
2015-09-14 13:30:06 -04: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-07 11:07:59 -05:00
|
|
|
def self.report_topics(report)
|
2015-06-24 09:19:39 -04: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-27 22:39:42 -05:00
|
|
|
end
|
|
|
|
|
2013-03-07 11:07:59 -05:00
|
|
|
def self.report_posts(report)
|
2015-06-24 09:19:39 -04:00
|
|
|
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date, report.category_id
|
2017-02-21 14:45:34 -05:00
|
|
|
countable = Post.public_posts.where(post_type: Post.types[:regular])
|
2015-06-24 09:19:39 -04: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-03 13:25:52 -04:00
|
|
|
end
|
|
|
|
|
2015-06-22 13:46:51 -04:00
|
|
|
def self.report_time_to_first_response(report)
|
|
|
|
report.data = []
|
2015-09-14 13:36:41 -04:00
|
|
|
Topic.time_to_first_response_per_day(report.start_date, report.end_date, {category_id: report.category_id}).each do |r|
|
2015-06-22 13:46:51 -04:00
|
|
|
report.data << { x: Date.parse(r["date"]), y: r["hours"].to_f.round(2) }
|
|
|
|
end
|
2015-06-24 09:19:39 -04: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-22 13:46:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.report_topics_with_no_response(report)
|
2015-06-25 18:45:11 -04:00
|
|
|
report.data = []
|
|
|
|
Topic.with_no_response_per_day(report.start_date, report.end_date, report.category_id).each do |r|
|
2015-06-30 15:42:38 -04:00
|
|
|
report.data << { x: Date.parse(r["date"]), y: r["count"].to_i }
|
2015-06-25 18:45:11 -04:00
|
|
|
end
|
2015-06-24 09:19:39 -04: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-22 13:46:51 -04:00
|
|
|
end
|
|
|
|
|
2013-04-01 09:21:34 -04: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-05 13:11:23 -05:00
|
|
|
basic_report_about report, subject_class, report_method, report.start_date, report.end_date
|
2014-12-30 09:06:15 -05:00
|
|
|
add_counts report, subject_class
|
2013-04-01 09:21:34 -04:00
|
|
|
end
|
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
def self.basic_report_about(report, subject_class, report_method, *args)
|
2013-03-07 11:07:59 -05:00
|
|
|
report.data = []
|
2014-11-04 17:08:39 -05:00
|
|
|
subject_class.send(report_method, *args).each do |date, count|
|
2015-06-24 09:19:39 -04:00
|
|
|
report.data << { x: date, y: count }
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
2013-04-01 09:21:34 -04:00
|
|
|
end
|
|
|
|
|
2014-12-30 09:06:15 -05:00
|
|
|
def self.add_counts(report, subject_class, query_column = 'created_at')
|
2013-04-01 09:21:34 -04:00
|
|
|
report.total = subject_class.count
|
2014-12-30 09:06:15 -05:00
|
|
|
report.prev30Days = subject_class.where("#{query_column} >= ? and #{query_column} < ?", report.start_date - 30.days, report.start_date).count
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2013-04-18 14:27:22 -04:00
|
|
|
def self.report_users_by_trust_level(report)
|
|
|
|
report.data = []
|
2013-09-06 00:07:23 -04:00
|
|
|
User.real.group('trust_level').count.each do |level, count|
|
2015-06-24 09:19:39 -04:00
|
|
|
report.data << { x: level.to_i, y: count }
|
2013-04-18 14:27:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Post action counts:
|
2013-03-12 14:19:01 -04:00
|
|
|
def self.report_flags(report)
|
2015-06-24 09:19:39 -04:00
|
|
|
basic_report_about report, PostAction, :flag_count_by_date, report.start_date, report.end_date, report.category_id
|
|
|
|
countable = PostAction.where(post_action_type_id: PostActionType.flag_types.values)
|
|
|
|
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-12 14:19:01 -04:00
|
|
|
end
|
|
|
|
|
2013-04-18 14:27:22 -04:00
|
|
|
def self.report_likes(report)
|
|
|
|
post_action_report report, PostActionType.types[:like]
|
2013-03-15 18:08:46 -04:00
|
|
|
end
|
|
|
|
|
2013-04-18 14:27:22 -04: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-17 13:53:00 -04:00
|
|
|
report.data = []
|
2015-10-19 16:30:34 -04: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-28 13:17:37 -04:00
|
|
|
report.data << { x: date, y: count }
|
2013-03-17 13:53:00 -04:00
|
|
|
end
|
2015-06-24 09:19:39 -04: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-17 13:53:00 -04:00
|
|
|
end
|
2013-04-16 16:56:18 -04:00
|
|
|
|
2013-04-18 14:27:22 -04:00
|
|
|
# Private messages counts:
|
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
def self.private_messages_report(report, topic_subtype)
|
2016-04-21 05:22:41 -04:00
|
|
|
basic_report_about report, Post, :private_messages_count_per_day, report.start_date, report.end_date, topic_subtype
|
2014-12-30 09:06:15 -05:00
|
|
|
add_counts report, Post.private_posts.with_topic_subtype(topic_subtype), 'posts.created_at'
|
2013-04-16 16:56:18 -04: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-27 22:39:42 -05:00
|
|
|
end
|