fix(stats): remove super admin stats
This commit is contained in:
parent
dc52606374
commit
46e2e34b89
3 changed files with 3 additions and 377 deletions
|
@ -7,7 +7,6 @@ class StatsController < ApplicationController
|
|||
stat = Stat.first
|
||||
|
||||
procedures = Procedure.publiees_ou_closes
|
||||
dossiers = Dossier.state_not_brouillon
|
||||
|
||||
@procedures_numbers = procedures_numbers(procedures)
|
||||
|
||||
|
@ -17,7 +16,9 @@ class StatsController < ApplicationController
|
|||
stat.dossiers_deposes_entre_60_et_30_jours
|
||||
)
|
||||
|
||||
@contact_percentage = contact_percentage
|
||||
@contact_percentage = Rails.cache.fetch("stats.contact_percentage", expires_in: 1.day) do
|
||||
contact_percentage
|
||||
end
|
||||
|
||||
@dossiers_states_for_pie = {
|
||||
"Brouillon" => stat.dossiers_brouillon,
|
||||
|
@ -31,25 +32,6 @@ class StatsController < ApplicationController
|
|||
|
||||
@dossiers_cumulative = stat.dossiers_cumulative
|
||||
@dossiers_in_the_last_4_months = stat.dossiers_in_the_last_4_months
|
||||
|
||||
if super_admin_signed_in?
|
||||
@dossier_instruction_mean_time = Rails.cache.fetch("dossier_instruction_mean_time", expires_in: 1.day) do
|
||||
dossier_instruction_mean_time(dossiers)
|
||||
end
|
||||
|
||||
@dossier_filling_mean_time = Rails.cache.fetch("dossier_filling_mean_time", expires_in: 1.day) do
|
||||
dossier_filling_mean_time(dossiers)
|
||||
end
|
||||
|
||||
@avis_usage = avis_usage
|
||||
@avis_average_answer_time = avis_average_answer_time
|
||||
@avis_answer_percentages = avis_answer_percentages
|
||||
|
||||
@motivation_usage_dossier = motivation_usage_dossier
|
||||
@motivation_usage_procedure = motivation_usage_procedure
|
||||
|
||||
@cloned_from_library_procedures_ratio = cloned_from_library_procedures_ratio
|
||||
end
|
||||
end
|
||||
|
||||
def download
|
||||
|
@ -146,22 +128,6 @@ class StatsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def cloned_from_library_procedures_ratio
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |date|
|
||||
min_date = date.beginning_of_week
|
||||
max_date = min_date.end_of_week
|
||||
|
||||
all_procedures = Procedure.created_during(min_date..max_date)
|
||||
cloned_from_library_procedures = all_procedures.cloned_from_library
|
||||
|
||||
denominator = [1, all_procedures.count].max
|
||||
|
||||
ratio = percentage(cloned_from_library_procedures.count, denominator)
|
||||
|
||||
[l(max_date, format: '%d/%m/%Y'), ratio]
|
||||
end
|
||||
end
|
||||
|
||||
def max_date
|
||||
if super_admin_signed_in?
|
||||
Time.zone.now
|
||||
|
@ -193,220 +159,4 @@ class StatsController < ApplicationController
|
|||
.map { |x, y| { x => (sum += y) } }
|
||||
.reduce({}, :merge)
|
||||
end
|
||||
|
||||
def mean(collection)
|
||||
(collection.sum.to_f / collection.size).round(2)
|
||||
end
|
||||
|
||||
def percentage(numerator, denominator)
|
||||
((numerator.to_f / denominator) * 100).round(2)
|
||||
end
|
||||
|
||||
def dossier_instruction_mean_time(dossiers)
|
||||
# In the 12 last months, we compute for each month
|
||||
# the average time it took to instruct a dossier
|
||||
# We compute monthly averages by first making an average per procedure
|
||||
# and then computing the average for all the procedures
|
||||
|
||||
min_date = 11.months.ago
|
||||
max_date = Time.zone.now.to_date
|
||||
|
||||
processed_dossiers = Traitement.includes(:dossier)
|
||||
.where(dossier_id: dossiers)
|
||||
.where('dossiers.state' => Dossier::TERMINE)
|
||||
.where(:processed_at => min_date..max_date)
|
||||
.pluck('dossiers.groupe_instructeur_id', 'dossiers.en_construction_at', :processed_at)
|
||||
|
||||
# Group dossiers by month
|
||||
processed_dossiers_by_month = processed_dossiers
|
||||
.group_by do |dossier|
|
||||
dossier[2].beginning_of_month.to_s
|
||||
end
|
||||
|
||||
processed_dossiers_by_month.map do |month, value|
|
||||
# Group the dossiers for this month by procedure
|
||||
dossiers_grouped_by_groupe_instructeur = value.group_by { |dossier| dossier[0] }
|
||||
|
||||
# Compute the mean time for this procedure
|
||||
procedure_processing_times = dossiers_grouped_by_groupe_instructeur.map do |_procedure_id, procedure_dossiers|
|
||||
procedure_dossiers_processing_time = procedure_dossiers.map do |dossier|
|
||||
(dossier[2] - dossier[1]).to_f / (3600 * 24)
|
||||
end
|
||||
|
||||
mean(procedure_dossiers_processing_time)
|
||||
end
|
||||
|
||||
# Compute the average mean time for all the procedures of this month
|
||||
month_average = mean(procedure_processing_times)
|
||||
|
||||
[month, month_average]
|
||||
end.to_h
|
||||
end
|
||||
|
||||
def dossier_filling_mean_time(dossiers)
|
||||
# In the 12 last months, we compute for each month
|
||||
# the average time it took to fill a dossier
|
||||
# We compute monthly averages by first making an average per procedure
|
||||
# and then computing the average for all the procedures
|
||||
# For each procedure, we normalize the data: the time is calculated
|
||||
# for a 24 champs form (the current form mean length)
|
||||
|
||||
min_date = 11.months.ago
|
||||
max_date = Time.zone.now.to_date
|
||||
|
||||
processed_dossiers = Traitement.includes(:dossier)
|
||||
.where(dossier: dossiers)
|
||||
.where('dossiers.state' => Dossier::TERMINE)
|
||||
.where(:processed_at => min_date..max_date)
|
||||
.pluck(
|
||||
'dossiers.groupe_instructeur_id',
|
||||
Arel.sql('EXTRACT(EPOCH FROM (dossiers.en_construction_at - dossiers.created_at)) / 60 AS processing_time'),
|
||||
:processed_at
|
||||
)
|
||||
|
||||
# Group dossiers by month
|
||||
processed_dossiers_by_month = processed_dossiers
|
||||
.group_by do |(*_, processed_at)|
|
||||
processed_at.beginning_of_month.to_s
|
||||
end
|
||||
|
||||
groupe_instructeur_ids = processed_dossiers.map { |gid, _, _| gid }.uniq
|
||||
groupe_instructeurs = GroupeInstructeur.where(id: groupe_instructeur_ids).pluck(:id, :procedure_id)
|
||||
|
||||
procedure_id_type_de_champs_count = TypeDeChamp
|
||||
.where(private: false)
|
||||
.joins(:revision)
|
||||
.group('procedure_revisions.procedure_id')
|
||||
.count
|
||||
|
||||
groupe_instructeur_id_type_de_champs_count = groupe_instructeurs.reduce({}) do |acc, (gi_id, procedure_id)|
|
||||
acc[gi_id] = procedure_id_type_de_champs_count[procedure_id]
|
||||
acc
|
||||
end
|
||||
|
||||
processed_dossiers_by_month.map do |month, dossier_plucks|
|
||||
# Group the dossiers for this month by procedure
|
||||
dossiers_grouped_by_groupe_instructeur = dossier_plucks.group_by { |(groupe_instructeur_id, *_)| groupe_instructeur_id }
|
||||
|
||||
# Compute the mean time for this procedure
|
||||
procedure_processing_times = dossiers_grouped_by_groupe_instructeur.filter_map do |groupe_instructeur_id, procedure_dossiers|
|
||||
procedure_fields_count = groupe_instructeur_id_type_de_champs_count[groupe_instructeur_id]
|
||||
|
||||
if (procedure_fields_count == 0 || procedure_fields_count.nil?)
|
||||
next
|
||||
end
|
||||
|
||||
procedure_dossiers_processing_time = procedure_dossiers.map { |_, processing_time, _| processing_time }
|
||||
procedure_mean = mean(procedure_dossiers_processing_time)
|
||||
|
||||
# We normalize the data for 24 fields
|
||||
procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count)
|
||||
end
|
||||
|
||||
# Compute the average mean time for all the procedures of this month
|
||||
month_average = mean(procedure_processing_times)
|
||||
|
||||
[month, month_average]
|
||||
end.to_h
|
||||
end
|
||||
|
||||
def avis_usage
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |min_date|
|
||||
max_date = min_date + 1.week
|
||||
|
||||
weekly_dossiers = Dossier.includes(:avis).where(created_at: min_date..max_date).to_a
|
||||
|
||||
weekly_dossiers_count = weekly_dossiers.count
|
||||
|
||||
if weekly_dossiers_count == 0
|
||||
result = 0
|
||||
else
|
||||
weekly_dossier_with_avis_count = weekly_dossiers.count { |dossier| dossier.avis.present? }
|
||||
result = percentage(weekly_dossier_with_avis_count, weekly_dossiers_count)
|
||||
end
|
||||
|
||||
[min_date.to_i, result]
|
||||
end
|
||||
end
|
||||
|
||||
def avis_average_answer_time
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |min_date|
|
||||
max_date = min_date + 1.week
|
||||
|
||||
average = Avis.with_answer
|
||||
.where(created_at: min_date..max_date)
|
||||
.average("EXTRACT(EPOCH FROM avis.updated_at - avis.created_at) / 86400")
|
||||
|
||||
result = average ? average.to_f.round(2) : 0
|
||||
|
||||
[min_date.to_i, result]
|
||||
end
|
||||
end
|
||||
|
||||
def avis_answer_percentages
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |min_date|
|
||||
max_date = min_date + 1.week
|
||||
|
||||
weekly_avis = Avis.where(created_at: min_date..max_date)
|
||||
|
||||
weekly_avis_count = weekly_avis.count
|
||||
|
||||
if weekly_avis_count == 0
|
||||
[min_date.to_i, 0]
|
||||
else
|
||||
answered_weekly_avis_count = weekly_avis.with_answer.count
|
||||
result = percentage(answered_weekly_avis_count, weekly_avis_count)
|
||||
|
||||
[min_date.to_i, result]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def motivation_usage_dossier
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |date|
|
||||
min_date = date.beginning_of_week
|
||||
max_date = date.end_of_week
|
||||
|
||||
weekly_termine_dossiers = Dossier.where(processed_at: min_date..max_date)
|
||||
weekly_termine_dossiers_count = weekly_termine_dossiers.count
|
||||
weekly_termine_dossiers_with_motivation_count = weekly_termine_dossiers.where.not(motivation: nil).count
|
||||
|
||||
if weekly_termine_dossiers_count == 0
|
||||
result = 0
|
||||
else
|
||||
result = percentage(weekly_termine_dossiers_with_motivation_count, weekly_termine_dossiers_count)
|
||||
end
|
||||
|
||||
[l(max_date, format: '%d/%m/%Y'), result]
|
||||
end
|
||||
end
|
||||
|
||||
def motivation_usage_procedure
|
||||
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |date|
|
||||
min_date = date.beginning_of_week
|
||||
max_date = date.end_of_week
|
||||
|
||||
procedures_with_dossier_processed_this_week = Procedure
|
||||
.joins(:dossiers)
|
||||
.where(dossiers: { processed_at: min_date..max_date })
|
||||
|
||||
procedures_with_dossier_processed_this_week_count = procedures_with_dossier_processed_this_week
|
||||
.uniq
|
||||
.count
|
||||
|
||||
procedures_with_dossier_processed_this_week_and_with_motivation_count = procedures_with_dossier_processed_this_week
|
||||
.where
|
||||
.not(dossiers: { motivation: nil })
|
||||
.uniq
|
||||
.count
|
||||
|
||||
if procedures_with_dossier_processed_this_week_count == 0
|
||||
result = 0
|
||||
else
|
||||
result = percentage(procedures_with_dossier_processed_this_week_and_with_motivation_count, procedures_with_dossier_processed_this_week_count)
|
||||
end
|
||||
|
||||
[l(max_date, format: '%d/%m/%Y'), result]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -75,23 +75,6 @@
|
|||
.chart.cumulative-dossiers-chart.hidden
|
||||
= area_chart @dossiers_cumulative
|
||||
|
||||
- if super_admin_signed_in?
|
||||
.stat-card.stat-card-half.pull-left
|
||||
%span.stat-card-title Temps de traitement moyen d’un dossier
|
||||
|
||||
.chart-container
|
||||
.chart
|
||||
= line_chart @dossier_instruction_mean_time,
|
||||
:ytitle => "Jours"
|
||||
|
||||
.stat-card.stat-card-half.pull-left
|
||||
%span.stat-card-title Temps de remplissage moyen d’un dossier
|
||||
|
||||
.chart-container
|
||||
.chart
|
||||
= line_chart @dossier_filling_mean_time,
|
||||
:ytitle => "Minutes"
|
||||
|
||||
.clearfix
|
||||
|
||||
- if super_admin_signed_in?
|
||||
|
|
|
@ -90,111 +90,4 @@ describe StatsController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#dossier_instruction_mean_time" do
|
||||
# Month-2: mean 3 days
|
||||
# procedure_1: mean 2 days
|
||||
# dossier_p1_a: 3 days
|
||||
# dossier_p1_b: 1 days
|
||||
# procedure_2: mean 4 days
|
||||
# dossier_p2_a: 4 days
|
||||
#
|
||||
# Month-1: mean 5 days
|
||||
# procedure_1: mean 5 days
|
||||
# dossier_p1_c: 5 days
|
||||
|
||||
before do
|
||||
procedure_1 = create(:procedure)
|
||||
procedure_2 = create(:procedure)
|
||||
dossier_p1_a = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
en_construction_at: 2.months.ago.beginning_of_month,
|
||||
processed_at: 2.months.ago.beginning_of_month + 3.days)
|
||||
dossier_p1_b = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
en_construction_at: 2.months.ago.beginning_of_month,
|
||||
processed_at: 2.months.ago.beginning_of_month + 1.day)
|
||||
dossier_p1_c = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
en_construction_at: 1.month.ago.beginning_of_month,
|
||||
processed_at: 1.month.ago.beginning_of_month + 5.days)
|
||||
dossier_p2_a = create(:dossier, :accepte,
|
||||
procedure: procedure_2,
|
||||
en_construction_at: 2.months.ago.beginning_of_month,
|
||||
processed_at: 2.months.ago.beginning_of_month + 4.days)
|
||||
|
||||
@expected_hash = {
|
||||
(2.months.ago.beginning_of_month).to_s => 3.0,
|
||||
(1.month.ago.beginning_of_month).to_s => 5.0
|
||||
}
|
||||
end
|
||||
|
||||
let (:association) { Dossier.state_not_brouillon }
|
||||
|
||||
subject { StatsController.new.send(:dossier_instruction_mean_time, association) }
|
||||
|
||||
it { expect(subject).to eq(@expected_hash) }
|
||||
end
|
||||
|
||||
describe "#dossier_filling_mean_time" do
|
||||
# Month-2: mean 30 minutes
|
||||
# procedure_1: mean 20 minutes
|
||||
# dossier_p1_a: 30 minutes
|
||||
# dossier_p1_b: 10 minutes
|
||||
# procedure_2: mean 40 minutes
|
||||
# dossier_p2_a: 80 minutes, for twice the fields
|
||||
#
|
||||
# Month-1: mean 50 minutes
|
||||
# procedure_1: mean 50 minutes
|
||||
# dossier_p1_c: 50 minutes
|
||||
|
||||
before do
|
||||
procedure_1 = create(:procedure, :with_type_de_champ, types_de_champ_count: 24)
|
||||
procedure_2 = create(:procedure, :with_type_de_champ, types_de_champ_count: 48)
|
||||
dossier_p1_a = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
created_at: 2.months.ago.beginning_of_month,
|
||||
en_construction_at: 2.months.ago.beginning_of_month + 30.minutes,
|
||||
processed_at: 2.months.ago.beginning_of_month + 1.day)
|
||||
dossier_p1_b = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
created_at: 2.months.ago.beginning_of_month,
|
||||
en_construction_at: 2.months.ago.beginning_of_month + 10.minutes,
|
||||
processed_at: 2.months.ago.beginning_of_month + 1.day)
|
||||
dossier_p1_c = create(:dossier, :accepte,
|
||||
procedure: procedure_1,
|
||||
created_at: 1.month.ago.beginning_of_month,
|
||||
en_construction_at: 1.month.ago.beginning_of_month + 50.minutes,
|
||||
processed_at: 1.month.ago.beginning_of_month + 1.day)
|
||||
dossier_p2_a = create(:dossier, :accepte,
|
||||
procedure: procedure_2,
|
||||
created_at: 2.months.ago.beginning_of_month,
|
||||
en_construction_at: 2.months.ago.beginning_of_month + 80.minutes,
|
||||
processed_at: 2.months.ago.beginning_of_month + 1.day)
|
||||
|
||||
@expected_hash = {
|
||||
(2.months.ago.beginning_of_month).to_s => 30.0,
|
||||
(1.month.ago.beginning_of_month).to_s => 50.0
|
||||
}
|
||||
end
|
||||
|
||||
let (:association) { Dossier.state_not_brouillon }
|
||||
|
||||
subject { StatsController.new.send(:dossier_filling_mean_time, association) }
|
||||
|
||||
it { expect(subject).to eq(@expected_hash) }
|
||||
end
|
||||
|
||||
describe '#avis_usage' do
|
||||
let!(:dossier) { create(:dossier) }
|
||||
let!(:avis_with_dossier) { create(:avis) }
|
||||
let!(:dossier2) { create(:dossier) }
|
||||
|
||||
before { Timecop.freeze(Time.zone.now) }
|
||||
after { Timecop.return }
|
||||
|
||||
subject { StatsController.new.send(:avis_usage) }
|
||||
|
||||
it { expect(subject).to match([[3.weeks.ago.to_i, 0], [2.weeks.ago.to_i, 0], [1.week.ago.to_i, 33.33]]) }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue