Merge pull request #152 from sgmap/fix-stats

Fix stats
This commit is contained in:
gregoirenovel 2017-05-05 12:04:32 +02:00 committed by GitHub
commit 7a597a802c
2 changed files with 70 additions and 26 deletions

View file

@ -5,10 +5,10 @@ class StatsController < ApplicationController
dossiers = Dossier.where.not(:state => :draft)
@procedures_30_days_flow = thirty_days_flow_hash(procedures)
@dossiers_30_days_flow = thirty_days_flow_hash(dossiers)
@dossiers_30_days_flow = thirty_days_flow_hash(dossiers, :initiated_at)
@procedures_cumulative = cumulative_hash(procedures)
@dossiers_cumulative = cumulative_hash(dossiers)
@dossiers_cumulative = cumulative_hash(dossiers, :initiated_at)
@procedures_count = procedures.count
@dossiers_count = dossiers.count
@ -16,13 +16,13 @@ class StatsController < ApplicationController
private
def thirty_days_flow_hash(association)
def thirty_days_flow_hash(association, date_attribute = :created_at)
min_date = 30.days.ago.to_date
max_date = Time.now.to_date
thirty_days_flow_hash = association
.where(:created_at => min_date..max_date)
.group("date_trunc('day', created_at)")
.where(date_attribute => min_date..max_date)
.group("date_trunc('day', #{date_attribute.to_s})")
.count
clean_hash(thirty_days_flow_hash, min_date, max_date)
@ -42,10 +42,10 @@ class StatsController < ApplicationController
h
end
def cumulative_hash(association)
def cumulative_hash(association, date_attribute = :created_at)
sum = 0
association
.group("DATE_TRUNC('month', created_at)")
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
.count
.to_a
.sort{ |x, y| x[0] <=> y[0] }

View file

@ -2,42 +2,86 @@ require 'spec_helper'
describe StatsController, type: :controller do
describe '#thirty_days_flow_hash' do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 1.day.ago)
context "without a date_attribut" do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 1.day.ago)
@expected_hash = {}
(30.days.ago.to_date..Time.now.to_date).each do |day|
if [15.days.ago.to_date, 1.day.ago.to_date].include?(day)
@expected_hash[day] = 1
else
@expected_hash[day] = 0
@expected_hash = {}
(30.days.ago.to_date..Time.now.to_date).each do |day|
if [15.days.ago.to_date, 1.day.ago.to_date].include?(day)
@expected_hash[day] = 1
else
@expected_hash[day] = 0
end
end
end
let (:association) { Procedure.all }
subject { StatsController.new.send(:thirty_days_flow_hash, association) }
it { expect(subject).to eq(@expected_hash) }
end
let (:association) { Procedure.all }
context "with a date_attribut" do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago, :updated_at => 50.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 10.days.ago)
FactoryGirl.create(:procedure, :created_at => 1.day.ago, :updated_at => 3.days.ago)
subject { StatsController.new.send(:thirty_days_flow_hash, association) }
@expected_hash = {}
(30.days.ago.to_date..Time.now.to_date).each do |day|
if [10.days.ago.to_date, 3.day.ago.to_date].include?(day)
@expected_hash[day] = 1
else
@expected_hash[day] = 0
end
end
end
it { expect(subject).to eq(@expected_hash) }
let (:association) { Procedure.all }
subject { StatsController.new.send(:thirty_days_flow_hash, association, :updated_at) }
it { expect(subject).to eq(@expected_hash) }
end
end
describe '#cumulative_hash' do
context "without a date attribute" do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
end
let (:association) { Procedure.all }
subject { StatsController.new.send(:cumulative_hash, association) }
it { expect(subject).to eq({
45.days.ago.beginning_of_month => 1,
15.days.ago.beginning_of_month => 3
}) }
end
end
context "with a date attribute" do
before do
FactoryGirl.create(:procedure, :created_at => 45.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago)
FactoryGirl.create(:procedure, :created_at => 45.days.ago, :updated_at => 20.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 20.days.ago)
FactoryGirl.create(:procedure, :created_at => 15.days.ago, :updated_at => 10.days.ago)
end
let (:association) { Procedure.all }
subject { StatsController.new.send(:cumulative_hash, association) }
subject { StatsController.new.send(:cumulative_hash, association, :updated_at) }
it { expect(subject).to eq({
45.days.ago.beginning_of_month => 1,
15.days.ago.beginning_of_month => 3
20.days.ago.beginning_of_month => 2,
10.days.ago.beginning_of_month => 3
}) }
end
end