cache dossiers count

This commit is contained in:
Christophe Robillard 2023-01-06 17:47:28 +01:00
parent bbbcfb5889
commit bd92291f8a
4 changed files with 34 additions and 2 deletions

View file

@ -15,10 +15,12 @@
# closed_at :datetime
# declarative_with_state :string
# description :string
# dossiers_count_computed_at :datetime
# duree_conservation_dossiers_dans_ds :integer
# duree_conservation_etendue_par_ds :boolean default(FALSE)
# encrypted_api_particulier_token :string
# estimated_duration_visible :boolean default(TRUE), not null
# estimated_dossiers_count :integer
# euro_flag :boolean default(FALSE)
# experts_require_administrateur_invitation :boolean default(FALSE)
# for_individual :boolean default(FALSE)
@ -71,6 +73,8 @@ class Procedure < ApplicationRecord
MIN_WEIGHT = 350000
DOSSIERS_COUNT_EXPIRING = 1.hour
attr_encrypted :api_particulier_token
has_many :revisions, -> { order(:id) }, class_name: 'ProcedureRevision', inverse_of: :procedure
@ -836,7 +840,13 @@ class Procedure < ApplicationRecord
self.connection.query(query.to_sql).flatten
end
private
def compute_dossiers_count
now = Time.zone.now
if now > (self.dossiers_count_computed_at || self.created_at) + DOSSIERS_COUNT_EXPIRING
self.update(estimated_dossiers_count: self.dossiers.visible_by_administration.count,
dossiers_count_computed_at: now)
end
end
def move_new_children_to_new_parent_coordinate(new_draft)
children = new_draft.revision_types_de_champ

View file

@ -0,0 +1,6 @@
class AddEstimatedDossiersCountAndDossiersCountComputedAtToProcedures < ActiveRecord::Migration[6.1]
def change
add_column :procedures, :estimated_dossiers_count, :integer
add_column :procedures, :dossiers_count_computed_at, :datetime
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_02_16_141558) do
ActiveRecord::Schema.define(version: 2023_02_17_094119) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
@ -703,12 +703,14 @@ ActiveRecord::Schema.define(version: 2023_02_16_141558) do
t.string "declarative_with_state"
t.string "description"
t.string "direction"
t.datetime "dossiers_count_computed_at"
t.bigint "draft_revision_id"
t.integer "duree_conservation_dossiers_dans_ds"
t.boolean "duree_conservation_etendue_par_ds", default: false
t.boolean "durees_conservation_required", default: true
t.string "encrypted_api_particulier_token"
t.boolean "estimated_duration_visible", default: true, null: false
t.integer "estimated_dossiers_count"
t.boolean "euro_flag", default: false
t.boolean "experts_require_administrateur_invitation", default: false
t.boolean "for_individual", default: false

View file

@ -9,6 +9,20 @@ describe Procedure do
it { expect(subject.without_continuation_mail_template).to be_a(Mails::WithoutContinuationMail) }
end
describe 'compute_dossiers_count' do
let(:procedure) { create(:procedure_with_dossiers, dossiers_count: 2, dossiers_count_computed_at: Time.zone.now - Procedure::DOSSIERS_COUNT_EXPIRING) }
it 'caches estimated_dossiers_count' do
procedure.dossiers.each(&:passer_en_construction!)
expect { procedure.compute_dossiers_count }.to change(procedure, :estimated_dossiers_count).from(nil).to(2)
expect { create(:dossier, procedure: procedure).passer_en_construction! }.not_to change(procedure, :estimated_dossiers_count)
Timecop.freeze(Time.zone.now + Procedure::DOSSIERS_COUNT_EXPIRING)
expect { procedure.compute_dossiers_count }.to change(procedure, :estimated_dossiers_count).from(2).to(3)
Timecop.return
end
end
describe 'initiated_mail' do
let(:procedure) { create(:procedure) }