diff --git a/app/controllers/admin/api/stats_controller.rb b/app/controllers/admin/api/stats_controller.rb index af8ffb3aac4..37be264bffe 100644 --- a/app/controllers/admin/api/stats_controller.rb +++ b/app/controllers/admin/api/stats_controller.rb @@ -1,20 +1,18 @@ class Admin::Api::StatsController < Admin::Api::BaseController def show - unless params[:events].present? || + unless params[:event].present? || params[:visits].present? || params[:spending_proposals].present? || - params[:budget_investments].present? + params[:budget_investments].present? || + params[:user_supported_budgets].present? return render json: {}, status: :bad_request end ds = Ahoy::DataSource.new - if params[:events].present? - event_types = params[:events].split "," - event_types.each do |event| - ds.add event.titleize, Ahoy::Event.where(name: event).group_by_day(:time).count - end + if params[:event].present? + ds.add params[:event].titleize, Ahoy::Event.where(name: params[:event]).group_by_day(:time).count end if params[:visits].present? @@ -29,6 +27,9 @@ def show 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 diff --git a/app/controllers/admin/stats_controller.rb b/app/controllers/admin/stats_controller.rb index 352232d55d4..b6a926792aa 100644 --- a/app/controllers/admin/stats_controller.rb +++ b/app/controllers/admin/stats_controller.rb @@ -1,7 +1,7 @@ class Admin::StatsController < Admin::BaseController def show - @event_types = Ahoy::Event.group(:name).count + @event_types = Ahoy::Event.pluck(:name).uniq.sort @visits = Visit.count @debates = Debate.with_hidden.count @@ -31,6 +31,17 @@ def show @investments = Budget::Investment.where(budget_id: budgets_ids).count end + def graph + @name = params[:id] + @event = params[:event] + + if params[:event] + @count = Ahoy::Event.where(name: params[:event]).count + else + @count = params[:count] + end + end + def proposal_notifications @proposal_notifications = ProposalNotification.all @proposals_with_notifications = @proposal_notifications.select(:proposal_id).distinct.count @@ -41,9 +52,50 @@ def direct_messages @users_who_have_sent_message = DirectMessage.select(:sender_id).distinct.count end + + def budgets + @budgets = Budget.all + end + + def budget_supporting + @budget = Budget.find(params[:budget_id]) + heading_ids = @budget.heading_ids + + votes = Vote.where(votable_type: "Budget::Investment"). + includes(:budget_investment). + where(budget_investments: { heading_id: heading_ids }) + + @vote_count = votes.count + @user_count = votes.select(:voter_id).distinct.count + + @voters_in_heading = {} + @budget.headings.each do |heading| + @voters_in_heading[heading] = voters_in_heading(heading) + end + end + + def budget_balloting + @budget = Budget.find(params[:budget_id]) + @user_count = @budget.ballots.select {|ballot| ballot.lines.any? }.count + + @vote_count = @budget.lines.count + + @vote_count_by_heading = @budget.lines.group(:heading_id).count.collect {|k,v| [Budget::Heading.find(k).name, v]}.sort + + @user_count_by_district = User.where.not(balloted_heading_id: nil).group(:balloted_heading_id).count.collect {|k,v| [Budget::Heading.find(k).name, v]}.sort + end + def polls @polls = ::Poll.current @participants = ::Poll::Voter.where(poll: @polls) end + private + + def voters_in_heading(heading) + Vote.where(votable_type: "Budget::Investment"). + includes(:budget_investment). + where(budget_investments: { heading_id: heading.id }). + select("votes.voter_id").distinct.count + end end diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index 15fdc91a878..d8425c2170f 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -44,10 +44,6 @@ def namespaced_budget_investment_vote_path(investment, options = {}) end end - def display_budget_countdown?(budget) - budget.balloting? - end - def css_for_ballot_heading(heading) return "" if current_ballot.blank? || @current_filter == "unfeasible" current_ballot.has_lines_in_heading?(heading) ? "is-active" : "" diff --git a/app/helpers/stats_helper.rb b/app/helpers/stats_helper.rb index b36977b3e28..57db7166a0d 100644 --- a/app/helpers/stats_helper.rb +++ b/app/helpers/stats_helper.rb @@ -1,24 +1,27 @@ module StatsHelper - def events_chart_tag(events, opt = {}) - events = events.join(",") if events.is_a? Array + def chart_tag(opt = {}) opt[:data] ||= {} - opt[:data][:graph] = admin_api_stats_path(events: events) + opt[:data][:graph] = admin_api_stats_path(chart_data(opt)) content_tag :div, "", opt end - def visits_chart_tag(opt = {}) - events = events.join(",") if events.is_a? Array - opt[:data] ||= {} - opt[:data][:graph] = admin_api_stats_path(visits: true) - content_tag :div, "", opt + def chart_data(opt = {}) + data = nil + if opt[:id].present? + data = { opt[:id] => true } + elsif opt[:event].present? + data = { event: opt[:event] } + end + data end - def spending_proposals_chart_tag(opt = {}) - events = events.join(",") if events.is_a? Array - opt[:data] ||= {} - opt[:data][:graph] = admin_api_stats_path(spending_proposals: true) - content_tag :div, "", opt + def graph_link_text(event) + text = t("admin.stats.graph.#{event}") + if text.to_s.match(/translation missing/) + text = event + end + text end def budget_investments_chart_tag(opt = {}) diff --git a/app/models/budget.rb b/app/models/budget.rb index 8c34e030db4..8cc5c9b8f6c 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -31,6 +31,7 @@ def name_uniqueness_by_budget has_many :ballots, dependent: :destroy has_many :groups, dependent: :destroy has_many :headings, through: :groups + has_many :lines, through: :ballots, class_name: "Budget::Ballot::Line" has_many :phases, class_name: Budget::Phase has_one :poll diff --git a/app/views/admin/stats/_graph.html.erb b/app/views/admin/stats/_graph.html.erb new file mode 100644 index 00000000000..1a89752899d --- /dev/null +++ b/app/views/admin/stats/_graph.html.erb @@ -0,0 +1,6 @@ +<%= back_link_to %> + +
+ <%= t("admin.stats.budget_balloting.vote_count") %>
+
+
+ <%= @vote_count %>
+
+
+ <%= t("admin.stats.budget_balloting.participant_count") %>
+
+
+ <%= @user_count %>
+
+
<%= t("admin.stats.budget_balloting.votes_per_heading") %> | + + <% @vote_count_by_heading.each do |heading_name, count| %> +|
---|---|
+ <%= heading_name %> + | ++ <%= number_with_delimiter count %> + | +
<%= t("admin.stats.budget_balloting.participants_per_district") %> | + + <% @user_count_by_district.each do |heading_name, count| %> +|
---|---|
+ <%= heading_name %> + | ++ <%= number_with_delimiter count %> + | +
+ <%= t("admin.stats.budget_supporting.vote_count") %>
+
+
+ <%= @vote_count %>
+
+
+ <%= t("admin.stats.budget_supporting.participant_count") %>
+
+
+ <%= @user_count %>
+
+
<%= t("admin.stats.budget_supporting.headings") %> | +<%= t("admin.stats.budget_supporting.users") %> | + + <% @voters_in_heading.each do |heading, count| %> +
---|---|
+ <%= heading.name %> + | ++ <%= number_with_delimiter count %> + | +
+ <%= budget.name %> + | ++ <%= link_to t("admin.stats.budgets.supporting_phase"), budget_supporting_admin_stats_path(budget_id: budget.id), class: "button hollow" %> + <%= link_to t("admin.stats.budgets.balloting_phase"), budget_balloting_admin_stats_path(budget_id: budget.id), class: "button hollow" %> + | +
- <%= t "admin.stats.show.summary.visits" %>
+ <%= link_to t("admin.stats.show.summary.visits"),
+ graph_admin_stats_path(id: "visits", count: @visits) %>
<%= number_with_delimiter(@visits) %>