Skip to content

Commit 7da6b0f

Browse files
author
Felix Van der Jeugt
committed
Merge pull request #10 from ZeusWPI/cute-little-statistics
Cute little statistics
2 parents cbf491e + 4aebb0b commit 7da6b0f

File tree

8 files changed

+103
-51
lines changed

8 files changed

+103
-51
lines changed

app/assets/stylesheets/pages.scss

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
// Place all the styles related to the pages controller here.
22
// They will automatically be included in application.css.
33
// You can use Sass (SCSS) here: http://sass-lang.com/
4-
.shame-percentage { text-align: right }
4+
.shame-percentage {
5+
text-align: right
6+
}
7+
8+
a.login-button {
9+
font-size: 200%;
10+
margin: 2em;
11+
}
12+
13+
.columns-title {
14+
text-align: center;
15+
}
16+
17+
.left-column {
18+
text-align: right;
19+
padding: 1em 3em;
20+
border-right: 1px solid black;
21+
}
22+
23+
.right-column {
24+
text-align: left;
25+
padding: 1em 3em;
26+
border-left: 1px solid black;
27+
}
28+
29+
.full-table {
30+
width: 100%;
31+
}
32+
33+
.landing-column {
34+
padding: 1em;
35+
}

app/controllers/concerns/statistics.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@
22
class Statistics < Rails::Application
33

44
def shameful_users
5-
User.where('balance > :amount', amount: config.shameful_balance)
6-
.order(balance: :desc)
5+
User.humans
6+
.where('-balance > :amount', amount: config.shameful_balance)
7+
.order(:balance)
78
end
89

910
def total_debt
10-
User.where.not(id: User.zeus).where('balance > 0').sum(:balance)
11+
-User.humans.where('balance < 0').sum(:balance)
1112
end
1213

1314
def shamehash
14-
none_shaming = shameful_users.sum(:balance)
15-
shameful_users.inject({'None-shameful users' => none_shaming}) do |h, u|
16-
h.merge({u.name => u.balance})
15+
none_shaming = total_debt + shameful_users.sum(:balance)
16+
shameful_users.inject({'Reputable users' => none_shaming.to_f / total_debt}) do |h, u|
17+
h.merge({u.name => - u.balance.to_f / total_debt})
1718
end
1819
end
1920

2021
def by_issuer
21-
Transaction.group(:issuer_id).count.inject(Hash.new) do |hash, (id, count)|
22-
hash.merge({User.find(id).name => count})
23-
end
22+
Client.joins(:issued_transactions).group(:name).count.merge({
23+
"User created" => Transaction.where(issuer_type: "User").count
24+
})
2425
end
2526

26-
def amount_distribution
27-
Transaction.group("round(amount / 10)").count.inject(Hash.new) do |hash, (group, count)|
28-
hash.merge({10 * group.to_i => count})
29-
end
27+
def creation_counts
28+
User
29+
.joins(:issued_transactions)
30+
.group(:name)
31+
.order("count_all DESC")
32+
.count
33+
.take([shameful_users.count, 4].max)
3034
end
3135

3236
private
@@ -36,3 +40,4 @@ def zeus_balance
3640
end
3741

3842
end
43+

app/models/user.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class User < ActiveRecord::Base
2323

2424
validates :name, presence: true, uniqueness: true
2525

26+
scope :humans, -> { where.not(id: self.zeus) }
27+
2628
def transactions
2729
Transaction.where("creditor_id = ? OR debtor_id = ?", id, id)
2830
end
@@ -42,4 +44,5 @@ def self.from_omniauth(auth)
4244
def self.zeus
4345
find_or_create_by name: 'Zeus'
4446
end
47+
4548
end

app/views/layouts/application.html.haml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
- if current_user.penning
1818
%li.pure-menu-item
1919
=link_to "Zeus", User.zeus, class: "pure-menu-link"
20-
- else
21-
= link_to "Sign in", user_omniauth_authorize_path(:zeuswpi), class: "pure-menu-link" unless current_user
2220
.pure-u-1
2321
= render 'partials/flash'
2422
= yield

app/views/pages/landing.html.haml

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
%h1 Tab!
2-
%h2 Authentication
3-
- if user_signed_in?
4-
%p Yeah! Je bent ingelogd.
5-
- else
6-
Log een keer in!
7-
%p= link_to "Log in met Zeus WPI", user_omniauth_authorize_path(:zeuswpi)
8-
%h2 Cute Little Statistics
1+
%h1.columns-title Tab
92
= javascript_include_tag "//www.google.com/jsapi", "chartkick"
10-
.pure-g
11-
.pure-u-1-2
12-
%h3 Table of Shame
13-
%table.pure-table
14-
%thead
15-
%th Shame on
16-
%th Contribution to Zeus' lack of money
17-
%tbody
18-
- @statistics.shameful_users.each do |user|
19-
%tr
20-
%td.shameful-person= user.name
21-
// Won't divide by zero because there won't be any users with
22-
// a shameful debt if the total debt is zero.
23-
%td.shame-percentage= "#{100 * user.balance / @statistics.total_debt}%"
24-
.pure-u-1-2
25-
%h3 Pie of Shame
26-
= pie_chart @statistics.shamehash
27-
.pure-g
28-
.pure-u-1-2
29-
%h3 Distribution of Debt Sources
30-
= pie_chart @statistics.by_issuer
31-
.pure-u-1-2
32-
%h3 Distribution of Transaction Amounts
33-
= column_chart @statistics.amount_distribution
3+
- unless user_signed_in?
4+
.pure-g
5+
.pure-u-1-2.left-column
6+
%h2 Authentication
7+
Log een keer in en betaal uw schulden!
8+
= link_to "Log in met Zeus WPI", user_omniauth_authorize_path(:zeuswpi), class: "pure-button pure-button-primary login-button"
9+
.pure-u-1-2.right-column
10+
%h2 Pie of Shame
11+
= pie_chart @statistics.shamehash
12+
- else
13+
%h2.columns-title Cute Little Statistics
14+
.pure-g
15+
.pure-u-1-2.landing-column
16+
%h3.columns-title Pie of Shame
17+
= pie_chart @statistics.shamehash
18+
%h3.columns-title Table of Shame
19+
%table.pure-table.full-table
20+
%thead
21+
%th Shame on
22+
%th Contribution to Zeus' lack of money
23+
%tbody
24+
- @statistics.shameful_users.each do |user|
25+
%tr
26+
%td.shameful-person= user.name
27+
// Won't divide by zero because there won't be any users with
28+
// a shameful debt if the total debt is zero.
29+
%td.shame-percentage= "#{-100 * user.balance / @statistics.total_debt}%"
30+
.pure-u-1-2.landing-column
31+
%h3.columns-title Distribution of Debt Sources
32+
= pie_chart @statistics.by_issuer
33+
%h3.columns-title Top Debt Creators
34+
%table.pure-table.full-table
35+
%thead
36+
%th Issuer
37+
%th Number of Transactions issued
38+
%tbody
39+
- @statistics.creation_counts.each do |name, count|
40+
%tr
41+
%td.shameful-person= name
42+
%td.shame-percentage= count
43+

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ class Application < Rails::Application
2424
config.active_record.raise_in_transactional_callbacks = true
2525

2626
# Which is the lowest balance you should be ashamed of.
27-
config.shameful_balance = 50 # In eurocents!
27+
config.shameful_balance = 5000 # In eurocents!
2828
end
2929
end

lib/tasks/devseed.rake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ unless Rails.env.production?
55
users = FactoryGirl.create_list(:user, 20)
66
100.times do
77
sample_users = users.sample(2)
8-
FactoryGirl.create :transaction, debtor: sample_users[0], creditor: sample_users[1], amount: 1 + rand(100)
8+
FactoryGirl.create :transaction, debtor: sample_users[0], creditor: sample_users[1]
9+
end
10+
clients = FactoryGirl.create_list(:client, 5)
11+
100.times do
12+
debtor, creditor = users.sample(2)
13+
FactoryGirl.create :client_transaction, issuer: clients.sample, debtor: debtor, creditor: creditor
914
end
1015
end
1116
end

spec/factories/transactions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
association :debtor, factory: :user
2020
association :creditor, factory: :user
2121
issuer { debtor }
22-
amount { 1 + rand(100) }
22+
amount { 1 + rand(10000) }
2323
message { Faker::Lorem.sentence }
2424
factory :client_transaction do
2525
association :issuer, factory: :client

0 commit comments

Comments
 (0)