Merge pull request #7983 from mfo/US/fix-archive-count

correctif(administrateur/procedure/archives#index): les compteurs sont desynchronisés
This commit is contained in:
mfo 2022-11-01 12:50:12 +01:00 committed by GitHub
commit c63afc7f1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 90 deletions

View file

@ -8,7 +8,7 @@ module Administrateurs
def index
@exports = Export.find_for_groupe_instructeurs(all_groupe_instructeurs.map(&:id), nil)
@average_dossier_weight = @procedure.average_dossier_weight
@count_dossiers_termines_by_month = Traitement.count_dossiers_termines_by_month(all_groupe_instructeurs)
@count_dossiers_termines_by_month = @procedure.dossiers.processed_by_month(all_groupe_instructeurs).count
@archives = Archive.for_groupe_instructeur(all_groupe_instructeurs).to_a
end

View file

@ -7,7 +7,7 @@ module Instructeurs
def index
@average_dossier_weight = @procedure.average_dossier_weight
@count_dossiers_termines_by_month = Traitement.count_dossiers_termines_by_month(groupe_instructeurs)
@count_dossiers_termines_by_month = @procedure.dossiers.processed_by_month(groupe_instructeurs).count
@archives = Archive.for_groupe_instructeur(groupe_instructeurs).to_a
end

View file

@ -235,11 +235,16 @@ class Dossier < ApplicationRecord
scope :en_instruction, -> { not_archived.state_en_instruction }
scope :termine, -> { not_archived.state_termine }
scope :processed_by_month, -> (all_groupe_instructeurs) {
state_termine
.where(groupe_instructeurs: all_groupe_instructeurs)
.group_by_period(:month, :processed_at, reverse: true)
}
scope :processed_in_month, -> (date) do
date = date.to_datetime
state_termine
.joins(:traitements)
.where(traitements: { processed_at: date.beginning_of_month..date.end_of_month })
.where(processed_at: date.beginning_of_month..date.end_of_month)
end
scope :ordered_for_export, -> {
order(depose_at: 'asc')

View file

@ -25,30 +25,4 @@ class Traitement < ApplicationRecord
.where.not('dossiers.depose_at' => nil, processed_at: nil)
.order(:processed_at)
end
scope :termine_close_to_expiration, -> do
joins(dossier: :procedure)
.termine
.where(process_expired: true)
.where('dossiers.state' => Dossier::TERMINE)
.where("traitements.processed_at + (procedures.duree_conservation_dossiers_dans_ds * INTERVAL '1 month') - INTERVAL :expires_in < :now", { now: Time.zone.now, expires_in: Dossier::INTERVAL_BEFORE_EXPIRATION })
end
def self.count_dossiers_termines_by_month(groupe_instructeurs)
last_traitements_per_dossier = Traitement
.select('max(traitements.processed_at) as processed_at')
.termine
.where(dossier: Dossier.state_termine.where(groupe_instructeur: groupe_instructeurs))
.group(:dossier_id)
.to_sql
sql = <<~EOF
select date_trunc('month', r1.processed_at::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.formatted_offset}'::INTERVAL) as month, count(r1.processed_at)
from (#{last_traitements_per_dossier}) as r1
group by date_trunc('month', r1.processed_at::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.formatted_offset}'::INTERVAL)
order by month desc
EOF
ActiveRecord::Base.connection.execute(sql)
end
end

View file

@ -7,17 +7,15 @@
%th.center Télécharger
%tbody
- count_dossiers_termines_by_month.each do |count_by_month|
- month = count_by_month["month"].to_date
- nb_dossiers_termines = count_by_month["count"]
- matching_archive = archives.find { |archive| archive.time_span_type == 'monthly' && archive.month == month }
- weight = estimate_weight(matching_archive, nb_dossiers_termines, average_dossier_weight)
- count_dossiers_termines_by_month.each do |date, count|
- matching_archive = archives.find { |archive| archive.time_span_type == 'monthly' && archive.month == date.month }
- weight = estimate_weight(matching_archive, count, average_dossier_weight)
%tr
%td
= I18n.l(month, format: "%B %Y").capitalize
= I18n.l(date, format: "%B %Y").capitalize
%td.text-right
= nb_dossiers_termines
= count
%td.text-right
= number_to_human_size(weight)
%td.center
@ -30,7 +28,7 @@
%span.icon.retry
= t(:archive_pending_html, scope: [:instructeurs, :procedure], created_period: time_ago_in_words(matching_archive.created_at))
- elsif weight.nil? || weight < Archive::MAX_SIZE
= link_to create_archive_url(procedure, month), method: :post, class: "button" do
= link_to create_archive_url(procedure, date), method: :post, class: "button" do
%span.icon.new-folder
Demander la création
- else

View file

@ -1709,4 +1709,50 @@ describe Dossier do
end
end
end
describe '#processed_by_month' do
let(:procedure) { create(:procedure, :published, groupe_instructeurs: [groupe_instructeurs]) }
let(:groupe_instructeurs) { create(:groupe_instructeur) }
before do
create_dossier_for_month(procedure, 2021, 3)
create_dossier_for_month(procedure, 2021, 3)
create_archived_dossier_for_month(procedure, 2021, 3)
create_dossier_for_month(procedure, 2021, 2)
end
subject do
Timecop.freeze(Time.zone.local(2021, 3, 5)) do
Dossier.processed_by_month(groupe_instructeurs).count
end
end
it 'count dossiers_termines by month' do
expect(count_for_month(subject, 3)).to eq 3
expect(count_for_month(subject, 2)).to eq 1
end
it 'returns descending order by month' do
expect(subject.keys.first.month).to eq 3
expect(subject.keys.last.month).to eq 2
end
end
private
def count_for_month(processed_by_month, month)
processed_by_month.find { |date, _count| date.month == month }[1]
end
def create_dossier_for_month(procedure, year, month)
Timecop.freeze(Time.zone.local(year, month, 5)) do
create(:dossier, :accepte, :with_attestation, procedure: procedure)
end
end
def create_archived_dossier_for_month(procedure, year, month)
Timecop.freeze(Time.zone.local(year, month, 5)) do
create(:dossier, :accepte, :archived, :with_attestation, procedure: procedure)
end
end
end

View file

@ -1,49 +0,0 @@
describe Traitement do
describe '#count_dossiers_termines_by_month' do
let(:procedure) { create(:procedure, :published, groupe_instructeurs: [groupe_instructeurs]) }
let(:groupe_instructeurs) { create(:groupe_instructeur) }
before do
create_dossier_for_month(procedure, 2021, 3)
create_dossier_for_month(procedure, 2021, 3)
create_archived_dossier_for_month(procedure, 2021, 3)
create_dossier_for_month(procedure, 2021, 2)
end
subject do
Timecop.freeze(Time.zone.local(2021, 3, 5)) do
Traitement.count_dossiers_termines_by_month(groupe_instructeurs)
end
end
it 'count dossiers_termines by month' do
expect(count_for_month(subject, 3)).to eq 3
expect(count_for_month(subject, 2)).to eq 1
end
it 'returns descending order by month' do
expect(subject[0]["month"].to_date.month).to eq 3
expect(subject[1]["month"].to_date.month).to eq 2
end
end
private
def count_for_month(count_dossiers_termines_by_month, month)
count_dossiers_termines_by_month.find do |count_by_month|
count_by_month["month"].to_date.month == month
end["count"]
end
def create_dossier_for_month(procedure, year, month)
Timecop.freeze(Time.zone.local(year, month, 5)) do
create(:dossier, :accepte, :with_attestation, procedure: procedure)
end
end
def create_archived_dossier_for_month(procedure, year, month)
Timecop.freeze(Time.zone.local(year, month, 5)) do
create(:dossier, :accepte, :archived, :with_attestation, procedure: procedure)
end
end
end

View file

@ -3,21 +3,21 @@ RSpec.describe 'shared/archives/_table.html.haml', type: :view do
let(:procedure) { create(:procedure) }
let(:all_archives) { create_list(:archive, 2) }
let(:month_date) { "2022-01-01T00:00:00+00:00" }
let(:month_date) { DateTime.parse("2022-01-01T00:00:00+00:00") }
let(:average_dossier_weight) { 1024 }
before do
allow(view).to receive(:create_archive_url).and_return("/archive/created/stubed")
end
subject { render 'shared/archives/table.html.haml', count_dossiers_termines_by_month: [{ "month" => month_date, "count" => 5 }], archives: all_archives + [monthly_archive].compact, average_dossier_weight: average_dossier_weight, procedure: procedure }
subject { render 'shared/archives/table.html.haml', count_dossiers_termines_by_month: { month_date => 5 }, archives: all_archives + [monthly_archive].compact, average_dossier_weight: average_dossier_weight, procedure: procedure }
context "when archive is available" do
let(:monthly_archive) { create(:archive, time_span_type: "monthly", month: month_date, job_status: :generated, file: Rack::Test::UploadedFile.new('spec/fixtures/files/RIB.pdf', 'application/pdf')) }
it 'renders archive by month with estimate_weight' do
expect(subject).to have_text("Janvier 2022")
expect(subject).to have_text("Télécharger")
expect(subject).to have_text("338 ko")
expect(subject).to have_text("5 ko")
end
end