Work in progress (up till about?)

This commit is contained in:
Robin Ward 2014-11-04 17:08:39 -05:00
parent acfd99ebf2
commit fde5e739c9
5 changed files with 47 additions and 30 deletions

View File

@ -3,12 +3,17 @@ require_dependency 'report'
class Admin::ReportsController < Admin::AdminController
def show
report_type = params[:type]
raise Discourse::NotFound.new unless report_type =~ /^[a-z0-9\_]+$/
report = Report.find(report_type)
start_date = 1.month.ago
start_date = Time.parse(params[:start_date]) if params[:start_date].present?
end_date = start_date + 1.month
end_date = Time.parse(params[:end_date]) if params[:end_date].present?
report = Report.find(report_type, {start_date: start_date, end_date: end_date})
raise Discourse::NotFound.new if report.blank?
render_json_dump(report: report)

View File

@ -460,8 +460,8 @@ class Post < ActiveRecord::Base
Jobs.enqueue(:process_post, args)
end
def self.public_posts_count_per_day(since_days_ago=30)
public_posts.where('posts.created_at > ?', since_days_ago.days.ago).group('date(posts.created_at)').order('date(posts.created_at)').count
def self.public_posts_count_per_day(start_date, end_date)
public_posts.where('posts.created_at >= ? AND posts.created_at <= ?', start_date, end_date).group('date(posts.created_at)').order('date(posts.created_at)').count
end
def self.private_messages_count_per_day(since_days_ago, topic_subtype)

View File

@ -2,39 +2,51 @@ require_dependency 'topic_subtype'
class Report
attr_accessor :type, :data, :total, :prev30Days
attr_accessor :type, :data, :total, :prev30Days, :start_date, :end_date
def self.default_days
30
end
def initialize(type)
@type = type
@data = nil
@total = nil
@prev30Days = nil
@start_date ||= 1.month.ago
@end_date ||= Time.now
end
def as_json(options = nil)
{
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,
total: self.total,
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,
prev30Days: self.prev30Days
}
end
def self.find(type, opts={})
def self.find(type, opts=nil)
opts ||= {}
report_method = :"report_#{type}"
return nil unless respond_to?(report_method)
# Load the report
report = Report.new(type)
report.start_date = opts[:start_date]
report.end_date = opts[:end_date]
send(report_method, report)
report
end
def self.report_visits(report)
basic_report_about report, UserVisit, :by_day
basic_report_about report, UserVisit, :by_day, report.start_date, report.end_date
end
def self.report_signups(report)
@ -42,15 +54,15 @@ class Report
end
def self.report_topics(report)
basic_report_about report, Topic, :listable_count_per_day
basic_report_about report, Topic, :listable_count_per_day, report.start_date, report.end_date
report.total = Topic.listable_topics.count
report.prev30Days = Topic.listable_topics.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
report.prev30Days = Topic.listable_topics.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
end
def self.report_posts(report)
basic_report_about report, Post, :public_posts_count_per_day
basic_report_about report, Post, :public_posts_count_per_day, report.start_date, report.end_date
report.total = Post.public_posts.count
report.prev30Days = Post.public_posts.where('posts.created_at > ? and posts.created_at < ?', 60.days.ago, 30.days.ago).count
report.prev30Days = Post.public_posts.where('posts.created_at > ? and posts.created_at < ?', report.start_date - 30.days, report.start_date).count
end
def self.report_emails(report)
@ -58,20 +70,20 @@ class Report
end
def self.report_about(report, subject_class, report_method = :count_per_day)
basic_report_about report, subject_class, report_method
basic_report_about report, subject_class, report_method, default_days
add_counts(report, subject_class)
end
def self.basic_report_about(report, subject_class, report_method, *args)
report.data = []
subject_class.send(report_method, 30, *args).each do |date, count|
subject_class.send(report_method, *args).each do |date, count|
report.data << {x: date, y: count}
end
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
report.prev30Days = subject_class.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
end
def self.report_users_by_trust_level(report)
@ -82,10 +94,10 @@ class Report
end
def self.report_starred(report)
basic_report_about report, Topic, :starred_counts_per_day
basic_report_about report, Topic, :starred_counts_per_day, default_days
query = TopicUser.where(starred: true)
report.total = query.count
report.prev30Days = query.where('starred_at > ? and starred_at < ?', 60.days.ago, 30.days.ago).count
report.prev30Days = query.where('starred_at > ? and starred_at < ?', report.start_date - 30.days, report.start_date).count
end
# Post action counts:
@ -102,7 +114,7 @@ class Report
end
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
report.prev30Days = flagsQuery.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
end
def self.report_likes(report)
@ -120,15 +132,15 @@ class Report
end
query = PostAction.unscoped.where(post_action_type_id: post_action_type)
report.total = query.count
report.prev30Days = query.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
report.prev30Days = query.where('created_at > ? and created_at < ?', report.start_date - 30.days, report.start_date).count
end
# Private messages counts:
def self.private_messages_report(report, topic_subtype)
basic_report_about report, Post, :private_messages_count_per_day, topic_subtype
basic_report_about report, Post, :private_messages_count_per_day, default_days, topic_subtype
report.total = Post.private_posts.with_topic_subtype(topic_subtype).count
report.prev30Days = Post.private_posts.with_topic_subtype(topic_subtype).where('posts.created_at > ? and posts.created_at < ?', 60.days.ago, 30.days.ago).count
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
end
def self.report_user_to_user_private_messages(report)

View File

@ -345,8 +345,8 @@ class Topic < ActiveRecord::Base
custom_fields[key.to_s]
end
def self.listable_count_per_day(sinceDaysAgo=30)
listable_topics.where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
def self.listable_count_per_day(start_date, end_date)
listable_topics.where('created_at >= ? and created_at < ?', start_date, end_date).group('date(created_at)').order('date(created_at)').count
end
def private_message?

View File

@ -1,8 +1,8 @@
class UserVisit < ActiveRecord::Base
# A count of visits in the last month by day
def self.by_day(sinceDaysAgo=30)
where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count
def self.by_day(start_date, end_date)
where("visited_at >= ? and visited_at < ?", start_date, end_date).group(:visited_at).order(:visited_at).count
end
def self.ensure_consistency!