Add the weekly avis answer time average to Stats

This commit is contained in:
Mathieu Magnin 2017-05-09 18:11:58 +02:00 committed by gregoirenovel
parent b5f1b898c7
commit 068b635b7c
4 changed files with 45 additions and 1 deletions

View file

@ -22,6 +22,7 @@ class StatsController < ApplicationController
@dossier_filling_mean_time = dossier_filling_mean_time(dossiers)
@avis_usage = avis_usage
@avis_average_answer_time = avis_average_answer_time
end
private
@ -165,4 +166,18 @@ class StatsController < ApplicationController
[min_date.to_i, result]
end
end
def avis_average_answer_time
[3.week.ago, 2.week.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 updated_at - created_at) / 86400")
result = average ? average.to_f.round(2) : 0
[min_date.to_i, result]
end
end
end

View file

@ -22,7 +22,7 @@ class Avis < ApplicationRecord
Avis.where(email: gestionnaire.email).update_all(email: nil, gestionnaire_id: gestionnaire.id)
end
def self.avis_exists_and_email_belongs_to_avis?(avis_id, email)
def self.avis_exists_and_email_belongs_to_avis?(avis_id, email)
avis = Avis.find_by(id: avis_id)
avis.present? && avis.email == email
end

View file

@ -86,4 +86,8 @@
%span.stat-card-title Taux d'utilisation des avis
= line_chart @avis_usage, ytitle: 'dossiers avec avis / total dossiers', xtitle: 'semaines'
.stat-card.stat-card-half.pull-left
%span.stat-card-title Temps de réponse moyen par avis
= line_chart @avis_average_answer_time, ytitle: 'jours', xtitle: 'semaines'
.clearfix

View file

@ -224,4 +224,29 @@ describe StatsController, type: :controller do
it { expect(subject).to match([[3.week.ago.to_i, 0], [2.week.ago.to_i, 0], [1.week.ago.to_i, 33.33]]) }
end
describe "#avis_average_answer_time" do
before do
# 1 week ago
create(:avis, answer: "voila ma réponse", created_at: 1.week.ago + 1.day, updated_at: 1.week.ago + 2.days) # 1 day
create(:avis, created_at: 1.week.ago + 2.days)
# 2 weeks ago
create(:avis, answer: "voila ma réponse", created_at: 2.week.ago + 1.day, updated_at: 2.week.ago + 2.days) # 1 day
create(:avis, answer: "voila ma réponse2", created_at: 2.week.ago + 3.days, updated_at: 1.week.ago + 6.days) # 10 days
create(:avis, answer: "voila ma réponse2", created_at: 2.week.ago + 2.days, updated_at: 1.week.ago + 6.days) # 11 days
create(:avis, created_at: 2.week.ago + 1.day, updated_at: 2.week.ago + 2.days)
# 3 weeks ago
create(:avis, answer: "voila ma réponse2", created_at: 3.weeks.ago + 1.day, updated_at: 3.weeks.ago + 2.days) # 1 day
create(:avis, answer: "voila ma réponse2", created_at: 3.weeks.ago + 1.day, updated_at: 1.week.ago + 5.days) # 18 day
end
subject { StatsController.new.send(:avis_average_answer_time) }
it { expect(subject.count).to eq(3) }
it { is_expected.to include [1.week.ago.to_i, 1.0] }
it { is_expected.to include [2.week.ago.to_i, 7.33] }
it { is_expected.to include [3.week.ago.to_i, 9.5] }
end
end