[Fix #264] Add the mean filling time graph to Stats

This commit is contained in:
gregoirenovel 2017-05-26 17:27:23 +02:00
parent faa547e891
commit 711f21c458
3 changed files with 109 additions and 0 deletions

View file

@ -1,6 +1,8 @@
class StatsController < ApplicationController
layout "new_application"
MEAN_NUMBER_OF_CHAMPS_IN_A_FORM = 24.0
def index
procedures = Procedure.where(:published => true)
dossiers = Dossier.where.not(:state => :draft)
@ -18,6 +20,7 @@ class StatsController < ApplicationController
@dossiers_count = dossiers.count
@dossier_instruction_mean_time = dossier_instruction_mean_time(dossiers)
@dossier_filling_mean_time = dossier_filling_mean_time(dossiers)
end
private
@ -114,4 +117,49 @@ class StatsController < ApplicationController
[month, month_average]
end.to_h
end
def dossier_filling_mean_time(dossiers)
# In the 12 last months, we compute for each month
# the average time it took to fill a dossier
# We compute monthly averages by first making an average per procedure
# and then computing the average for all the procedures
# For each procedure, we normalize the data: the time is calculated
# for a 24 champs form (the current form mean length)
min_date = 11.months.ago
max_date = Time.now.to_date
processed_dossiers = dossiers
.where(:processed_at => min_date..max_date)
.pluck(:procedure_id, :created_at, :initiated_at, :processed_at)
# Group dossiers by month
processed_dossiers_by_month = processed_dossiers
.group_by do |e|
e[3].beginning_of_month.to_s
end
processed_dossiers_by_month.map do |month, value|
# Group the dossiers for this month by procedure
dossiers_grouped_by_procedure = value.group_by { |dossier| dossier[0] }
# 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_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_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count)
end
# Compute the average mean time for all the procedures of this month
month_average = mean(procedure_processing_times)
[month, month_average]
end.to_h
end
end

View file

@ -73,4 +73,13 @@
:ytitle => "Jours",
:colors => ["rgba(61, 149, 236, 1)"]
.stat-card.stat-card-half.pull-left
%span.stat-card-title Temps de remplissage moyen d'un dossier
.chart-container
.chart
= line_chart @dossier_filling_mean_time,
:ytitle => "Minutes",
:colors => ["rgba(61, 149, 236, 1)"]
.clearfix

View file

@ -170,4 +170,56 @@ describe StatsController, type: :controller do
it { expect(subject).to eq(@expected_hash) }
end
describe "#dossier_filling_mean_time" do
# Month-2: mean 30 minutes
# procedure_1: mean 20 minutes
# dossier_p1_a: 30 minutes
# dossier_p1_b: 10 minutes
# procedure_2: mean 40 minutes
# dossier_p2_a: 80 minutes, for twice the fields
#
# Month-1: mean 50 minutes
# procedure_1: mean 50 minutes
# dossier_p1_c: 50 minutes
before do
procedure_1 = FactoryGirl.create(:procedure, :with_type_de_champ, :types_de_champ_count => 24)
procedure_2 = FactoryGirl.create(:procedure, :with_type_de_champ, :types_de_champ_count => 48)
dossier_p1_a = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:created_at => 2.months.ago.beginning_of_month,
:initiated_at => 2.months.ago.beginning_of_month + 30.minutes,
:processed_at => 2.months.ago.beginning_of_month + 1.day)
dossier_p1_b = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:created_at => 2.months.ago.beginning_of_month,
:initiated_at => 2.months.ago.beginning_of_month + 10.minutes,
:processed_at => 2.months.ago.beginning_of_month + 1.day)
dossier_p1_c = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:created_at => 1.months.ago.beginning_of_month,
:initiated_at => 1.months.ago.beginning_of_month + 50.minutes,
:processed_at => 1.months.ago.beginning_of_month + 1.day)
dossier_p2_a = FactoryGirl.create(:dossier,
:procedure => procedure_2,
:created_at => 2.month.ago.beginning_of_month,
:initiated_at => 2.month.ago.beginning_of_month + 80.minutes,
:processed_at => 2.month.ago.beginning_of_month + 1.day)
# Write directly in the DB to avoid the before_validation hook
Dossier.update_all(state: "closed")
@expected_hash = {
"#{2.months.ago.beginning_of_month}" => 30.0,
"#{1.months.ago.beginning_of_month}" => 50.0
}
end
let (:association) { Dossier.where.not(:state => :draft) }
subject { StatsController.new.send(:dossier_filling_mean_time, association) }
it { expect(subject).to eq(@expected_hash) }
end
end