Class: RailsConsolePro::Services::StatsCalculator
- Inherits:
-
Object
- Object
- RailsConsolePro::Services::StatsCalculator
- Defined in:
- lib/rails_console_pro/services/stats_calculator.rb
Overview
Service for calculating model growth rate statistics
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_growth_rate ⇒ Object
-
#initialize(model_class, safe_count_proc) ⇒ StatsCalculator
constructor
A new instance of StatsCalculator.
Constructor Details
#initialize(model_class, safe_count_proc) ⇒ StatsCalculator
Returns a new instance of StatsCalculator.
11 12 13 14 |
# File 'lib/rails_console_pro/services/stats_calculator.rb', line 11 def initialize(model_class, safe_count_proc) @model_class = model_class @safe_count = safe_count_proc end |
Class Method Details
.calculate_growth_rate(model_class, safe_count_proc) ⇒ Object
7 8 9 |
# File 'lib/rails_console_pro/services/stats_calculator.rb', line 7 def self.calculate_growth_rate(model_class, safe_count_proc) new(model_class, safe_count_proc).calculate_growth_rate end |
Instance Method Details
#calculate_growth_rate ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_console_pro/services/stats_calculator.rb', line 16 def calculate_growth_rate # Double-check created_at exists (defensive programming) return nil unless ModelValidator.(@model_class) return nil unless ModelValidator.has_table?(@model_class) begin # Get count from 1 hour ago one_hour_ago = 1.hour.ago old_count = @model_class.where('created_at < ?', one_hour_ago).count return nil if old_count == 0 current_count = @safe_count.call return nil if current_count == old_count || current_count == 0 # Calculate percentage change ((current_count - old_count).to_f / old_count * 100).round(2) rescue => e # Silently fail - growth rate is optional nil end end |