Skip to content

Commit

Permalink
Merge pull request #10 from ZeusWPI/cute-little-statistics
Browse files Browse the repository at this point in the history
Cute little statistics
  • Loading branch information
Felix Van der Jeugt committed Sep 17, 2015
2 parents cbf491e + 4aebb0b commit 7da6b0f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 51 deletions.
33 changes: 32 additions & 1 deletion app/assets/stylesheets/pages.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
// Place all the styles related to the pages controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.shame-percentage { text-align: right }
.shame-percentage {
text-align: right
}

a.login-button {
font-size: 200%;
margin: 2em;
}

.columns-title {
text-align: center;
}

.left-column {
text-align: right;
padding: 1em 3em;
border-right: 1px solid black;
}

.right-column {
text-align: left;
padding: 1em 3em;
border-left: 1px solid black;
}

.full-table {
width: 100%;
}

.landing-column {
padding: 1em;
}
31 changes: 18 additions & 13 deletions app/controllers/concerns/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@
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

def by_issuer
Transaction.group(:issuer_id).count.inject(Hash.new) do |hash, (id, count)|
hash.merge({User.find(id).name => count})
end
Client.joins(:issued_transactions).group(:name).count.merge({
"User created" => Transaction.where(issuer_type: "User").count
})
end

def amount_distribution
Transaction.group("round(amount / 10)").count.inject(Hash.new) do |hash, (group, count)|
hash.merge({10 * group.to_i => count})
end
def creation_counts
User
.joins(:issued_transactions)
.group(:name)
.order("count_all DESC")
.count
.take([shameful_users.count, 4].max)
end

private
Expand All @@ -36,3 +40,4 @@ def zeus_balance
end

end

3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,4 +44,5 @@ def self.from_omniauth(auth)
def self.zeus
find_or_create_by name: 'Zeus'
end

end
2 changes: 0 additions & 2 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
- if current_user.penning
%li.pure-menu-item
=link_to "Zeus", User.zeus, class: "pure-menu-link"
- else
= link_to "Sign in", user_omniauth_authorize_path(:zeuswpi), class: "pure-menu-link" unless current_user
.pure-u-1
= render 'partials/flash'
= yield
74 changes: 42 additions & 32 deletions app/views/pages/landing.html.haml
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
%h1 Tab!
%h2 Authentication
- if user_signed_in?
%p Yeah! Je bent ingelogd.
- else
Log een keer in!
%p= link_to "Log in met Zeus WPI", user_omniauth_authorize_path(:zeuswpi)
%h2 Cute Little Statistics
%h1.columns-title Tab
= javascript_include_tag "//www.google.com/jsapi", "chartkick"
.pure-g
.pure-u-1-2
%h3 Table of Shame
%table.pure-table
%thead
%th Shame on
%th Contribution to Zeus' lack of money
%tbody
- @statistics.shameful_users.each do |user|
%tr
%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}%"
.pure-u-1-2
%h3 Pie of Shame
= pie_chart @statistics.shamehash
.pure-g
.pure-u-1-2
%h3 Distribution of Debt Sources
= pie_chart @statistics.by_issuer
.pure-u-1-2
%h3 Distribution of Transaction Amounts
= column_chart @statistics.amount_distribution
- unless user_signed_in?
.pure-g
.pure-u-1-2.left-column
%h2 Authentication
Log een keer in en betaal uw schulden!
= link_to "Log in met Zeus WPI", user_omniauth_authorize_path(:zeuswpi), class: "pure-button pure-button-primary login-button"
.pure-u-1-2.right-column
%h2 Pie of Shame
= pie_chart @statistics.shamehash
- else
%h2.columns-title Cute Little Statistics
.pure-g
.pure-u-1-2.landing-column
%h3.columns-title Pie of Shame
= pie_chart @statistics.shamehash
%h3.columns-title Table of Shame
%table.pure-table.full-table
%thead
%th Shame on
%th Contribution to Zeus' lack of money
%tbody
- @statistics.shameful_users.each do |user|
%tr
%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}%"
.pure-u-1-2.landing-column
%h3.columns-title Distribution of Debt Sources
= pie_chart @statistics.by_issuer
%h3.columns-title Top Debt Creators
%table.pure-table.full-table
%thead
%th Issuer
%th Number of Transactions issued
%tbody
- @statistics.creation_counts.each do |name, count|
%tr
%td.shameful-person= name
%td.shame-percentage= count

2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class Application < Rails::Application
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
7 changes: 6 additions & 1 deletion lib/tasks/devseed.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ unless Rails.env.production?
users = FactoryGirl.create_list(:user, 20)
100.times do
sample_users = users.sample(2)
FactoryGirl.create :transaction, debtor: sample_users[0], creditor: sample_users[1], amount: 1 + rand(100)
FactoryGirl.create :transaction, debtor: sample_users[0], creditor: sample_users[1]
end
clients = FactoryGirl.create_list(:client, 5)
100.times do
debtor, creditor = users.sample(2)
FactoryGirl.create :client_transaction, issuer: clients.sample, debtor: debtor, creditor: creditor
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
association :debtor, factory: :user
association :creditor, factory: :user
issuer { debtor }
amount { 1 + rand(100) }
amount { 1 + rand(10000) }
message { Faker::Lorem.sentence }
factory :client_transaction do
association :issuer, factory: :client
Expand Down

0 comments on commit 7da6b0f

Please sign in to comment.