From 50b04b22097e5597d23a64a48e1ba8fe01de0c4d Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Sun, 17 Mar 2013 15:08:38 -0400 Subject: [PATCH] Add email counts to admin dashboard --- .../admin/templates/dashboard.js.handlebars | 1 + .../admin/views/report/admin_report_emails_view.js | 4 ++++ app/controllers/admin/dashboard_controller.rb | 2 +- app/models/email_log.rb | 4 ++++ app/models/report.rb | 8 ++++++++ config/locales/server.en.yml | 4 ++++ spec/fabricators/email_log_fabricator.rb | 5 +++++ spec/models/report_spec.rb | 11 +++++++++-- 8 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/admin/views/report/admin_report_emails_view.js create mode 100644 spec/fabricators/email_log_fabricator.rb diff --git a/app/assets/javascripts/admin/templates/dashboard.js.handlebars b/app/assets/javascripts/admin/templates/dashboard.js.handlebars index e559bcd76..0a221cbf0 100644 --- a/app/assets/javascripts/admin/templates/dashboard.js.handlebars +++ b/app/assets/javascripts/admin/templates/dashboard.js.handlebars @@ -79,6 +79,7 @@ {{ render 'admin_report_posts' posts }} {{ render 'admin_report_likes' likes }} {{ render 'admin_report_flags' flags }} + {{ render 'admin_report_emails' emails }} {{/unless}} diff --git a/app/assets/javascripts/admin/views/report/admin_report_emails_view.js b/app/assets/javascripts/admin/views/report/admin_report_emails_view.js new file mode 100644 index 000000000..8c100f9a6 --- /dev/null +++ b/app/assets/javascripts/admin/views/report/admin_report_emails_view.js @@ -0,0 +1,4 @@ +Discourse.AdminReportEmailsView = Discourse.View.extend({ + templateName: 'admin/templates/reports/summed_counts_report', + tagName: 'tbody' +}); \ No newline at end of file diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 4f1b0b58e..ce9b26913 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -3,7 +3,7 @@ class Admin::DashboardController < Admin::AdminController def index render_json_dump({ - reports: ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes'].map { |type| Report.find(type) }, + reports: ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails'].map { |type| Report.find(type) }, total_users: User.count }.merge( SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {} diff --git a/app/models/email_log.rb b/app/models/email_log.rb index e324bedbe..9c8e052fd 100644 --- a/app/models/email_log.rb +++ b/app/models/email_log.rb @@ -7,4 +7,8 @@ class EmailLog < ActiveRecord::Base # Update last_emailed_at if the user_id is present 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 + end end diff --git a/app/models/report.rb b/app/models/report.rb index 49ab1eb5f..7c2b2c4e3 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -98,6 +98,14 @@ class Report end end + def self.report_emails(report) + report.data = [] + fetch report do + EmailLog.count_per_day(30.days.ago).each do |date, count| + report.data << {x: date, y: count} + end + end + end private diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ae4234032..41114ccda 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -279,6 +279,10 @@ en: title: "Users per Trust Level" xaxis: "Trust Level" yaxis: "Number of Users" + emails: + title: "Emails Sent" + xaxis: "Day" + yaxis: "Number of Emails" site_settings: default_locale: "The default language of this Discourse instance (ISO 639-1 Code)" diff --git a/spec/fabricators/email_log_fabricator.rb b/spec/fabricators/email_log_fabricator.rb new file mode 100644 index 000000000..58ae914cf --- /dev/null +++ b/spec/fabricators/email_log_fabricator.rb @@ -0,0 +1,5 @@ +Fabricator(:email_log,) do + user + to_address { sequence(:address) { |i| "blah#{i}@example.com" } } + email_type :invite +end \ No newline at end of file diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 6de5acb2b..0c73735be 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -26,7 +26,7 @@ # end # end -# [:signup, :topic, :post, :flag, :like].each do |arg| +# [:signup, :topic, :post, :flag, :like, :email].each do |arg| # describe "#{arg} report" do # pluralized = arg.to_s.pluralize @@ -40,7 +40,14 @@ # context "with #{pluralized}" do # before do -# fabricator = (arg == :signup ? :user : arg) +# 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)