diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 629b2dad7..9b4131f1a 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -5,6 +5,9 @@ class StatsController < ApplicationController procedures = Procedure.where(:published => true) dossiers = Dossier.where.not(:state => :draft) + @procedures_in_the_last_4_months = last_four_months_hash(procedures) + @dossiers_in_the_last_4_months = last_four_months_hash(dossiers, :initiated_at) + @procedures_30_days_flow = thirty_days_flow_hash(procedures) @dossiers_30_days_flow = thirty_days_flow_hash(dossiers, :initiated_at) @@ -17,6 +20,19 @@ class StatsController < ApplicationController private + def last_four_months_hash(association, date_attribute = :created_at) + min_date = 3.months.ago.beginning_of_month.to_date + max_date = Time.now.to_date + + association + .where(date_attribute => min_date..max_date) + .group("DATE_TRUNC('month', #{date_attribute.to_s})") + .count + .to_a + .sort{ |x, y| x[0] <=> y[0] } + .map { |e| [I18n.l(e.first, format: "%B %Y"), e.last] } + end + def thirty_days_flow_hash(association, date_attribute = :created_at) min_date = 30.days.ago.to_date max_date = Time.now.to_date diff --git a/app/views/stats/index.html.haml b/app/views/stats/index.html.haml index 9b0a389b1..ce252935d 100644 --- a/app/views/stats/index.html.haml +++ b/app/views/stats/index.html.haml @@ -4,6 +4,22 @@ .stat-cards + .stat-card.stat-card-half.pull-left + %span.stat-card-title Procédures dématérialisées / mois + + .chart-container + .chart + = column_chart @procedures_in_the_last_4_months, + :colors => ["rgba(61, 149, 236, 1)"] + + .stat-card.stat-card-half.pull-left + %span.stat-card-title Dossiers déposés / mois + + .chart-container + .chart + = column_chart @dossiers_in_the_last_4_months, + :colors => ["rgba(61, 149, 236, 1)"] + .stat-card.stat-card-half.pull-left %ul.segmented-control.pull-right %li.segmented-control-item.segmented-control-item-active{ :onclick => "TPS.toggleChart(event, '.cumulative-procedures-chart');" } diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index 1b5753fc0..54c48be7a 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -1,6 +1,44 @@ require 'spec_helper' describe StatsController, type: :controller do + describe "#last_four_months_hash" do + context "without a date attribute" do + before do + FactoryGirl.create(:procedure, :created_at => 6.months.ago) + FactoryGirl.create(:procedure, :created_at => 45.days.ago) + FactoryGirl.create(:procedure, :created_at => 1.days.ago) + FactoryGirl.create(:procedure, :created_at => 1.days.ago) + end + + let (:association) { Procedure.all } + + subject { StatsController.new.send(:last_four_months_hash, association) } + + it { expect(subject).to eq([ + [I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 1], + [I18n.l(1.days.ago.beginning_of_month, format: "%B %Y"), 2] + ] ) } + end + + context "with a date attribute" do + before do + FactoryGirl.create(:procedure, :created_at => 6.months.ago, :updated_at => 6.months.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 45.days.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 45.days.ago) + FactoryGirl.create(:procedure, :created_at => 2.months.ago, :updated_at => 1.days.ago) + end + + let (:association) { Procedure.all } + + subject { StatsController.new.send(:last_four_months_hash, association, :updated_at) } + + it { expect(subject).to eq([ + [I18n.l(45.days.ago.beginning_of_month, format: "%B %Y"), 2], + [I18n.l(1.days.ago.beginning_of_month, format: "%B %Y"), 1] + ] ) } + end + end + describe '#thirty_days_flow_hash' do context "without a date_attribut" do before do