diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 54cce84a2..86688ae12 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -275,28 +275,34 @@ class StatsController < ApplicationController processed_dossiers = dossiers .where(:processed_at => min_date..max_date) - .pluck(:procedure_id, :created_at, :en_construction_at, :processed_at) + .pluck( + :procedure_id, + Arel.sql('EXTRACT(EPOCH FROM (en_construction_at - created_at)) / 60 AS processing_time'), + :processed_at + ) # Group dossiers by month processed_dossiers_by_month = processed_dossiers - .group_by do |e| - e[3].beginning_of_month.to_s + .group_by do |(*_, processed_at)| + processed_at.beginning_of_month.to_s end - processed_dossiers_by_month.map do |month, value| + procedure_id_type_de_champs_count = TypeDeChamp + .where(private: false) + .group(:procedure_id) + .count + + processed_dossiers_by_month.map do |month, dossier_plucks| # Group the dossiers for this month by procedure - dossiers_grouped_by_procedure = value.group_by { |dossier| dossier[0] } + dossiers_grouped_by_procedure = dossier_plucks.group_by { |(procedure_id, *_)| procedure_id } # Compute the mean time for this procedure procedure_processing_times = dossiers_grouped_by_procedure.map do |procedure_id, procedure_dossiers| - procedure_dossiers_processing_time = procedure_dossiers.map do |dossier| - (dossier[2] - dossier[1]).to_f / 60 - end - + procedure_dossiers_processing_time = procedure_dossiers.map { |_, processing_time, _| processing_time } procedure_mean = mean(procedure_dossiers_processing_time) # We normalize the data for 24 fields - procedure_fields_count = Procedure.find(procedure_id).types_de_champ.count + procedure_fields_count = procedure_id_type_de_champs_count[procedure_id] procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count) end