Files
grecia/app/controllers/admin/api/stats_controller.rb
Javi Martín 0b2975194b Extract model to handle chart data
We're adding it inside the Ahoy module because the Ahoy::DataSource
class is also in that module. We might move it to a different place in
the future.
2024-05-09 14:28:31 +02:00

31 lines
921 B
Ruby

class Admin::Api::StatsController < Admin::Api::BaseController
def show
if params[:event].blank? &&
params[:visits].blank? &&
params[:budget_investments].blank? &&
params[:user_supported_budgets].blank?
return render json: {}, status: :bad_request
end
ds = Ahoy::DataSource.new
if params[:event].present?
ds.add params[:event].titleize, Ahoy::Chart.new(params[:event]).group_by_day(:time).count
end
if params[:visits].present?
ds.add "Visits", Visit.group_by_day(:started_at).count
end
if params[:budget_investments].present?
ds.add "Budget Investments", Budget::Investment.group_by_day(:created_at).count
end
if params[:user_supported_budgets].present?
ds.add "User supported budgets",
Vote.where(votable_type: "Budget::Investment").group_by_day(:updated_at).count
end
render json: ds.build
end
end