Class: RailsErrorDashboard::Services::BaselineCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/baseline_calculator.rb

Overview

Calculates baseline statistics for error types

This service analyzes historical error data to calculate statistical baselines for different time periods (hourly, daily, weekly). These baselines enable anomaly detection by establishing “normal” error behavior.

Statistical methods used:

  • Mean and Standard Deviation

  • 95th and 99th Percentiles

  • Outlier removal (> 3 std devs)

Examples:

BaselineCalculator.calculate_all_baselines
# Calculates baselines for all error types and platforms

Constant Summary collapse

HOURLY_LOOKBACK =

Lookback periods for baseline calculation

4.weeks
DAILY_LOOKBACK =
12.weeks
WEEKLY_LOOKBACK =
1.year
OUTLIER_THRESHOLD =

Outlier threshold (standard deviations)

3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaselineCalculator

Returns a new instance of BaselineCalculator.



36
37
38
# File 'lib/rails_error_dashboard/services/baseline_calculator.rb', line 36

def initialize
  @calculated_count = 0
end

Class Method Details

.calculate_all_baselinesObject



28
29
30
# File 'lib/rails_error_dashboard/services/baseline_calculator.rb', line 28

def self.calculate_all_baselines
  new.calculate_all_baselines
end

.calculate_for_error_type(error_type, platform) ⇒ Object



32
33
34
# File 'lib/rails_error_dashboard/services/baseline_calculator.rb', line 32

def self.calculate_for_error_type(error_type, platform)
  new.calculate_for_error_type(error_type, platform)
end

Instance Method Details

#calculate_all_baselinesHash

Calculate baselines for all error types and platforms

Returns:

  • (Hash)

    Summary of calculated baselines



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_error_dashboard/services/baseline_calculator.rb', line 42

def calculate_all_baselines
  return { calculated: 0, message: "ErrorBaseline table not available" } unless can_calculate?

  # Get all unique combinations of error_type and platform
  combinations = ErrorLog.distinct.pluck(:error_type, :platform).compact

  combinations.each do |(error_type, platform)|
    calculate_for_error_type(error_type, platform)
  end

  { calculated: @calculated_count }
end

#calculate_for_error_type(error_type, platform) ⇒ Hash

Calculate baselines for a specific error type and platform

Parameters:

  • error_type (String)

    The error type

  • platform (String)

    The platform

Returns:

  • (Hash)

    Summary with hourly, daily, weekly baseline info



59
60
61
62
63
64
65
66
67
# File 'lib/rails_error_dashboard/services/baseline_calculator.rb', line 59

def calculate_for_error_type(error_type, platform)
  return {} unless can_calculate?

  {
    hourly: calculate_hourly_baseline(error_type, platform),
    daily: calculate_daily_baseline(error_type, platform),
    weekly: calculate_weekly_baseline(error_type, platform)
  }
end