2017-03-24 18:04:37 +01:00
|
|
|
class StatsController < ApplicationController
|
2017-05-22 16:56:09 +02:00
|
|
|
layout "new_application"
|
2017-03-24 18:04:37 +01:00
|
|
|
|
|
|
|
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)
|
2017-04-26 14:26:03 +02:00
|
|
|
@dossiers_30_days_flow = thirty_days_flow_hash(dossiers, :initiated_at)
|
2017-03-28 15:42:48 +02:00
|
|
|
|
|
|
|
@procedures_cumulative = cumulative_hash(procedures)
|
2017-04-26 14:29:22 +02:00
|
|
|
@dossiers_cumulative = cumulative_hash(dossiers, :initiated_at)
|
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-26 14:24:48 +02:00
|
|
|
def thirty_days_flow_hash(association, date_attribute = :created_at)
|
2017-04-11 16:48:55 +02:00
|
|
|
min_date = 30.days.ago.to_date
|
|
|
|
max_date = Time.now.to_date
|
|
|
|
|
2017-04-03 16:26:05 +02:00
|
|
|
thirty_days_flow_hash = association
|
2017-04-26 14:24:48 +02:00
|
|
|
.where(date_attribute => min_date..max_date)
|
|
|
|
.group("date_trunc('day', #{date_attribute.to_s})")
|
2017-04-03 16:26:05 +02:00
|
|
|
.count
|
|
|
|
|
2017-04-11 16:48:55 +02:00
|
|
|
clean_hash(thirty_days_flow_hash, min_date, max_date)
|
2017-04-03 16:26:05 +02:00
|
|
|
end
|
|
|
|
|
2017-04-11 16:48:55 +02:00
|
|
|
def clean_hash(h, min_date, max_date)
|
|
|
|
# Convert keys to date
|
|
|
|
h = Hash[h.map { |(k, v)| [k.to_date, v] }]
|
|
|
|
|
|
|
|
# Add missing vales where count is 0
|
2017-03-24 18:04:37 +01:00
|
|
|
(min_date..max_date).each do |date|
|
2017-04-11 16:48:55 +02:00
|
|
|
if h[date].nil?
|
|
|
|
h[date] = 0
|
|
|
|
end
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|
2017-04-11 16:48:55 +02:00
|
|
|
|
2017-03-24 18:04:37 +01:00
|
|
|
h
|
|
|
|
end
|
2017-03-28 15:42:48 +02:00
|
|
|
|
2017-04-26 14:28:39 +02:00
|
|
|
def cumulative_hash(association, date_attribute = :created_at)
|
2017-03-28 15:42:48 +02:00
|
|
|
sum = 0
|
|
|
|
association
|
2017-04-26 14:28:39 +02:00
|
|
|
.group("DATE_TRUNC('month', #{date_attribute.to_s})")
|
2017-03-28 15:42:48 +02:00
|
|
|
.count
|
|
|
|
.to_a
|
|
|
|
.sort{ |x, y| x[0] <=> y[0] }
|
|
|
|
.map { |x, y| { x => (sum += y)} }
|
|
|
|
.reduce({}, :merge)
|
|
|
|
end
|
2017-04-11 16:55:13 +02:00
|
|
|
|
2017-03-24 18:04:37 +01:00
|
|
|
end
|