update usual_traitement_time with traitement model

This commit is contained in:
Christophe Robillard 2020-07-06 16:58:08 +02:00
parent a072d35211
commit 0be4b50ade
3 changed files with 21 additions and 18 deletions

View file

@ -438,7 +438,15 @@ class Procedure < ApplicationRecord
end
def usual_traitement_time
percentile_time(:en_construction_at, :processed_at, 90)
times = Traitement.includes(:dossier)
.where(state: Dossier::TERMINE)
.where(processed_at: 1.month.ago..Time.zone.now)
.pluck('dossiers.en_construction_at', :processed_at)
.map { |(en_construction_at, processed_at)| processed_at - en_construction_at }
if times.present?
times.percentile(90).ceil
end
end
def populate_champ_stable_ids
@ -610,18 +618,6 @@ class Procedure < ApplicationRecord
end
end
def percentile_time(start_attribute, end_attribute, p)
times = dossiers
.where.not(start_attribute => nil, end_attribute => nil)
.where(end_attribute => 1.month.ago..Time.zone.now)
.pluck(start_attribute, end_attribute)
.map { |(start_date, end_date)| end_date - start_date }
if times.present?
times.percentile(p).ceil
end
end
def ensure_path_exists
if self.path.blank?
self.path = SecureRandom.uuid

View file

@ -129,13 +129,21 @@ FactoryBot.define do
trait :accepte do
transient do
motivation { nil }
processed_at { nil }
end
after(:create) do |dossier, evaluator|
dossier.state = Dossier.states.fetch(:accepte)
processed_at = evaluator.processed_at
if processed_at.present?
dossier.en_construction_at ||= processed_at - 2.minutes
dossier.en_instruction_at ||= processed_at - 1.minute
dossier.traitements.build(state: Dossier.states.fetch(:accepte), processed_at: processed_at, motivation: evaluator.motivation)
else
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute
dossier.traitements.build(state: Dossier.states.fetch(:accepte), processed_at: dossier.en_instruction_at + 1.minute, motivation: evaluator.motivation)
end
dossier.save!
end
end

View file

@ -958,8 +958,7 @@ describe Procedure do
let(:procedure) { create(:procedure) }
def create_dossier(construction_date:, instruction_date:, processed_date:)
dossier = create(:dossier, :accepte, procedure: procedure)
dossier.update!(en_construction_at: construction_date, en_instruction_at: instruction_date, processed_at: processed_date)
dossier = create(:dossier, :accepte, procedure: procedure, en_construction_at: construction_date, en_instruction_at: instruction_date, processed_at: processed_date)
end
before do