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 class Admin::ReportsController < Admin::AdminController
def show def show
report_type = params[:type] report_type = params[:type]
raise Discourse::NotFound.new unless report_type =~ /^[a-z0-9\_]+$/ 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? raise Discourse::NotFound.new if report.blank?
render_json_dump(report: report) render_json_dump(report: report)

View file

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

View file

@ -2,39 +2,51 @@ require_dependency 'topic_subtype'
class Report 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) def initialize(type)
@type = type @type = type
@data = nil @data = nil
@total = nil @total = nil
@prev30Days = nil @prev30Days = nil
@start_date ||= 1.month.ago
@end_date ||= Time.now
end end
def as_json(options = nil) def as_json(options = nil)
{ {
type: self.type, type: type,
title: I18n.t("reports.#{self.type}.title"), title: I18n.t("reports.#{type}.title"),
xaxis: I18n.t("reports.#{self.type}.xaxis"), xaxis: I18n.t("reports.#{type}.xaxis"),
yaxis: I18n.t("reports.#{self.type}.yaxis"), yaxis: I18n.t("reports.#{type}.yaxis"),
data: self.data, data: data,
total: self.total, total: total,
start_date: start_date,
end_date: end_date,
prev30Days: self.prev30Days prev30Days: self.prev30Days
} }
end end
def self.find(type, opts={}) def self.find(type, opts=nil)
opts ||= {}
report_method = :"report_#{type}" report_method = :"report_#{type}"
return nil unless respond_to?(report_method) return nil unless respond_to?(report_method)
# Load the report # Load the report
report = Report.new(type) report = Report.new(type)
report.start_date = opts[:start_date]
report.end_date = opts[:end_date]
send(report_method, report) send(report_method, report)
report report
end end
def self.report_visits(report) 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 end
def self.report_signups(report) def self.report_signups(report)
@ -42,15 +54,15 @@ class Report
end end
def self.report_topics(report) 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.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 end
def self.report_posts(report) 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.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 end
def self.report_emails(report) def self.report_emails(report)
@ -58,20 +70,20 @@ class Report
end end
def self.report_about(report, subject_class, report_method = :count_per_day) 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) add_counts(report, subject_class)
end end
def self.basic_report_about(report, subject_class, report_method, *args) def self.basic_report_about(report, subject_class, report_method, *args)
report.data = [] 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} report.data << {x: date, y: count}
end end
end end
def self.add_counts(report, subject_class) def self.add_counts(report, subject_class)
report.total = subject_class.count 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 end
def self.report_users_by_trust_level(report) def self.report_users_by_trust_level(report)
@ -82,10 +94,10 @@ class Report
end end
def self.report_starred(report) 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) query = TopicUser.where(starred: true)
report.total = query.count 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 end
# Post action counts: # Post action counts:
@ -102,7 +114,7 @@ class Report
end end
flagsQuery = PostAction.where(post_action_type_id: PostActionType.flag_types.values) flagsQuery = PostAction.where(post_action_type_id: PostActionType.flag_types.values)
report.total = flagsQuery.count 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 end
def self.report_likes(report) def self.report_likes(report)
@ -120,15 +132,15 @@ class Report
end end
query = PostAction.unscoped.where(post_action_type_id: post_action_type) query = PostAction.unscoped.where(post_action_type_id: post_action_type)
report.total = query.count 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 end
# Private messages counts: # Private messages counts:
def self.private_messages_report(report, topic_subtype) 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.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 end
def self.report_user_to_user_private_messages(report) def self.report_user_to_user_private_messages(report)

View file

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

View file

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