Permalink
Browse files

Fixed problem with dashboard page and statistics but more specs are n…

…eeded for this object.
  • Loading branch information...
1 parent 16426d0 commit 2efaae57e411d57ab3abb3c2df61d821d972ac08 @bguthrie bguthrie committed Aug 19, 2013
@@ -1,6 +1,6 @@
module DashboardHelper
- delegate :heartbeat, :nps_summary, :npfs_summary, :petition_extremes,
+ delegate :heartbeat, :nps_summary, :npfs_summary, :best_petitions, :worst_petitions,
:nps_chart, :emails_sent_chart, :emails_opened_chart, :emails_clicked_chart, :unsubscribes_chart,
:facebook_actions_chart, :facebook_referrals_chart, :facebook_action_per_signature_chart,
:signatures_from_email_chart, :petition_page_load_chart, :signature_per_petition_page_load_chart,
View
@@ -5,6 +5,10 @@ class Donation < ActiveRecord::Base
attr_accessible :member, :petition, :referral, :amount
validates_presence_of :petition, :member, :referral
+ scope :recent, lambda {
+ where("created_at BETWEEN now() - interval'8 days' AND now() - interval'1 day'")
+ }
+
def self.confirm_payment(amount, hash)
donor = Member.find_by_hash(hash)
donation = Donation.where(:member_id => donor, :amount => nil).last
View
@@ -29,7 +29,8 @@ def heartbeat
end
def average_donations_per_day
- Donation.where("created_at BETWEEN now() - interval'8 days' AND now() - interval'1 day'").group('DATE(created_at)').sum('amount').map { |d| d[1] }.inject(0.0) { |sum, n| sum + n } / 7
+ donations = Donation.recent.group('DATE(created_at)').sum('amount')
+ donations.map { |date, sum| sum }.sum / 7.0
end
def total_donations
@@ -162,26 +163,16 @@ def strip_chart gauge, averaging_window, thresholds=[]
u.strip
end
- def petition_extremes
- @extremes ||= fetch_petition_extremes(extremes_count.to_i, extremes_threshold.to_i)
+ def best_petitions
+ count = self.extremes_count.to_i
+ best_nps = nps_by_timeframe.first(count)
+ Petition.where(id: best_nps.map(&:id)).zip best_nps
end
- def fetch_petition_extremes count, threshold
- timespan = 1.send(timeframe).ago
- threshold = extremes_threshold.to_i
- nps = Metrics::Nps.email_by_timeframe(timespan, sent_threshold: threshold).sort_by(&:nps).reverse
- best = nps.first(count)
- worst = nps.last(count) - best
- {
- best: associate_petitions(best),
- worst: associate_petitions(worst)
- }
- end
-
- def associate_petitions stats
- ids = stats.map &:id
- petitions = Petition.select("id, title").where("id in (?)", ids)
- stats.map {|s| [petitions.find {|p| p.id == s.id}, s]}
+ def worst_petitions
+ count = self.extremes_count.to_i
+ worst_nps = nps_by_timeframe.last(count) - nps_by_timeframe.first(count)
+ Petition.where(id: worst_nps.map(&:id)).zip worst_nps
end
class ThresholdLine
@@ -201,4 +192,20 @@ def self.threshold_line value, color
"target=color(lineWidth(threshold(#{value}), 1), '#{color}')"
end
end
+
+ private
+
+ def nps_by_timeframe
+ unless defined?(@nps_by_timeframe)
+ timeframe = 1.send(self.timeframe).ago
+ threshold = self.extremes_threshold.to_i
+
+ @nps_by_timeframe = Metrics::Nps.email_by_timeframe(
+ timeframe, sent_threshold: threshold
+ ).sort_by(&:nps).reverse
+ end
+
+ @nps_by_timeframe
+ end
+
end
@@ -122,55 +122,45 @@
%td
= "$#{average_donations_per_day.round}"
- -##extremes
- -# %table.table.table-condensed.bordered-table.table-striped
- -# %tr
- -# %th
- -# Petitions (Best/Worst NPS)
- -# %th.numeric
- -# Sent
- -# %th.numeric
- -# Sub
- -# %th.numeric
- -# Unsub
- -# %th.numeric
- -# NPS
- -# %tr
- -# %td
- -# Show
- -# = link_to_self_with_param :x, Statistics::EXTREMES_COUNTS, " | "
- -# %td{style: "white-space:nowrap"}
- -# Min
- -# = link_to_self_with_param :th, Statistics::EXTREMES_THRESHOLDS, " | "
- -# %td
- -# %td
- -# %td
- -#
- -# - petition_extremes[:best].each do |p|
- -# %tr
- -# %td
- -# = link_to p[0].title, p[0]
- -# %td.numeric
- -# = p[1][:sent]
- -# %td.numeric
- -# = p[1][:subscribes]
- -# %td.numeric
- -# = p[1][:unsubscribes]
- -# %td.numeric
- -# = float_to_percentage p[1][:nps]
- -#
- -# - petition_extremes[:worst].each do |p|
- -# %tr
- -# %td
- -# = link_to truncate(p[0].title, :length => 75), p[0]
- -# %td.numeric
- -# = p[1][:sent]
- -# %td.numeric
- -# = p[1][:subscribes]
- -# %td.numeric
- -# = p[1][:unsubscribes]
- -# %td.numeric
- -# = float_to_percentage p[1][:nps]
+ #extremes
+ %table.table.table-condensed.bordered-table.table-striped
+ %tr
+ %th
+ Petitions (Best/Worst NPS)
+ %th.numeric
+ Sent
+ %th.numeric
+ Sub
+ %th.numeric
+ Unsub
+ %th.numeric
+ NPS
+ %tr
+ %td
+ Show
+ = link_to_self_with_param :x, Statistics::EXTREMES_COUNTS, " | "
+ %td{style: "white-space:nowrap"}
+ Min
+ = link_to_self_with_param :th, Statistics::EXTREMES_THRESHOLDS, " | "
+ %td
+ %td
+ %td
+
+ - best_petitions.each do |petition, nps|
+ %tr
+ %td= link_to truncate(petition.title, :length => 75), petition
+ %td.numeric= nps.actions
+ %td.numeric= nps.subscribes
+ %td.numeric= nps.unsubscribes
+ %td.numeric= float_to_percentage nps.nps
+
+ - worst_petitions.each do |petition, nps|
+ %tr
+ %td= link_to truncate(petition.title, :length => 75), petition
+ %td.numeric= nps.actions
+ %td.numeric= nps.subscribes
+ %td.numeric= nps.unsubscribes
+ %td.numeric= float_to_percentage nps.nps
%div#charts.row
@@ -1,15 +1,2 @@
describe Admin::DashboardController do
- before do
- create(:donation, amount: 10.0, created_at: 2.days.ago)
- create(:donation, amount: 10.0, created_at: 3.days.ago)
- create(:donation, amount: 10.0, created_at: 4.days.ago)
- create(:donation, amount: 10.0, created_at: 5.days.ago)
- create(:donation, amount: 10.0, created_at: 6.days.ago)
- create(:donation, amount: 10.0, created_at: 7.days.ago)
- create(:donation, amount: 10.0, created_at: 8.days.ago)
- end
-
- subject { Statistics.new }
- its(:total_donations) {should eq 70.0}
- its(:average_donations_per_day) {should eq 10.0}
end
@@ -0,0 +1,29 @@
+describe Statistics do
+
+ context "donation totals" do
+ subject { Statistics.new }
+
+ context "with no donations" do
+ specify { expect(subject.total_donations).to eq(0.0) }
+ end
+
+ context "with multiple donations" do
+ before { 3.times { create :donation, amount: 10.0 } }
+ specify { expect(subject.total_donations).to eq(30.0) }
+ end
+ end
+
+ context "donation averages" do
+ subject { Statistics.new }
+
+ context "with no donations" do
+ specify { expect(subject.average_donations_per_day).to eq(0.0) }
+ end
+
+ context "with multiple donations" do
+ before { 7.times { create :donation, amount: 10.0, created_at: 2.days.ago } }
+ specify { expect(subject.average_donations_per_day).to eq(10.0) }
+ end
+ end
+
+end

0 comments on commit 2efaae5

Please sign in to comment.