Files
nairobi/app/controllers/polls_controller.rb
Javi Martín a5646fcdb3 Remove Cron job to generate stats
Since now generating stats (assuming the results aren't in the cache)
only takes a few seconds even when there are a hundred thousand
participants, as opposed to the several minutes it took to generate them
when we introduced the Cron job, we can simply generate the stats during
the first request to the stats page.

Note that, in order to avoid creating a temporary table when the stats
are cached, we're making sure we only create this table when we need to.
Otherwise, we could spend up to 1 second on every request to the stats
page creating a table that isn't going to be used.

Also note we're using an instance variable to check whether we're
creating a table; I tried to use `table_exists?`, but it didn't work. I
wonder whether `table_exists?` doesn't detect temporary tables.
2024-05-17 16:08:08 +02:00

42 lines
909 B
Ruby

class PollsController < ApplicationController
include FeatureFlags
feature_flag :polls
before_action :load_poll, except: [:index]
before_action :load_active_poll, only: :index
load_and_authorize_resource
has_filters %w[current expired]
has_orders %w[most_voted newest oldest], only: :show
def index
@polls = Kaminari.paginate_array(
@polls.created_by_admin.not_budget.send(@current_filter).includes(:geozones).sort_for_list(current_user)
).page(params[:page])
end
def show
@questions = @poll.questions.for_render.sort_for_list
@comment_tree = CommentTree.new(@poll, params[:page], @current_order)
end
def stats
@stats = Poll::Stats.new(@poll).tap(&:generate)
end
def results
end
private
def load_poll
@poll = Poll.find_by_slug_or_id!(params[:id])
end
def load_active_poll
@active_poll = ActivePoll.first
end
end