From 6a86a418eb16ab223713724cbe8dc9451252bc83 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 15:14:09 +0200 Subject: [PATCH 1/6] show transaction distribution in euro's --- app/controllers/concerns/statistics.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index d2c835a0..a560f18f 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -24,7 +24,7 @@ def by_issuer end def amount_distribution - Transaction.group("round(amount / 10)").count.inject(Hash.new) do |hash, (group, count)| + Transaction.group("round(amount / 1000, 2)").count.inject(Hash.new) do |hash, (group, count)| hash.merge({10 * group.to_i => count}) end end From d0d75105320ffcac61489291548ef0f9c71d4f6d Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 16:17:01 +0200 Subject: [PATCH 2/6] better piechart of origins --- app/controllers/concerns/statistics.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index a560f18f..3783d677 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -18,9 +18,9 @@ def shamehash 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 From a88f94ba3ad7c9196e32aa3624d9ac4c4c1776fb Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 16:50:47 +0200 Subject: [PATCH 3/6] improve seeds for the users and add clients --- lib/tasks/devseed.rake | 7 ++++++- spec/factories/transactions.rb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/tasks/devseed.rake b/lib/tasks/devseed.rake index 041998f2..6708eeb9 100644 --- a/lib/tasks/devseed.rake +++ b/lib/tasks/devseed.rake @@ -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 diff --git a/spec/factories/transactions.rb b/spec/factories/transactions.rb index 237b789c..23de4b8b 100644 --- a/spec/factories/transactions.rb +++ b/spec/factories/transactions.rb @@ -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 From 2a51355046b466197cef149c731ead42d98ecfa5 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 17:21:53 +0200 Subject: [PATCH 4/6] fix shamepie, break distribution --- app/controllers/concerns/statistics.rb | 15 ++++++++------- app/models/user.rb | 3 +++ app/views/pages/landing.html.haml | 2 +- config/application.rb | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index 3783d677..6d91c678 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -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 @@ def by_issuer 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 diff --git a/app/models/user.rb b/app/models/user.rb index d490c290..d3c234b0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ def self.from_omniauth(auth) def self.zeus find_or_create_by name: 'Zeus' end + end diff --git a/app/views/pages/landing.html.haml b/app/views/pages/landing.html.haml index 765edb7a..eef0fbcf 100644 --- a/app/views/pages/landing.html.haml +++ b/app/views/pages/landing.html.haml @@ -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 diff --git a/config/application.rb b/config/application.rb index 5525edd1..90e07433 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 From 9191e12ce9aeb9fbc5d0a5c425c0c989de214d37 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 14 Sep 2015 17:30:52 +0200 Subject: [PATCH 5/6] fix amount distribution, still ugly though --- app/controllers/concerns/statistics.rb | 4 ++-- app/views/pages/landing.html.haml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index 6d91c678..e2f756f3 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -25,8 +25,8 @@ def by_issuer end def amount_distribution - Transaction.group("round(amount / 1000.0, 2)").count.inject(Hash.new) do |hash, (group, count)| - hash.merge({10 * group.to_i => count}) + Transaction.group("round(amount / 1000)").count.inject(Hash.new) do |hash, (group, count)| + hash.merge({10 * group.to_f => count}) end end diff --git a/app/views/pages/landing.html.haml b/app/views/pages/landing.html.haml index eef0fbcf..80f85a50 100644 --- a/app/views/pages/landing.html.haml +++ b/app/views/pages/landing.html.haml @@ -30,4 +30,5 @@ = pie_chart @statistics.by_issuer .pure-u-1-2 %h3 Distribution of Transaction Amounts + = @statistics.amount_distribution = column_chart @statistics.amount_distribution From 4aebb0be59792830a81c4b33d4f7aea822919f8a Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Thu, 17 Sep 2015 18:23:21 +0200 Subject: [PATCH 6/6] improve landing page --- app/assets/stylesheets/pages.scss | 33 ++++++++++- app/controllers/concerns/statistics.rb | 12 ++-- app/views/layouts/application.html.haml | 2 - app/views/pages/landing.html.haml | 75 ++++++++++++++----------- 4 files changed, 82 insertions(+), 40 deletions(-) diff --git a/app/assets/stylesheets/pages.scss b/app/assets/stylesheets/pages.scss index 6286627e..72c5f896 100644 --- a/app/assets/stylesheets/pages.scss +++ b/app/assets/stylesheets/pages.scss @@ -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; +} diff --git a/app/controllers/concerns/statistics.rb b/app/controllers/concerns/statistics.rb index e2f756f3..a74cd3b3 100644 --- a/app/controllers/concerns/statistics.rb +++ b/app/controllers/concerns/statistics.rb @@ -24,10 +24,13 @@ def by_issuer }) end - def amount_distribution - Transaction.group("round(amount / 1000)").count.inject(Hash.new) do |hash, (group, count)| - hash.merge({10 * group.to_f => count}) - end + def creation_counts + User + .joins(:issued_transactions) + .group(:name) + .order("count_all DESC") + .count + .take([shameful_users.count, 4].max) end private @@ -37,3 +40,4 @@ def zeus_balance end end + diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 1dd2d963..bdd65421 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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 diff --git a/app/views/pages/landing.html.haml b/app/views/pages/landing.html.haml index 80f85a50..159d11ae 100644 --- a/app/views/pages/landing.html.haml +++ b/app/views/pages/landing.html.haml @@ -1,34 +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 - = @statistics.amount_distribution - = 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 +