2019-09-17 15:52:38 +02:00
|
|
|
module ProcedureStatsConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2021-06-16 14:57:42 +02:00
|
|
|
NB_DAYS_RECENT_DOSSIERS = 30
|
2021-06-17 14:33:37 +02:00
|
|
|
# Percentage of dossiers considered to compute the 'usual traitement time'.
|
|
|
|
# For instance, a value of '90' means that the usual traitement time will return
|
|
|
|
# the duration under which 90% of the given dossiers are closed.
|
|
|
|
USUAL_TRAITEMENT_TIME_PERCENTILE = 90
|
2021-06-16 14:57:42 +02:00
|
|
|
|
2019-09-17 15:52:38 +02:00
|
|
|
def stats_usual_traitement_time
|
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/stats_usual_traitement_time", expires_in: 12.hours) do
|
2021-06-16 14:57:42 +02:00
|
|
|
usual_traitement_time_for_recent_dossiers(NB_DAYS_RECENT_DOSSIERS)
|
2019-09-17 15:52:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-04 16:19:19 +02:00
|
|
|
def stats_usual_traitement_time_by_month_in_days
|
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/stats_usual_traitement_time_by_month_in_days", expires_in: 12.hours) do
|
|
|
|
usual_traitement_time_by_month_in_days
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-17 15:52:38 +02:00
|
|
|
def stats_dossiers_funnel
|
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/stats_dossiers_funnel", expires_in: 12.hours) do
|
|
|
|
[
|
2023-02-23 17:14:25 +01:00
|
|
|
['Démarrés', dossiers.visible_by_user_or_administration.count + nb_dossiers_termines_supprimes],
|
|
|
|
['Déposés', dossiers.visible_by_administration.count + nb_dossiers_termines_supprimes],
|
|
|
|
['Instruction débutée', dossiers.visible_by_administration.state_instruction_commencee.count + nb_dossiers_termines_supprimes],
|
2023-02-23 13:13:01 +01:00
|
|
|
['Traités', nb_dossiers_termines]
|
2019-09-17 15:52:38 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stats_termines_states
|
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/stats_termines_states", expires_in: 12.hours) do
|
|
|
|
[
|
2023-02-23 13:13:01 +01:00
|
|
|
['Acceptés', percentage(dossiers.visible_by_administration.state_accepte.count, nb_dossiers_termines)],
|
|
|
|
['Refusés', percentage(dossiers.visible_by_administration.state_refuse.count, nb_dossiers_termines)],
|
|
|
|
['Classés sans suite', percentage(dossiers.visible_by_administration.state_sans_suite.count, nb_dossiers_termines)]
|
2019-09-17 15:52:38 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2021-06-02 12:47:07 +02:00
|
|
|
|
|
|
|
def stats_termines_by_week
|
|
|
|
Rails.cache.fetch("#{cache_key_with_version}/stats_termines_by_week", expires_in: 12.hours) do
|
|
|
|
now = Time.zone.now
|
2021-06-08 15:03:06 +02:00
|
|
|
chart_data = dossiers.includes(:traitements)
|
2023-02-23 13:13:01 +01:00
|
|
|
.visible_by_administration
|
2021-06-02 12:47:07 +02:00
|
|
|
.state_termine
|
|
|
|
.where(traitements: { processed_at: (now.beginning_of_week - 6.months)..now.end_of_week })
|
|
|
|
|
|
|
|
dossier_state_values = chart_data.pluck(:state).sort.uniq
|
|
|
|
|
|
|
|
# rubocop:disable Style/HashTransformValues
|
|
|
|
dossier_state_values
|
|
|
|
.map do |state|
|
2021-08-10 15:02:11 +02:00
|
|
|
{
|
|
|
|
name: state,
|
|
|
|
data: chart_data .where(state: state) .group_by_week do |dossier|
|
|
|
|
dossier.traitements.first.processed_at
|
|
|
|
end.map { |k, v| [k, v.count] }.to_h.transform_keys { |week| pretty_week(week) }
|
|
|
|
}
|
2021-06-02 12:47:07 +02:00
|
|
|
# rubocop:enable Style/HashTransformValues
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-06-15 11:33:17 +02:00
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
def traitement_times(date_range)
|
|
|
|
Traitement.for_traitement_time_stats(self)
|
|
|
|
.where(processed_at: date_range)
|
2021-12-06 15:49:17 +01:00
|
|
|
.pluck('dossiers.depose_at', :processed_at)
|
|
|
|
.map { |depose_at, processed_at| { depose_at: depose_at, processed_at: processed_at } }
|
2021-06-16 10:51:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def usual_traitement_time_by_month_in_days
|
|
|
|
traitement_times(first_processed_at..last_considered_processed_at)
|
2021-06-16 15:13:36 +02:00
|
|
|
.group_by { |t| t[:processed_at].beginning_of_month }
|
2021-12-06 15:49:17 +01:00
|
|
|
.transform_values { |month| month.map { |h| h[:processed_at] - h[:depose_at] } }
|
2021-06-17 14:33:37 +02:00
|
|
|
.transform_values { |traitement_times_for_month| traitement_times_for_month.percentile(USUAL_TRAITEMENT_TIME_PERCENTILE).ceil }
|
2021-06-17 10:30:27 +02:00
|
|
|
.transform_values { |seconds| seconds == 0 ? nil : seconds }
|
2021-06-16 15:13:36 +02:00
|
|
|
.transform_values { |seconds| convert_seconds_in_days(seconds) }
|
|
|
|
.transform_keys { |month| pretty_month(month) }
|
2021-06-16 10:51:56 +02:00
|
|
|
end
|
|
|
|
|
2021-06-16 13:23:03 +02:00
|
|
|
def usual_traitement_time_for_recent_dossiers(nb_days)
|
|
|
|
now = Time.zone.now
|
2021-06-17 10:30:27 +02:00
|
|
|
traitement_time =
|
|
|
|
traitement_times((now - nb_days.days)..now)
|
2021-12-06 15:49:17 +01:00
|
|
|
.map { |times| times[:processed_at] - times[:depose_at] }
|
2021-06-17 14:33:37 +02:00
|
|
|
.percentile(USUAL_TRAITEMENT_TIME_PERCENTILE)
|
2021-06-17 10:30:27 +02:00
|
|
|
.ceil
|
|
|
|
|
|
|
|
traitement_time = nil if traitement_time == 0
|
|
|
|
traitement_time
|
2021-06-15 11:33:17 +02:00
|
|
|
end
|
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
private
|
2021-06-16 15:13:36 +02:00
|
|
|
|
2023-02-23 13:13:01 +01:00
|
|
|
def nb_dossiers_termines
|
2023-02-23 17:14:25 +01:00
|
|
|
@nb_dossiers_termines ||= dossiers.visible_by_administration.state_termine.count + nb_dossiers_termines_supprimes
|
|
|
|
end
|
|
|
|
|
|
|
|
def nb_dossiers_termines_supprimes
|
|
|
|
@nb_dossiers_termines_supprimes ||= deleted_dossiers.state_termine.count
|
2023-02-23 13:13:01 +01:00
|
|
|
end
|
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
def first_processed_at
|
|
|
|
Traitement.for_traitement_time_stats(self).pick(:processed_at)
|
|
|
|
end
|
2021-06-15 11:33:17 +02:00
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
def last_considered_processed_at
|
|
|
|
(Time.zone.now - 1.month).end_of_month
|
2021-06-15 11:33:17 +02:00
|
|
|
end
|
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
def convert_seconds_in_days(seconds)
|
|
|
|
(seconds / 60.0 / 60.0 / 24.0).ceil
|
|
|
|
end
|
|
|
|
|
2021-08-10 17:37:42 +02:00
|
|
|
def percentage(value, total)
|
|
|
|
(100 * value / total.to_f).round(1)
|
|
|
|
end
|
|
|
|
|
2021-06-16 10:51:56 +02:00
|
|
|
def pretty_month(month)
|
|
|
|
I18n.l(month, format: "%B %Y")
|
2021-06-15 11:33:17 +02:00
|
|
|
end
|
2021-08-10 15:02:11 +02:00
|
|
|
|
|
|
|
def pretty_week(week)
|
|
|
|
I18n.l(week, format: '%d %b')
|
|
|
|
end
|
2019-09-17 15:52:38 +02:00
|
|
|
end
|