[Fix #264] Add the mean instruction time graph to Stats

This commit is contained in:
gregoirenovel 2017-05-29 17:19:50 +02:00
parent ed7ba60cf0
commit faa547e891
3 changed files with 101 additions and 0 deletions

View file

@ -16,6 +16,8 @@ class StatsController < ApplicationController
@procedures_count = procedures.count
@dossiers_count = dossiers.count
@dossier_instruction_mean_time = dossier_instruction_mean_time(dossiers)
end
private
@ -70,4 +72,46 @@ class StatsController < ApplicationController
.reduce({}, :merge)
end
def mean(collection)
(collection.sum.to_f / collection.size).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.now.to_date
processed_dossiers = dossiers
.where(:processed_at => min_date..max_date)
.pluck(:procedure_id, :initiated_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_procedure = value.group_by { |dossier| dossier[0] }
# Compute the mean time for this procedure
procedure_processing_times = dossiers_grouped_by_procedure.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
end

View file

@ -64,4 +64,13 @@
%span.big-number-card-number
= number_with_delimiter(@dossiers_count)
.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",
:colors => ["rgba(61, 149, 236, 1)"]
.clearfix

View file

@ -122,4 +122,52 @@ describe StatsController, type: :controller do
}) }
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 = FactoryGirl.create(:procedure)
procedure_2 = FactoryGirl.create(:procedure)
dossier_p1_a = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:initiated_at => 2.months.ago.beginning_of_month,
:processed_at => 2.months.ago.beginning_of_month + 3.days)
dossier_p1_b = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:initiated_at => 2.months.ago.beginning_of_month,
:processed_at => 2.months.ago.beginning_of_month + 1.days)
dossier_p1_c = FactoryGirl.create(:dossier,
:procedure => procedure_1,
:initiated_at => 1.months.ago.beginning_of_month,
:processed_at => 1.months.ago.beginning_of_month + 5.days)
dossier_p2_a = FactoryGirl.create(:dossier,
:procedure => procedure_2,
:initiated_at => 2.month.ago.beginning_of_month,
:processed_at => 2.month.ago.beginning_of_month + 4.days)
# Write directly in the DB to avoid the before_validation hook
Dossier.update_all(state: "closed")
@expected_hash = {
"#{2.months.ago.beginning_of_month}" => 3.0,
"#{1.months.ago.beginning_of_month}" => 5.0
}
end
let (:association) { Dossier.where.not(:state => :draft) }
subject { StatsController.new.send(:dossier_instruction_mean_time, association) }
it { expect(subject).to eq(@expected_hash) }
end
end