Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Update stats page
Browse files Browse the repository at this point in the history
appease code climate
  • Loading branch information
adelevie committed Jun 28, 2016
1 parent 54e2f82 commit 6e71cc6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
50 changes: 36 additions & 14 deletions app/view_models/winners_view_model.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -53,20 +63,24 @@ 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
end.reduce(:+)
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
Expand All @@ -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
8 changes: 7 additions & 1 deletion app/views/winners/index.html.erb
Expand Up @@ -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',
Expand All @@ -49,7 +55,7 @@
locals: {
href: '#',
statistic: @view_model.small_business_count,
label: 'small businesses'
label: 'small businesses registered'
} %>
</div>
</section>
Expand Down

0 comments on commit 6e71cc6

Please sign in to comment.