2017-03-24 18:04:37 +01:00
|
|
|
class StatsController < ApplicationController
|
|
|
|
|
|
|
|
def index
|
2017-04-03 15:02:12 +02:00
|
|
|
procedures = Procedure.where(:published => true)
|
|
|
|
dossiers = Dossier.where.not(:state => :draft)
|
2017-04-03 15:00:26 +02:00
|
|
|
|
2017-04-03 16:26:05 +02:00
|
|
|
@procedures_30_days_flow = thirty_days_flow_hash(procedures)
|
|
|
|
@dossiers_30_days_flow = thirty_days_flow_hash(dossiers)
|
2017-03-28 15:42:48 +02:00
|
|
|
|
|
|
|
@procedures_cumulative = cumulative_hash(procedures)
|
|
|
|
@dossiers_cumulative = cumulative_hash(dossiers)
|
2017-04-03 17:04:31 +02:00
|
|
|
|
|
|
|
@procedures_count = procedures.count
|
|
|
|
@dossiers_count = dossiers.count
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-03 16:26:05 +02:00
|
|
|
def thirty_days_flow_hash(association)
|
|
|
|
thirty_days_flow_hash = association
|
|
|
|
.where(:created_at => 30.days.ago..Time.now)
|
|
|
|
.group("date_trunc('day', created_at)")
|
|
|
|
.count
|
|
|
|
|
|
|
|
clean_hash(thirty_days_flow_hash)
|
|
|
|
end
|
|
|
|
|
2017-03-24 18:04:37 +01:00
|
|
|
def clean_hash h
|
|
|
|
h.keys.each{ |key| h[key.to_date] = h[key]; h.delete(key) }
|
|
|
|
min_date = h.keys.min
|
|
|
|
max_date = h.keys.max
|
|
|
|
(min_date..max_date).each do |date|
|
|
|
|
h[date] = 0 if h[date].nil?
|
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
2017-03-28 15:42:48 +02:00
|
|
|
|
|
|
|
def cumulative_hash(association)
|
|
|
|
sum = 0
|
|
|
|
association
|
|
|
|
.group("DATE_TRUNC('month', created_at)")
|
|
|
|
.count
|
|
|
|
.to_a
|
|
|
|
.sort{ |x, y| x[0] <=> y[0] }
|
|
|
|
.map { |x, y| { x => (sum += y)} }
|
|
|
|
.reduce({}, :merge)
|
|
|
|
end
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|