tab/app/controllers/concerns/statistics.rb

56 lines
1.6 KiB
Ruby
Raw Normal View History

2015-09-11 16:04:45 +00:00
class Statistics < Rails::Application
def shameful_users
2015-09-14 15:21:53 +00:00
User.humans
.where('-balance > :amount', amount: config.shameful_balance)
.order(:balance)
2015-09-11 21:32:38 +00:00
end
def total_debt
2015-09-14 15:21:53 +00:00
-User.humans.where('balance < 0').sum(:balance)
2015-09-11 16:04:45 +00:00
end
def shamehash
2019-06-19 13:45:07 +00:00
# Make a hash of all users and their debt
# only include them if their percentage is big enough
2019-06-17 14:15:29 +00:00
debt_users = shameful_users.inject({}) do |h, u|
h.merge({u.name => - u.balance.to_f / total_debt * 100.0})
end
2019-06-19 13:45:07 +00:00
.select { |user, debtpct| debtpct > 2 }
2019-06-17 14:15:29 +00:00
2019-06-19 13:45:07 +00:00
# Sum of percentages that will be displayed
total_displayed_debt_pct = debt_users.values.reduce(0) {|a,b| a+b}
2019-06-17 14:15:29 +00:00
debt_users["Other users"] = 100 - total_displayed_debt_pct
2019-06-19 13:45:07 +00:00
rounded_debts = debt_users.transform_values { |debtpct| debtpct.floor }
rounded_deficit = 100 - rounded_debts.values.reduce(0) {|a,b| a+b}
# Sort decending by amount after fraction of debt percentage
# then award users percentage points based on their ranking
debt_users.sort_by { |k, v| -(v - v.floor) }
.first(rounded_deficit)
.each do |k|
rounded_debts[k[0]] += 1
end
if rounded_debts["Other users"] == 0
rounded_debts.delete("Other users")
end
return rounded_debts
end
def by_issuer
2015-09-14 14:17:01 +00:00
Client.joins(:issued_transactions).group(:name).count.merge({
"User created" => Transaction.where(issuer_type: "User").count
})
end
2015-09-17 16:23:21 +00:00
def creation_counts
User
.joins(:issued_transactions)
.group(:name)
.order("count_all DESC")
.count
.take([shameful_users.count, 4].max)
end
2015-09-11 16:04:45 +00:00
end