Class: Caseflow::Stats
- Inherits:
-
Object
- Object
- Caseflow::Stats
- Defined in:
- app/models/caseflow/stats.rb
Constant Summary collapse
- TIMEZONE =
"Eastern Time (US & Canada)".freeze
- INTERVALS =
[:hourly, :daily, :weekly, :monthly].freeze
- CALCULATIONS =
{}.freeze
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#time ⇒ Object
Returns the value of attribute time.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
- .calculate_all! ⇒ Object
- .now ⇒ Object
- .offset(interval:, time:, offset:) ⇒ Object
- .percentile(attribute, collection, percentile) ⇒ Object
Instance Method Summary collapse
- #calculate_and_save_values! ⇒ Object
- #complete? ⇒ Boolean
-
#initialize(interval:, time:) ⇒ Stats
constructor
A new instance of Stats.
- #range ⇒ Object
- #range_finish ⇒ Object
- #range_start ⇒ Object
Constructor Details
#initialize(interval:, time:) ⇒ Stats
Returns a new instance of Stats.
13 14 15 16 |
# File 'app/models/caseflow/stats.rb', line 13 def initialize(interval:, time:) self.interval = interval.to_sym self.time = time end |
Instance Attribute Details
#interval ⇒ Object
Returns the value of attribute interval.
7 8 9 |
# File 'app/models/caseflow/stats.rb', line 7 def interval @interval end |
#time ⇒ Object
Returns the value of attribute time.
7 8 9 |
# File 'app/models/caseflow/stats.rb', line 7 def time @time end |
#values ⇒ Object
Returns the value of attribute values.
7 8 9 |
# File 'app/models/caseflow/stats.rb', line 7 def values @values end |
Class Method Details
.calculate_all! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/caseflow/stats.rb', line 74 def self.calculate_all! INTERVALS.each do |interval| { hourly: 0...24, daily: 0...30, weekly: 0...26, monthly: 0...24 }[interval].each do |i| self.offset(interval: interval, time: Stats.now, offset: i) .calculate_and_save_values! end end end |
.now ⇒ Object
57 58 59 |
# File 'app/models/caseflow/stats.rb', line 57 def self.now Time.find_zone!(TIMEZONE).now end |
.offset(interval:, time:, offset:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/caseflow/stats.rb', line 61 def self.offset(interval:, time:, offset:) offset_time = time case interval when :monthly then offset_time -= offset.months when :weekly then offset_time -= offset.weeks when :daily then offset_time -= offset.days when :hourly then offset_time -= offset.hours end self.new(interval: interval, time: offset_time) end |
.percentile(attribute, collection, percentile) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/caseflow/stats.rb', line 88 def self.percentile(attribute, collection, percentile) return nil if collection.empty? filtered = collection.reject { |model| model.send(attribute).nil? } sorted = filtered.sort_by(&attribute) percentile_model = sorted[((sorted.size - 1) * (percentile / 100.0)).ceil] percentile_model && percentile_model.send(attribute) end |
Instance Method Details
#calculate_and_save_values! ⇒ Object
49 50 51 52 53 54 55 |
# File 'app/models/caseflow/stats.rb', line 49 def calculate_and_save_values! return true if complete? calculated_values = calculate_values calculated_values[:complete] = Stats.now >= range_finish Rails.cache.write(cache_id, calculated_values) calculated_values end |
#complete? ⇒ Boolean
22 23 24 25 |
# File 'app/models/caseflow/stats.rb', line 22 def complete? values = load_values values && values[:complete] end |
#range ⇒ Object
27 28 29 |
# File 'app/models/caseflow/stats.rb', line 27 def range range_start..range_finish end |
#range_finish ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'app/models/caseflow/stats.rb', line 40 def range_finish @range_finish ||= { hourly: range_start + 1.hour, daily: range_start + 1.day, weekly: range_start + 1.week, monthly: range_start.next_month }[interval] end |
#range_start ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'app/models/caseflow/stats.rb', line 31 def range_start @range_start ||= { hourly: time.beginning_of_hour, daily: time.beginning_of_day, weekly: time.beginning_of_week, monthly: time.beginning_of_month }[interval] end |