From bb18b6cb9bc1d2cda1f8ccab75d63d98c18eefc1 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 3 Apr 2013 13:25:52 -0400 Subject: [PATCH] Dashboard: split out private messages from topic and post counts; re-enable report_spec because I think I fixed it... --- .../admin/templates/dashboard.js.handlebars | 1 + app/models/admin_dashboard_data.rb | 2 +- app/models/email_log.rb | 4 +- app/models/post.rb | 8 +- app/models/post_action.rb | 4 +- app/models/report.rb | 12 +- app/models/topic.rb | 4 +- app/models/user.rb | 4 +- app/models/user_visit.rb | 4 +- config/locales/server.en.yml | 4 + spec/fabricators/post_fabricator.rb | 15 ++ spec/models/report_spec.rb | 153 ++++++++++++------ 12 files changed, 149 insertions(+), 66 deletions(-) diff --git a/app/assets/javascripts/admin/templates/dashboard.js.handlebars b/app/assets/javascripts/admin/templates/dashboard.js.handlebars index e5b441480ae..e22048926c4 100644 --- a/app/assets/javascripts/admin/templates/dashboard.js.handlebars +++ b/app/assets/javascripts/admin/templates/dashboard.js.handlebars @@ -113,6 +113,7 @@ {{ render 'admin_report_counts' likes }} {{ render 'admin_report_counts' flags }} {{ render 'admin_report_counts' emails }} + {{ render 'admin_report_counts' private_messages }} {{/unless}} diff --git a/app/models/admin_dashboard_data.rb b/app/models/admin_dashboard_data.rb index b35ee499819..f81597dc565 100644 --- a/app/models/admin_dashboard_data.rb +++ b/app/models/admin_dashboard_data.rb @@ -2,7 +2,7 @@ require_dependency 'mem_info' 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 AdminDashboardData.new diff --git a/app/models/email_log.rb b/app/models/email_log.rb index 9c8e052fdc8..da1e04fcb9d 100644 --- a/app/models/email_log.rb +++ b/app/models/email_log.rb @@ -8,7 +8,7 @@ class EmailLog < ActiveRecord::Base User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present? end - def self.count_per_day(since = 30.days.ago) - where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count + def self.count_per_day(sinceDaysAgo = 30) + where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count end end diff --git a/app/models/post.rb b/app/models/post.rb index 99ce50dbc3b..0ba7ffb742b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -390,7 +390,11 @@ class Post < ActiveRecord::Base Jobs.enqueue(:process_post, args) end - def self.count_per_day(since=30.days.ago) - where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count + def self.public_posts_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 + + 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 diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 1e9e66f7620..7fe39f6155e 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -50,8 +50,8 @@ class PostAction < ActiveRecord::Base user_actions end - def self.count_likes_per_day(since = 30.days.ago) - where(post_action_type_id: PostActionType.types[:like]).where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count + def self.count_likes_per_day(sinceDaysAgo = 30) + where(post_action_type_id: PostActionType.types[:like]).where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count end def self.clear_flags!(post, moderator_id, action_type_id = nil) diff --git a/app/models/report.rb b/app/models/report.rb index 5382622190b..ad1ba24cbb0 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -40,11 +40,15 @@ class Report end def self.report_topics(report) - report_about report, Topic + report_about report, Topic, :listable_count_per_day end 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 def self.report_emails(report) @@ -58,7 +62,7 @@ class Report def self.basic_report_about(report, subject_class, report_method) 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} end end @@ -89,7 +93,7 @@ class Report def self.report_likes(report) 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} end likesQuery = PostAction.where(post_action_type_id: PostActionType.types[:like]) diff --git a/app/models/topic.rb b/app/models/topic.rb index 83d89785af4..a4a4107c9f6 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -192,8 +192,8 @@ class Topic < ActiveRecord::Base where("created_at > ?", time_ago) end - def self.count_per_day(since=30.days.ago) - where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count + 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 end def private_message? diff --git a/app/models/user.rb b/app/models/user.rb index d43c2b9bda7..99f69581e91 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -479,8 +479,8 @@ class User < ActiveRecord::Base Summarize.new(bio_cooked).summary end - def self.count_by_signup_date(since=30.days.ago) - where('created_at > ?', since).group('date(created_at)').order('date(created_at)').count + def self.count_by_signup_date(sinceDaysAgo=30) + where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count end def self.counts_by_trust_level diff --git a/app/models/user_visit.rb b/app/models/user_visit.rb index f8f7ea43b13..8b13566dbf1 100644 --- a/app/models/user_visit.rb +++ b/app/models/user_visit.rb @@ -2,7 +2,7 @@ class UserVisit < ActiveRecord::Base attr_accessible :visited_at, :user_id # A list of visits in the last month by day - def self.by_day(since=30.days.ago) - where("visited_at >= ?", since).group(:visited_at).order(:visited_at).count + def self.by_day(sinceDaysAgo=30) + where("visited_at >= ?", sinceDaysAgo.days.ago).group(:visited_at).order(:visited_at).count end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index d3bed4eed6d..26022c77c63 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -294,6 +294,10 @@ en: title: "Emails Sent" xaxis: "Day" yaxis: "Number of Emails" + private_messages: + title: "Private Messages" + xaxis: "Day" + yaxis: "Number of private messages" dashboard: rails_env_warning: "Your server is running in %{env} mode." diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index 73f84fb1be2..4b497cbb51f 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -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). " 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 \ No newline at end of file diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 9353fae42da..0285ab98b1e 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -2,66 +2,121 @@ require 'spec_helper' describe Report do - # describe 'visits report' do - # let(:report) { Report.find('visits', cache: false) } + describe 'visits report' do + let(:report) { Report.find('visits') } - # context "no visits" do - # it "returns an empty report" do - # report.data.should be_blank - # end - # end + context "no visits" do + it "returns an empty report" do + report.data.should be_blank + end + end - # context "with visits" do - # let(:user) { Fabricate(:user) } + context "with visits" do + let(:user) { Fabricate(:user) } - # before do - # user.user_visits.create(visited_at: 1.day.ago) - # user.user_visits.create(visited_at: 2.days.ago) - # end + before do + user.user_visits.create(visited_at: 1.day.ago) + user.user_visits.create(visited_at: 2.days.ago) + end - # it "returns a report with data" do - # report.data.should be_present - # end - # end - # end + it "returns a report with data" do + report.data.should be_present + end + end + end - # [:signup, :topic, :post, :flag, :like, :email].each do |arg| - # describe "#{arg} report" do - # pluralized = arg.to_s.pluralize + [:signup, :topic, :post, :flag, :like, :email].each do |arg| + describe "#{arg} report" do + pluralized = arg.to_s.pluralize - # let(:report) { Report.find(pluralized, cache: false) } + let(:report) { Report.find(pluralized) } - # context "no #{pluralized}" do - # it 'returns an empty report' do - # report.data.should be_blank - # end - # end + context "no #{pluralized}" do + it 'returns an empty report' do + report.data.should be_blank + end + end - # context "with #{pluralized}" do - # before do - # fabricator = case arg - # when :signup - # :user - # when :email - # :email_log - # else - # arg - # end - # Fabricate(fabricator, created_at: 25.hours.ago) - # Fabricate(fabricator, created_at: 1.hours.ago) - # Fabricate(fabricator, created_at: 1.hours.ago) - # end + context "with #{pluralized}" do + before do + fabricator = case arg + when :signup + :user + when :email + :email_log + else + arg + end + Fabricate(fabricator, created_at: 25.hours.ago) + Fabricate(fabricator, created_at: 1.hours.ago) + Fabricate(fabricator, created_at: 1.hours.ago) + end - # it 'returns correct data' do - # report.data[0][:y].should == 1 - # report.data[1][:y].should == 2 - # end - # end - # end - # end + it 'returns correct data' do + report.data[0][:y].should == 1 + report.data[1][:y].should == 2 + 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 - let(:report) { Report.find('users_by_trust_level', cache: false) } + let(:report) { Report.find('users_by_trust_level') } context "no users" do it "returns an empty report" do