From 3590657777a497deecca349fbb0976c0d93fad26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Salvador=20P=C3=A9rez=20Garc=C3=ADa?= Date: Fri, 14 Sep 2018 10:49:29 +0200 Subject: [PATCH] Enhancements on supports controller Supports controller now fill the holes in the results: When there are no supports collected for one interval it takes the accumulated value from the previous one. Data starts in the publication date. --- .../concerns/dashboard/expects_date_range.rb | 4 +- .../dashboard/supports_controller.rb | 41 +++++++++++++++---- spec/requests/dashboard/supports_spec.rb | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/controllers/concerns/dashboard/expects_date_range.rb b/app/controllers/concerns/dashboard/expects_date_range.rb index 9c0cbb9b3..b20f327f9 100644 --- a/app/controllers/concerns/dashboard/expects_date_range.rb +++ b/app/controllers/concerns/dashboard/expects_date_range.rb @@ -3,9 +3,9 @@ module Dashboard::ExpectsDateRange include Dashboard::HasProposal - def start_date + def start_date(fallback_date = proposal.created_at.to_date) return Date.parse(params[:start_date]) unless params[:start_date].blank? - proposal.created_at.to_date + fallback_date end def end_date diff --git a/app/controllers/dashboard/supports_controller.rb b/app/controllers/dashboard/supports_controller.rb index b6f83f3ec..83172376f 100644 --- a/app/controllers/dashboard/supports_controller.rb +++ b/app/controllers/dashboard/supports_controller.rb @@ -20,19 +20,46 @@ class Dashboard::SupportsController < Dashboard::BaseController grouped_votes[k] = accumulated end - grouped_votes + fill_holes(grouped_votes) end def grouped_supports - if params[:group_by] == 'week' - return supports.group_by { |v| "#{v.created_at.to_date.cweek}/#{v.created_at.to_date.year}" } + supports.group_by { |v| grouping_key_for(v.created_at) } + end + + def grouping_key_for(created_at) + return "#{created_at.to_date.cweek}/#{created_at.to_date.year}" if params[:group_by] == 'week' + return "#{created_at.to_date.year}-#{created_at.to_date.month}" if params[:group_by] == 'month' + + created_at.to_date + end + + def fill_holes(grouped_votes) + (start_date(proposal.published_at.to_date)..end_date).step(interval).each do |date| + missing_key = grouping_key_for(date) + next if grouped_votes.key? missing_key + + previous_key = previous_key_for(date) + previous_value = if grouped_votes.key? previous_key + grouped_votes[previous_key] + else + 0 + end + + grouped_votes[missing_key] = previous_value end - if params[:group_by] == 'month' - return supports.group_by { |v| "#{v.created_at.to_date.year}-#{v.created_at.to_date.month}" } - end + grouped_votes + end - supports.group_by { |v| v.created_at.to_date } + def previous_key_for(date) + grouping_key_for(date - interval) + end + + def interval + return 1.week if params[:group_by] == 'week' + return 1.month if params[:group_by] == 'month' + 1.day end def supports diff --git a/spec/requests/dashboard/supports_spec.rb b/spec/requests/dashboard/supports_spec.rb index b3da0de52..89858e796 100644 --- a/spec/requests/dashboard/supports_spec.rb +++ b/spec/requests/dashboard/supports_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' describe "Retrieves number of supports for a proposal" do let(:created_at) { DateTime.parse("2018-01-01 12:00:00") } - let(:proposal) { create(:proposal, created_at: created_at) } + let(:proposal) { create(:proposal, created_at: created_at, published_at: created_at) } before do 8.times do |i|