move stats methods to appropriate concern class
This commit is contained in:
parent
98f0eb5ba9
commit
a8840faadb
4 changed files with 136 additions and 132 deletions
|
@ -51,4 +51,53 @@ module ProcedureStatsConcern
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def usual_traitement_time
|
||||
compute_usual_traitement_time_for_month(Time.zone.now)
|
||||
end
|
||||
|
||||
def compute_usual_traitement_time_for_month(month_date)
|
||||
times = Traitement.includes(:dossier)
|
||||
.where(dossier: self.dossiers)
|
||||
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
|
||||
.where(processed_at: (month_date - 1.month)..month_date)
|
||||
.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 usual_traitement_time_by_month
|
||||
first_processed_at = Traitement.includes(:dossier)
|
||||
.where(dossier: self.dossiers)
|
||||
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
|
||||
.order(:processed_at)
|
||||
.pick(:processed_at)
|
||||
|
||||
return [] if first_processed_at.nil?
|
||||
month_index = first_processed_at.at_end_of_month
|
||||
month_range = []
|
||||
while month_index <= Time.zone.now.at_end_of_month
|
||||
month_range << month_index
|
||||
month_index += 1.month
|
||||
end
|
||||
|
||||
month_range.map do |month|
|
||||
[I18n.l(month, format: "%B %Y"), compute_usual_traitement_time_for_month(month)]
|
||||
end
|
||||
end
|
||||
|
||||
def usual_traitement_time_by_month_in_days
|
||||
usual_traitement_time_by_month.map do |month, time_in_seconds|
|
||||
if time_in_seconds.present?
|
||||
time_in_days = (time_in_seconds / 60.0 / 60.0 / 24.0).ceil
|
||||
else
|
||||
time_in_days = nil
|
||||
end
|
||||
[month, time_in_days]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -565,54 +565,6 @@ class Procedure < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def usual_traitement_time
|
||||
compute_usual_traitement_time_for_month(Time.zone.now)
|
||||
end
|
||||
|
||||
def compute_usual_traitement_time_for_month(month_date)
|
||||
times = Traitement.includes(:dossier)
|
||||
.where(dossier: self.dossiers)
|
||||
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
|
||||
.where(processed_at: (month_date - 1.month)..month_date)
|
||||
.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 usual_traitement_time_by_month
|
||||
first_processed_at = Traitement.includes(:dossier)
|
||||
.where(dossier: self.dossiers)
|
||||
.where.not('dossiers.en_construction_at' => nil, :processed_at => nil)
|
||||
.order(:processed_at)
|
||||
.pick(:processed_at)
|
||||
|
||||
return [] if first_processed_at.nil?
|
||||
month_index = first_processed_at.at_end_of_month
|
||||
month_range = []
|
||||
while month_index <= Time.zone.now.at_end_of_month
|
||||
month_range << month_index
|
||||
month_index += 1.month
|
||||
end
|
||||
|
||||
month_range.map do |month|
|
||||
[I18n.l(month, format: "%B %Y"), compute_usual_traitement_time_for_month(month)]
|
||||
end
|
||||
end
|
||||
|
||||
def usual_traitement_time_by_month_in_days
|
||||
usual_traitement_time_by_month.map do |month, time_in_seconds|
|
||||
if time_in_seconds.present?
|
||||
time_in_days = (time_in_seconds / 60.0 / 60.0 / 24.0).ceil
|
||||
else
|
||||
time_in_days = nil
|
||||
end
|
||||
[month, time_in_days]
|
||||
end
|
||||
end
|
||||
|
||||
def populate_champ_stable_ids
|
||||
TypeDeChamp
|
||||
.joins(:revisions)
|
||||
|
|
87
spec/models/concern/procedure_stats_concern_spec.rb
Normal file
87
spec/models/concern/procedure_stats_concern_spec.rb
Normal file
|
@ -0,0 +1,87 @@
|
|||
describe ProcedureStatsConcern do
|
||||
describe '#usual_traitement_time' do
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
before do
|
||||
Timecop.freeze(Time.utc(2019, 6, 1, 12, 0))
|
||||
|
||||
delays.each do |delay|
|
||||
create_dossier(construction_date: 1.week.ago - delay, instruction_date: 1.week.ago - delay + 12.hours, processed_date: 1.week.ago)
|
||||
end
|
||||
end
|
||||
|
||||
after { Timecop.return }
|
||||
|
||||
context 'when there are several processed dossiers' do
|
||||
let(: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_traitement_time).to be_between(3.days, 4.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are very old dossiers' do
|
||||
let(:delays) { [2.days, 2.days] }
|
||||
let!(:old_dossier) { create_dossier(construction_date: 3.months.ago, instruction_date: 2.months.ago, processed_date: 2.months.ago) }
|
||||
|
||||
it 'ignores dossiers older than 1 month' do
|
||||
expect(procedure.usual_traitement_time).to be_within(1.hour).of(2.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is a dossier with bad data' do
|
||||
let(:delays) { [2.days, 2.days] }
|
||||
let!(:bad_dossier) { create_dossier(construction_date: nil, instruction_date: nil, processed_date: 10.days.ago) }
|
||||
|
||||
it 'ignores bad dossiers' do
|
||||
expect(procedure.usual_traitement_time).to be_within(1.hour).of(2.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is only one processed dossier' do
|
||||
let(:delays) { [1.day] }
|
||||
it { expect(procedure.usual_traitement_time).to be_within(1.hour).of(1.day) }
|
||||
end
|
||||
|
||||
context 'where there is no processed dossier' do
|
||||
let(:delays) { [] }
|
||||
it { expect(procedure.usual_traitement_time).to be_nil }
|
||||
end
|
||||
end
|
||||
|
||||
describe '.compute_usual_traitement_time_for_month' do
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
def create_dossiers(delays_by_month)
|
||||
delays_by_month.each_with_index do |delays, index|
|
||||
delays.each do |delay|
|
||||
processed_date = (index.months + 1.week).ago
|
||||
create_dossier(construction_date: processed_date - delay, instruction_date: processed_date - delay + 12.hours, processed_date: processed_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
Timecop.freeze(Time.utc(2019, 6, 1, 12, 0))
|
||||
|
||||
create_dossiers(delays_by_month)
|
||||
end
|
||||
|
||||
after { Timecop.return }
|
||||
|
||||
context 'when there are several processed dossiers' do
|
||||
let(:delays_by_month) { [[1.day, 2.days, 2.days, 2.days, 2.days, 3.days, 3.days, 3.days, 3.days, 12.days], [1.month, 2.months, 2.months, 2.months]] }
|
||||
|
||||
it 'computes a time representative of the dossier instruction delay for a specific month' do
|
||||
expect(procedure.compute_usual_traitement_time_for_month(Date.strptime("2019-05", "%Y-%m"))).to be_between(1.month, 2.months)
|
||||
expect(procedure.compute_usual_traitement_time_for_month(Date.strptime("2019-06", "%Y-%m"))).to be_between(3.days, 4.days)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_dossier(construction_date:, instruction_date:, processed_date:)
|
||||
dossier = create(:dossier, :accepte, procedure: procedure, en_construction_at: construction_date, en_instruction_at: instruction_date, processed_at: processed_date)
|
||||
end
|
||||
end
|
|
@ -980,86 +980,6 @@ describe Procedure do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#usual_traitement_time' do
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
before do
|
||||
Timecop.freeze(Time.utc(2019, 6, 1, 12, 0))
|
||||
|
||||
delays.each do |delay|
|
||||
create_dossier(construction_date: 1.week.ago - delay, instruction_date: 1.week.ago - delay + 12.hours, processed_date: 1.week.ago)
|
||||
end
|
||||
end
|
||||
|
||||
after { Timecop.return }
|
||||
|
||||
context 'when there are several processed dossiers' do
|
||||
let(: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_traitement_time).to be_between(3.days, 4.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are very old dossiers' do
|
||||
let(:delays) { [2.days, 2.days] }
|
||||
let!(:old_dossier) { create_dossier(construction_date: 3.months.ago, instruction_date: 2.months.ago, processed_date: 2.months.ago) }
|
||||
|
||||
it 'ignores dossiers older than 1 month' do
|
||||
expect(procedure.usual_traitement_time).to be_within(1.hour).of(2.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is a dossier with bad data' do
|
||||
let(:delays) { [2.days, 2.days] }
|
||||
let!(:bad_dossier) { create_dossier(construction_date: nil, instruction_date: nil, processed_date: 10.days.ago) }
|
||||
|
||||
it 'ignores bad dossiers' do
|
||||
expect(procedure.usual_traitement_time).to be_within(1.hour).of(2.days)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is only one processed dossier' do
|
||||
let(:delays) { [1.day] }
|
||||
it { expect(procedure.usual_traitement_time).to be_within(1.hour).of(1.day) }
|
||||
end
|
||||
|
||||
context 'where there is no processed dossier' do
|
||||
let(:delays) { [] }
|
||||
it { expect(procedure.usual_traitement_time).to be_nil }
|
||||
end
|
||||
end
|
||||
|
||||
describe '.compute_usual_traitement_time_for_month' do
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
def create_dossiers(delays_by_month)
|
||||
delays_by_month.each_with_index do |delays, index|
|
||||
delays.each do |delay|
|
||||
processed_date = (index.months + 1.week).ago
|
||||
create_dossier(construction_date: processed_date - delay, instruction_date: processed_date - delay + 12.hours, processed_date: processed_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
Timecop.freeze(Time.utc(2019, 6, 1, 12, 0))
|
||||
|
||||
create_dossiers(delays_by_month)
|
||||
end
|
||||
|
||||
after { Timecop.return }
|
||||
|
||||
context 'when there are several processed dossiers' do
|
||||
let(:delays_by_month) { [[1.day, 2.days, 2.days, 2.days, 2.days, 3.days, 3.days, 3.days, 3.days, 12.days], [1.month, 2.months, 2.months, 2.months]] }
|
||||
|
||||
it 'computes a time representative of the dossier instruction delay for a specific month' do
|
||||
expect(procedure.compute_usual_traitement_time_for_month(Date.strptime("2019-05", "%Y-%m"))).to be_between(1.month, 2.months)
|
||||
expect(procedure.compute_usual_traitement_time_for_month(Date.strptime("2019-06", "%Y-%m"))).to be_between(3.days, 4.days)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '.ensure_a_groupe_instructeur_exists' do
|
||||
let!(:procedure) { create(:procedure) }
|
||||
|
||||
|
@ -1116,8 +1036,4 @@ describe Procedure do
|
|||
dossier = create(:dossier, :accepte, procedure: procedure)
|
||||
create(:champ_piece_justificative, size: size, dossier: dossier)
|
||||
end
|
||||
|
||||
def create_dossier(construction_date:, instruction_date:, processed_date:)
|
||||
dossier = create(:dossier, :accepte, procedure: procedure, en_construction_at: construction_date, en_instruction_at: instruction_date, processed_at: processed_date)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue