From 27557b8402f63b0eedc2ed4328788aa1bf6da3e0 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 4 Feb 2015 15:05:16 -0500 Subject: [PATCH] ApplicationRequest can return a report --- app/models/application_request.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/models/application_request.rb b/app/models/application_request.rb index 36610234755..24fa623c51a 100644 --- a/app/models/application_request.rb +++ b/app/models/application_request.rb @@ -82,6 +82,28 @@ class ApplicationRequest < ActiveRecord::Base "app_req_#{req_type}#{time.strftime('%Y%m%d')}" end + def self.stats + @stats ||= begin + s = HashWithIndifferentAccess.new({ + all_total: 0, + all_30_days: 0, + all_7_days: 0 + }) + + self.req_types.each do |key, i| + query = self.where(req_type: i) + s["#{key}_total"] = query.sum(:count) + s["#{key}_30_days"] = query.where("date > ?", 30.days.ago).sum(:count) + s["#{key}_7_days"] = query.where("date > ?", 7.days.ago).sum(:count) + + s[:all_total] += s["#{key}_total"] + s[:all_30_days] += s["#{key}_30_days"] + s[:all_7_days] += s["#{key}_7_days"] + end + + s + end + end end # == Schema Information