procedure: use 90th percentile to estimate the completion delay

This commit is contained in:
Pierre de La Morinerie 2018-11-08 18:09:27 +01:00
parent 5c921182ea
commit e59bec51ef
3 changed files with 62 additions and 17 deletions

View file

@ -1,3 +1,5 @@
require Rails.root.join('lib', 'percentile')
class Procedure < ApplicationRecord class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36 MAX_DUREE_CONSERVATION = 36
@ -305,15 +307,15 @@ class Procedure < ApplicationRecord
end end
def usual_traitement_time def usual_traitement_time
mean_time(:en_construction_at, :processed_at) percentile_time(:en_construction_at, :processed_at, 90)
end end
def usual_verification_time def usual_verification_time
mean_time(:en_construction_at, :en_instruction_at) percentile_time(:en_construction_at, :en_instruction_at, 90)
end end
def usual_instruction_time def usual_instruction_time
mean_time(:en_instruction_at, :processed_at) percentile_time(:en_instruction_at, :processed_at, 90)
end end
PATH_AVAILABLE = :available PATH_AVAILABLE = :available
@ -421,14 +423,14 @@ class Procedure < ApplicationRecord
true true
end end
def mean_time(start_attribute, end_attribute) def percentile_time(start_attribute, end_attribute, p)
times = dossiers times = dossiers
.state_termine .state_termine
.pluck(start_attribute, end_attribute) .pluck(start_attribute, end_attribute)
.map { |(start_date, end_date)| end_date - start_date } .map { |(start_date, end_date)| end_date - start_date }
if times.present? if times.present?
times.sum.fdiv(times.size).ceil times.percentile(p).ceil
end end
end end
end end

31
lib/percentile.rb Normal file
View file

@ -0,0 +1,31 @@
# Adapted from https://github.com/thirtysixthspan/descriptive_statistics
# Copyright (c) 2010-2014 Derrick Parkhurst (derrick.parkhurst@gmail.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
class Array
def percentile(p)
values = self.sort
if values.empty?
return []
elsif values.size == 1
return values.first
elsif p == 100
return values.last
end
rank = p / 100.0 * (values.size - 1)
lower, upper = values[rank.floor, 2]
lower + (upper - lower) * (rank - rank.floor)
end
end

View file

@ -708,19 +708,31 @@ describe Procedure do
describe '#usual_instruction_time' do describe '#usual_instruction_time' do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
context 'when there is only one dossier' do
let(:dossier) { create(:dossier, procedure: procedure) }
context 'which is termine' do
before do before do
dossier.accepte! processed_delays.each do |delay|
processed_date = Time.zone.parse('12/12/2012') dossier = create :dossier, :accepte, procedure: procedure
instruction_date = processed_date - 1.day instruction_date = 1.month.ago
dossier.update(en_instruction_at: instruction_date, processed_at: processed_date) processed_date = instruction_date + delay
dossier.update!(en_instruction_at: instruction_date, processed_at: processed_date)
end
end end
it { expect(procedure.usual_instruction_time).to eq(1.day.to_i) } context 'when there are several processed dossiers' do
let(:processed_delays) { [1.day, 2.days, 2.days, 2.days, 2.days, 3.days, 3.days, 3.days, 3.days, 12.days] }
it 'returns a time representative of the dossier instruction delay' do
expect(procedure.usual_instruction_time).to be_between(3.days, 4.days)
end end
end end
context 'when there is only one processed dossier' do
let(:processed_delays) { [1.day] }
it { expect(procedure.usual_instruction_time).to eq(1.day) }
end
context 'where there is no processed dossier' do
let(:processed_delays) { [] }
it { expect(procedure.usual_instruction_time).to be_nil }
end
end end
end end