Dashboard: split out private messages from topic and post counts; re-enable report_spec because I think I fixed it...

This commit is contained in:
Neil Lalonde 2013-04-03 13:25:52 -04:00
parent 738789f336
commit bb18b6cb9b
12 changed files with 149 additions and 66 deletions

View File

@ -113,6 +113,7 @@
{{ render 'admin_report_counts' likes }} {{ render 'admin_report_counts' likes }}
{{ render 'admin_report_counts' flags }} {{ render 'admin_report_counts' flags }}
{{ render 'admin_report_counts' emails }} {{ render 'admin_report_counts' emails }}
{{ render 'admin_report_counts' private_messages }}
{{/unless}} {{/unless}}
</table> </table>
</div> </div>

View File

@ -2,7 +2,7 @@ require_dependency 'mem_info'
class AdminDashboardData class AdminDashboardData
REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails'] REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails', 'private_messages']
def self.fetch_all def self.fetch_all
AdminDashboardData.new AdminDashboardData.new

View File

@ -8,7 +8,7 @@ class EmailLog < ActiveRecord::Base
User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present? User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present?
end end
def self.count_per_day(since = 30.days.ago) def self.count_per_day(sinceDaysAgo = 30)
where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
end end
end end

View File

@ -390,7 +390,11 @@ class Post < ActiveRecord::Base
Jobs.enqueue(:process_post, args) Jobs.enqueue(:process_post, args)
end end
def self.count_per_day(since=30.days.ago) def self.public_posts_count_per_day(sinceDaysAgo=30)
where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count joins(:topic).where('topics.archetype <> ?', [Archetype.private_message]).where('posts.created_at > ?', sinceDaysAgo.days.ago).group('date(posts.created_at)').order('date(posts.created_at)').count
end
def self.private_messages_count_per_day(sinceDaysAgo=30)
joins(:topic).where('topics.archetype = ?', Archetype.private_message).where('posts.created_at > ?', sinceDaysAgo.days.ago).group('date(posts.created_at)').order('date(posts.created_at)').count
end end
end end

View File

@ -50,8 +50,8 @@ class PostAction < ActiveRecord::Base
user_actions user_actions
end end
def self.count_likes_per_day(since = 30.days.ago) def self.count_likes_per_day(sinceDaysAgo = 30)
where(post_action_type_id: PostActionType.types[:like]).where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count where(post_action_type_id: PostActionType.types[:like]).where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
end end
def self.clear_flags!(post, moderator_id, action_type_id = nil) def self.clear_flags!(post, moderator_id, action_type_id = nil)

View File

@ -40,11 +40,15 @@ class Report
end end
def self.report_topics(report) def self.report_topics(report)
report_about report, Topic report_about report, Topic, :listable_count_per_day
end end
def self.report_posts(report) def self.report_posts(report)
report_about report, Post 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
end end
def self.report_emails(report) def self.report_emails(report)
@ -58,7 +62,7 @@ class Report
def self.basic_report_about(report, subject_class, report_method) def self.basic_report_about(report, subject_class, report_method)
report.data = [] report.data = []
subject_class.send(report_method, 30.days.ago).each do |date, count| subject_class.send(report_method, 30).each do |date, count|
report.data << {x: date, y: count} report.data << {x: date, y: count}
end end
end end
@ -89,7 +93,7 @@ class Report
def self.report_likes(report) def self.report_likes(report)
report.data = [] report.data = []
PostAction.count_likes_per_day(30.days.ago).each do |date, count| PostAction.count_likes_per_day(30).each do |date, count|
report.data << {x: date, y: count} report.data << {x: date, y: count}
end end
likesQuery = PostAction.where(post_action_type_id: PostActionType.types[:like]) likesQuery = PostAction.where(post_action_type_id: PostActionType.types[:like])

View File

@ -192,8 +192,8 @@ class Topic < ActiveRecord::Base
where("created_at > ?", time_ago) where("created_at > ?", time_ago)
end end
def self.count_per_day(since=30.days.ago) def self.listable_count_per_day(sinceDaysAgo=30)
where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count listable_topics.where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
end end
def private_message? def private_message?

View File

@ -479,8 +479,8 @@ class User < ActiveRecord::Base
Summarize.new(bio_cooked).summary Summarize.new(bio_cooked).summary
end end
def self.count_by_signup_date(since=30.days.ago) def self.count_by_signup_date(sinceDaysAgo=30)
where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
end end
def self.counts_by_trust_level def self.counts_by_trust_level

View File

@ -2,7 +2,7 @@ class UserVisit < ActiveRecord::Base
attr_accessible :visited_at, :user_id attr_accessible :visited_at, :user_id
# A list of visits in the last month by day # A list of visits in the last month by day
def self.by_day(since=30.days.ago) def self.by_day(sinceDaysAgo=30)
where("visited_at >= ?", since).group(:visited_at).order(:visited_at).count where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count
end end
end end

View File

@ -294,6 +294,10 @@ en:
title: "Emails Sent" title: "Emails Sent"
xaxis: "Day" xaxis: "Day"
yaxis: "Number of Emails" yaxis: "Number of Emails"
private_messages:
title: "Private Messages"
xaxis: "Day"
yaxis: "Number of private messages"
dashboard: dashboard:
rails_env_warning: "Your server is running in %{env} mode." rails_env_warning: "Your server is running in %{env} mode."

View File

@ -61,3 +61,18 @@ And a markdown link: [forumwarz](http://forumwarz.com)
And a markdown link with a period after it [codinghorror](http://www.codinghorror.com/blog). And a markdown link with a period after it [codinghorror](http://www.codinghorror.com/blog).
" "
end end
Fabricator(:private_message_post, from: :post) do
user
topic do |attrs|
Fabricate( :private_message_topic,
user: attrs[:user],
created_at: attrs[:created_at],
topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user_id: attrs[:user].id),
Fabricate.build(:topic_allowed_user, user_id: Fabricate(:user).id)
]
)
end
raw "Ssshh! This is our secret conversation!"
end

View File

@ -2,66 +2,121 @@ require 'spec_helper'
describe Report do describe Report do
# describe 'visits report' do describe 'visits report' do
# let(:report) { Report.find('visits', cache: false) } let(:report) { Report.find('visits') }
# context "no visits" do context "no visits" do
# it "returns an empty report" do it "returns an empty report" do
# report.data.should be_blank report.data.should be_blank
# end end
# end end
# context "with visits" do context "with visits" do
# let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
# before do before do
# user.user_visits.create(visited_at: 1.day.ago) user.user_visits.create(visited_at: 1.day.ago)
# user.user_visits.create(visited_at: 2.days.ago) user.user_visits.create(visited_at: 2.days.ago)
# end end
# it "returns a report with data" do it "returns a report with data" do
# report.data.should be_present report.data.should be_present
# end end
# end end
# end end
# [:signup, :topic, :post, :flag, :like, :email].each do |arg| [:signup, :topic, :post, :flag, :like, :email].each do |arg|
# describe "#{arg} report" do describe "#{arg} report" do
# pluralized = arg.to_s.pluralize pluralized = arg.to_s.pluralize
# let(:report) { Report.find(pluralized, cache: false) } let(:report) { Report.find(pluralized) }
# context "no #{pluralized}" do context "no #{pluralized}" do
# it 'returns an empty report' do it 'returns an empty report' do
# report.data.should be_blank report.data.should be_blank
# end end
# end end
# context "with #{pluralized}" do context "with #{pluralized}" do
# before do before do
# fabricator = case arg fabricator = case arg
# when :signup when :signup
# :user :user
# when :email when :email
# :email_log :email_log
# else else
# arg arg
# end end
# Fabricate(fabricator, created_at: 25.hours.ago) Fabricate(fabricator, created_at: 25.hours.ago)
# Fabricate(fabricator, created_at: 1.hours.ago) Fabricate(fabricator, created_at: 1.hours.ago)
# Fabricate(fabricator, created_at: 1.hours.ago) Fabricate(fabricator, created_at: 1.hours.ago)
# end end
# it 'returns correct data' do it 'returns correct data' do
# report.data[0][:y].should == 1 report.data[0][:y].should == 1
# report.data[1][:y].should == 2 report.data[1][:y].should == 2
# end end
# end end
# end end
# end end
describe 'private messages' do
let(:report) { Report.find('private_messages') }
it 'topic report should not include private messages' do
Fabricate(:private_message_topic, created_at: 1.hour.ago)
Fabricate(:topic, created_at: 1.hour.ago)
report = Report.find('topics')
report.data[0][:y].should == 1
end
it 'post report should not include private messages' do
Fabricate(:private_message_post, created_at: 1.hour.ago)
Fabricate(:post)
report = Report.find('posts')
report.data[0][:y].should == 1
end
context 'no private messages' do
it 'returns an empty report' do
report.data.should be_blank
end
context 'some public posts' do
it 'returns an empty report' do
Fabricate(:post); Fabricate(:post)
report.data.should be_blank
end
end
end
context 'some private messages' do
before do
Fabricate(:private_message_post, created_at: 25.hours.ago)
Fabricate(:private_message_post, created_at: 1.hour.ago)
Fabricate(:private_message_post, created_at: 1.hour.ago)
end
it 'returns correct data' do
report.data[0][:y].should == 1
report.data[1][:y].should == 2
end
context 'and some public posts' do
before do
Fabricate(:post); Fabricate(:post)
end
it 'returns correct data' do
report.data[0][:y].should == 1
report.data[1][:y].should == 2
end
end
end
end
describe 'users by trust level report' do describe 'users by trust level report' do
let(:report) { Report.find('users_by_trust_level', cache: false) } let(:report) { Report.find('users_by_trust_level') }
context "no users" do context "no users" do
it "returns an empty report" do it "returns an empty report" do