Optimize ratings calculation
This commit is contained in:
parent
52cd3716c1
commit
485116e29c
|
@ -5,7 +5,7 @@ class HomeController < ApplicationController
|
|||
def ratings
|
||||
@talks = Talk.ordered_by_rating
|
||||
@ratings = Ratings.new
|
||||
@votes_count = TalkPreference.count
|
||||
@votes_count = TalkPreference.this_years.count
|
||||
end
|
||||
|
||||
def export
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
class Ratings
|
||||
@ratings = Hash.new(0)
|
||||
|
||||
def initialize
|
||||
ratings = TalkPreference.all.pluck(:talks).reduce(Hash.new(0)) do |result, talks|
|
||||
talks.map(&:to_i).each { |talk| result[talk] += 1 }
|
||||
result
|
||||
end.to_h
|
||||
|
||||
@ratings = Hash.new(0).merge!(ratings)
|
||||
@ratings = {}
|
||||
end
|
||||
|
||||
def [](id)
|
||||
@ratings[id]
|
||||
@ratings[id] ||= SelectedTalk.where(talk_id: id).count
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
<tr>
|
||||
<td><%= talk.id %></td>
|
||||
<td><%= talk.event_type.name %></td>
|
||||
<td><%= talk.title %></td>
|
||||
<td><%= talk.subtitle %></td>
|
||||
<td><%= @ratings[talk.id] %></td>
|
||||
<td><%= "%.2f%" % Rational(100 * @ratings[talk.id], @votes_count).to_f %></td>
|
||||
</tr>
|
|
@ -10,6 +10,15 @@
|
|||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= render partial: 'talk', collection: @talks %>
|
||||
<% @talks.each do |talk| %>
|
||||
<tr>
|
||||
<td><%= talk.id %></td>
|
||||
<td><%= talk.event_type.name %></td>
|
||||
<td><%= talk.title %></td>
|
||||
<td><%= talk.subtitle %></td>
|
||||
<td><%= @ratings[talk.id] %></td>
|
||||
<td><%= "%.2f%" % Rational(100 * @ratings[talk.id], @votes_count).to_f %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
Loading…
Reference in New Issue