From cea170e4a67e9c9f409403f31dca64a231d217c2 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Mon, 6 Jul 2020 18:42:21 +0200 Subject: [PATCH] update stats with traitement model --- app/controllers/stats_controller.rb | 14 +++++++++----- spec/controllers/stats_controller_spec.rb | 22 ++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 9e3934c35..d676b5b40 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -249,9 +249,11 @@ class StatsController < ApplicationController min_date = 11.months.ago max_date = Time.zone.now.to_date - processed_dossiers = dossiers + processed_dossiers = Traitement.includes(:dossier) + .where(dossier_id: dossiers) + .where('dossiers.state' => Dossier::TERMINE) .where(:processed_at => min_date..max_date) - .pluck(:groupe_instructeur_id, :en_construction_at, :processed_at) + .pluck('dossiers.groupe_instructeur_id', 'dossiers.en_construction_at', :processed_at) # Group dossiers by month processed_dossiers_by_month = processed_dossiers @@ -290,11 +292,13 @@ class StatsController < ApplicationController min_date = 11.months.ago max_date = Time.zone.now.to_date - processed_dossiers = dossiers + processed_dossiers = Traitement.includes(:dossier) + .where(dossier: dossiers) + .where('dossiers.state' => Dossier::TERMINE) .where(:processed_at => min_date..max_date) .pluck( - :groupe_instructeur_id, - Arel.sql('EXTRACT(EPOCH FROM (en_construction_at - created_at)) / 60 AS processing_time'), + 'dossiers.groupe_instructeur_id', + Arel.sql('EXTRACT(EPOCH FROM (dossiers.en_construction_at - dossiers.created_at)) / 60 AS processing_time'), :processed_at ) diff --git a/spec/controllers/stats_controller_spec.rb b/spec/controllers/stats_controller_spec.rb index d4547e81c..a16a9281d 100644 --- a/spec/controllers/stats_controller_spec.rb +++ b/spec/controllers/stats_controller_spec.rb @@ -106,26 +106,23 @@ describe StatsController, type: :controller do before do procedure_1 = FactoryBot.create(:procedure) procedure_2 = FactoryBot.create(:procedure) - dossier_p1_a = FactoryBot.create(:dossier, + dossier_p1_a = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :en_construction_at => 2.months.ago.beginning_of_month, :processed_at => 2.months.ago.beginning_of_month + 3.days) - dossier_p1_b = FactoryBot.create(:dossier, + dossier_p1_b = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :en_construction_at => 2.months.ago.beginning_of_month, :processed_at => 2.months.ago.beginning_of_month + 1.day) - dossier_p1_c = FactoryBot.create(:dossier, + dossier_p1_c = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :en_construction_at => 1.month.ago.beginning_of_month, :processed_at => 1.month.ago.beginning_of_month + 5.days) - dossier_p2_a = FactoryBot.create(:dossier, + dossier_p2_a = FactoryBot.create(:dossier, :accepte, :procedure => procedure_2, :en_construction_at => 2.months.ago.beginning_of_month, :processed_at => 2.months.ago.beginning_of_month + 4.days) - # Write directly in the DB to avoid the before_validation hook - Dossier.update_all(state: Dossier.states.fetch(:accepte)) - @expected_hash = { (2.months.ago.beginning_of_month).to_s => 3.0, (1.month.ago.beginning_of_month).to_s => 5.0 @@ -154,30 +151,27 @@ describe StatsController, type: :controller do before do procedure_1 = FactoryBot.create(:procedure, :with_type_de_champ, :types_de_champ_count => 24) procedure_2 = FactoryBot.create(:procedure, :with_type_de_champ, :types_de_champ_count => 48) - dossier_p1_a = FactoryBot.create(:dossier, + dossier_p1_a = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :created_at => 2.months.ago.beginning_of_month, :en_construction_at => 2.months.ago.beginning_of_month + 30.minutes, :processed_at => 2.months.ago.beginning_of_month + 1.day) - dossier_p1_b = FactoryBot.create(:dossier, + dossier_p1_b = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :created_at => 2.months.ago.beginning_of_month, :en_construction_at => 2.months.ago.beginning_of_month + 10.minutes, :processed_at => 2.months.ago.beginning_of_month + 1.day) - dossier_p1_c = FactoryBot.create(:dossier, + dossier_p1_c = FactoryBot.create(:dossier, :accepte, :procedure => procedure_1, :created_at => 1.month.ago.beginning_of_month, :en_construction_at => 1.month.ago.beginning_of_month + 50.minutes, :processed_at => 1.month.ago.beginning_of_month + 1.day) - dossier_p2_a = FactoryBot.create(:dossier, + dossier_p2_a = FactoryBot.create(:dossier, :accepte, :procedure => procedure_2, :created_at => 2.months.ago.beginning_of_month, :en_construction_at => 2.months.ago.beginning_of_month + 80.minutes, :processed_at => 2.months.ago.beginning_of_month + 1.day) - # Write directly in the DB to avoid the before_validation hook - Dossier.update_all(state: Dossier.states.fetch(:accepte)) - @expected_hash = { (2.months.ago.beginning_of_month).to_s => 30.0, (1.month.ago.beginning_of_month).to_s => 50.0