app: cache the duration estimate for published procedures
This commit is contained in:
parent
3e91a16895
commit
8936461d5b
3 changed files with 46 additions and 4 deletions
|
@ -84,7 +84,8 @@ module ProcedureHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def estimated_fill_duration_minutes(procedure)
|
def estimated_fill_duration_minutes(procedure)
|
||||||
minutes = (procedure.active_revision.estimated_fill_duration / 60.0).round
|
seconds = procedure.active_revision.estimated_fill_duration
|
||||||
|
minutes = (seconds / 60.0).round
|
||||||
[1, minutes].max
|
[1, minutes].max
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -180,8 +180,18 @@ class ProcedureRevision < ApplicationRecord
|
||||||
tdcs_as_json
|
tdcs_as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
# Estimated duration to fill the form, in seconds
|
# Estimated duration to fill the form, in seconds.
|
||||||
|
#
|
||||||
|
# If the revision is locked (i.e. published), the result is cached (because type de champs can no longer be mutated).
|
||||||
def estimated_fill_duration
|
def estimated_fill_duration
|
||||||
|
Rails.cache.fetch("#{cache_key_with_version}/estimated_fill_duration", expires_in: 12.hours, force: !locked?) do
|
||||||
|
compute_estimated_fill_duration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def compute_estimated_fill_duration
|
||||||
tdc_durations = types_de_champ_public.fillable.map do |tdc|
|
tdc_durations = types_de_champ_public.fillable.map do |tdc|
|
||||||
duration = tdc.estimated_fill_duration(self)
|
duration = tdc.estimated_fill_duration(self)
|
||||||
tdc.mandatory ? duration : duration / 2
|
tdc.mandatory ? duration : duration / 2
|
||||||
|
@ -189,8 +199,6 @@ class ProcedureRevision < ApplicationRecord
|
||||||
tdc_durations.sum
|
tdc_durations.sum
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def children_types_de_champ_as_json(tdcs_as_json, parent_tdcs)
|
def children_types_de_champ_as_json(tdcs_as_json, parent_tdcs)
|
||||||
parent_tdcs.each do |parent_tdc|
|
parent_tdcs.each do |parent_tdc|
|
||||||
tdc_as_json = tdcs_as_json.find { |json| json["id"] == parent_tdc.stable_id }
|
tdc_as_json = tdcs_as_json.find { |json| json["id"] == parent_tdc.stable_id }
|
||||||
|
|
|
@ -592,5 +592,38 @@ describe ProcedureRevision do
|
||||||
expect(subject).to eq row_duration * 2.5
|
expect(subject).to eq row_duration * 2.5
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'caching behavior' do
|
||||||
|
let(:procedure) { create(:procedure, :published, types_de_champ: types_de_champ) }
|
||||||
|
|
||||||
|
before { Rails.cache = ActiveSupport::Cache::MemoryStore.new }
|
||||||
|
after { Rails.cache = ActiveSupport::Cache::NullStore.new }
|
||||||
|
|
||||||
|
context 'when a type de champ belonging to a draft revision is updated' do
|
||||||
|
let(:draft_revision) { procedure.draft_revision }
|
||||||
|
|
||||||
|
before do
|
||||||
|
draft_revision.estimated_fill_duration
|
||||||
|
draft_revision.types_de_champ.first.update!(type_champ: TypeDeChamp.type_champs.fetch(:piece_justificative))
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns an up-to-date estimate' do
|
||||||
|
expect(draft_revision.estimated_fill_duration).to eq \
|
||||||
|
TypesDeChamp::TypeDeChampBase::FILL_DURATION_LONG \
|
||||||
|
+ TypesDeChamp::TypeDeChampBase::FILL_DURATION_MEDIUM \
|
||||||
|
+ TypesDeChamp::TypeDeChampBase::FILL_DURATION_LONG \
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the revision is published (and thus immutable)' do
|
||||||
|
let(:published_revision) { procedure.published_revision }
|
||||||
|
|
||||||
|
it 'caches the estimate' do
|
||||||
|
expect(published_revision).to receive(:compute_estimated_fill_duration).once
|
||||||
|
published_revision.estimated_fill_duration
|
||||||
|
published_revision.estimated_fill_duration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue