From 6e71cc6f432ddf6dbef37a0c162a43f94a72deeb Mon Sep 17 00:00:00 2001 From: Alan deLevie Date: Tue, 28 Jun 2016 12:17:51 -0400 Subject: [PATCH] Update stats page appease code climate --- app/view_models/winners_view_model.rb | 50 +++++++++++++++++++-------- app/views/winners/index.html.erb | 8 ++++- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/app/view_models/winners_view_model.rb b/app/view_models/winners_view_model.rb index 4214c374..b537378c 100644 --- a/app/view_models/winners_view_model.rb +++ b/app/view_models/winners_view_model.rb @@ -12,14 +12,14 @@ def total_auctions end def unique_auction_winners - successful_auctions.map do |auction| + completed_auctions.map do |auction| WinningBid.new(auction).find.bidder end.uniq.count end def average_bids_per_auction - if successful_auction_count > 0 - successful_auctions.map(&:bids).flatten.count / successful_auction_count + if completed_auction_count > 0 + completed_auctions.map(&:bids).flatten.count / completed_auction_count else 'n/a' end @@ -39,6 +39,16 @@ def average_auction_length end end + def average_delivery_time + if accepted_auctions_count > 0 + HumanTime.new( + time: (total_auction_time_length / accepted_auctions_count) + ).distance_of_time + else + 'n/a' + end + end + def average_winning_bid if completed_auction_count > 0 Currency.new(total_winning_bid_amount / completed_auction_count).to_s @@ -53,6 +63,12 @@ def small_business_count private + def total_delivery_time_length + accepted_auctions.map do |auction| + auction.delivery_due_at - auction.ended_at + end.reduce(:+) + end + def total_auction_time_length published_auctions.map do |auction| auction.ended_at - auction.started_at @@ -60,13 +76,11 @@ def total_auction_time_length end def total_winning_bid_amount - completed_auctions.map do |auction| - auction.lowest_bid.amount - end.reduce(:+) - end - - def successful_auction_count - @_successful_auction_count ||= successful_auctions.count + completed_auctions + .map(&:lowest_bid) + .map(&:amount) + .reject(&:nil?) + .reduce(:+) end def published_auction_count @@ -87,11 +101,19 @@ def completed_auctions .where.not(bids: { id: nil }) end - def published_auctions - @_published_auctions ||= AuctionQuery.new.published + def accepted_auctions + @_accepted_auctions ||= + AuctionQuery + .new + .published + .accepted + end + + def accepted_auctions_count + accepted_auctions.length end - def successful_auctions - @_successful_auctions ||= AuctionQuery.new.complete_and_successful + def published_auctions + @_published_auctions ||= AuctionQuery.new.published end end diff --git a/app/views/winners/index.html.erb b/app/views/winners/index.html.erb index 300ad9cf..02bc3bef 100644 --- a/app/views/winners/index.html.erb +++ b/app/views/winners/index.html.erb @@ -39,6 +39,12 @@ statistic: @view_model.average_auction_length, label: 'average auction length' } %> + <%= render partial: 'hero_metric', + locals: { + href: '#', + statistic: @view_model.average_delivery_time, + label: 'average delivery time' + } %> <%= render partial: 'hero_metric', locals: { href: '#chart-winning-bid', @@ -49,7 +55,7 @@ locals: { href: '#', statistic: @view_model.small_business_count, - label: 'small businesses' + label: 'small businesses registered' } %>