fix shamepie, break distribution

This commit is contained in:
Felix Van der Jeugt 2015-09-14 17:21:53 +02:00
parent a88f94ba3a
commit 2a51355046
4 changed files with 13 additions and 9 deletions

View file

@ -2,18 +2,19 @@
class Statistics < Rails::Application
def shameful_users
User.where('balance > :amount', amount: config.shameful_balance)
.order(balance: :desc)
User.humans
.where('-balance > :amount', amount: config.shameful_balance)
.order(:balance)
end
def total_debt
User.where.not(id: User.zeus).where('balance > 0').sum(:balance)
-User.humans.where('balance < 0').sum(:balance)
end
def shamehash
none_shaming = shameful_users.sum(:balance)
shameful_users.inject({'None-shameful users' => none_shaming}) do |h, u|
h.merge({u.name => u.balance})
none_shaming = total_debt + shameful_users.sum(:balance)
shameful_users.inject({'Reputable users' => none_shaming.to_f / total_debt}) do |h, u|
h.merge({u.name => - u.balance.to_f / total_debt})
end
end
@ -24,7 +25,7 @@ class Statistics < Rails::Application
end
def amount_distribution
Transaction.group("round(amount / 1000, 2)").count.inject(Hash.new) do |hash, (group, count)|
Transaction.group("round(amount / 1000.0, 2)").count.inject(Hash.new) do |hash, (group, count)|
hash.merge({10 * group.to_i => count})
end
end

View file

@ -23,6 +23,8 @@ class User < ActiveRecord::Base
validates :name, presence: true, uniqueness: true
scope :humans, -> { where.not(id: self.zeus) }
def transactions
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
end
@ -42,4 +44,5 @@ class User < ActiveRecord::Base
def self.zeus
find_or_create_by name: 'Zeus'
end
end

View file

@ -20,7 +20,7 @@
%td.shameful-person= user.name
// Won't divide by zero because there won't be any users with
// a shameful debt if the total debt is zero.
%td.shame-percentage= "#{100 * user.balance / @statistics.total_debt}%"
%td.shame-percentage= "#{-100 * user.balance / @statistics.total_debt}%"
.pure-u-1-2
%h3 Pie of Shame
= pie_chart @statistics.shamehash

View file

@ -24,6 +24,6 @@ module Tab
config.active_record.raise_in_transactional_callbacks = true
# Which is the lowest balance you should be ashamed of.
config.shameful_balance = 50 # In eurocents!
config.shameful_balance = 5000 # In eurocents!
end
end