Merge pull request #271 from sgmap/stats_for_avis

Stats for avis
This commit is contained in:
Mathieu Magnin 2017-05-31 14:53:06 +02:00 committed by GitHub
commit 96d743415e
5 changed files with 140 additions and 3 deletions

View file

@ -6,15 +6,25 @@ $blue-hover: rgba(61, 149, 236, 0.8);
$default-space: 15px;
$new-h1-margin-bottom: 4 * $default-space;
$new-h2-margin-bottom: 3 * $default-space;
.new-h1 {
.new-h1,
.new-h2 {
color: $dark-grey;
text-align: center;
margin-bottom: $new-h1-margin-bottom;
font-weight: bold;
}
.new-h1 {
margin-bottom: $new-h1-margin-bottom;
font-size: 41px;
}
.new-h2 {
margin-bottom: $new-h2-margin-bottom;
font-size: 36px;
}
$statistiques-padding-top: $default-space * 2;
.statistiques {

View file

@ -20,6 +20,10 @@ class StatsController < ApplicationController
@dossier_instruction_mean_time = dossier_instruction_mean_time(dossiers)
@dossier_filling_mean_time = dossier_filling_mean_time(dossiers)
@avis_usage = avis_usage
@avis_average_answer_time = avis_average_answer_time
@avis_answer_percentages = avis_answer_percentages
end
private
@ -144,4 +148,56 @@ class StatsController < ApplicationController
[month, month_average]
end.to_h
end
def avis_usage
[3.week.ago, 2.week.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.select { |dossier| dossier.avis.present? }.count
result = ((weekly_dossier_with_avis_count.to_f / weekly_dossiers_count) * 100).round(2)
end
[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
def avis_answer_percentages
[3.week.ago, 2.week.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 = ((answered_weekly_avis_count.to_f / weekly_avis_count) * 100).round(2)
[min_date.to_i, result]
end
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

@ -77,3 +77,21 @@
:colors => ["rgba(61, 149, 236, 1)"]
.clearfix
- if administration_signed_in?
%h2.new-h2 Avis
.stat-cards
.stat-card.stat-card-half.pull-left
%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'
.stat-card.stat-card-half.pull-left
%span.stat-card-title Pourcentage d'avis rempli
= line_chart @avis_answer_percentages, ytitle: 'avis avec reponse / total avis', xtitle: 'semaines'
.clearfix

View file

@ -210,4 +210,57 @@ describe StatsController, type: :controller do
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 do
Timecop.freeze(Time.now)
end
subject { StatsController.new.send(:avis_usage) }
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
describe '#avis_answer_percentages' do
let!(:avis) { create(:avis, created_at: 2.days.ago) }
let!(:avis2) { create(:avis, answer: 'answer', created_at: 2.days.ago) }
let!(:avis3) { create(:avis, answer: 'answer', created_at: 2.days.ago) }
subject { StatsController.new.send(:avis_answer_percentages) }
before do
Timecop.freeze(Time.now)
end
it { is_expected.to match [[3.week.ago.to_i, 0], [2.week.ago.to_i, 0], [1.week.ago.to_i, 66.67]] }
end
end