Class: RailsErrorDashboard::ErrorBaseline

Inherits:
ErrorLogsRecord
  • Object
show all
Defined in:
app/models/rails_error_dashboard/error_baseline.rb

Overview

Stores baseline statistics for error types

Baselines are calculated periodically (hourly, daily, weekly) to establish “normal” error behavior. This enables anomaly detection by comparing current error counts against historical baselines.

Constant Summary collapse

BASELINE_TYPES =
%w[hourly daily weekly].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baseline_typeString

Time period type (hourly, daily, weekly)

Returns:

  • (String)

    the current value of baseline_type



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def baseline_type
  @baseline_type
end

#countInteger

Total errors in this period

Returns:

  • (Integer)

    the current value of count



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def count
  @count
end

#error_typeString

The type of error (e.g., “NoMethodError”)

Returns:

  • (String)

    the current value of error_type



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def error_type
  @error_type
end

#meanFloat

Average error count

Returns:

  • (Float)

    the current value of mean



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def mean
  @mean
end

#percentile_95Float

95th percentile

Returns:

  • (Float)

    the current value of percentile_95



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def percentile_95
  @percentile_95
end

#percentile_99Float

99th percentile

Returns:

  • (Float)

    the current value of percentile_99



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def percentile_99
  @percentile_99
end

#period_endDateTime

End of the period this baseline covers

Returns:

  • (DateTime)

    the current value of period_end



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def period_end
  @period_end
end

#period_startDateTime

Start of the period this baseline covers

Returns:

  • (DateTime)

    the current value of period_start



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def period_start
  @period_start
end

#platformString

Platform (iOS, Android, API, Web)

Returns:

  • (String)

    the current value of platform



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def platform
  @platform
end

#sample_sizeInteger

Number of periods in the sample

Returns:

  • (Integer)

    the current value of sample_size



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def sample_size
  @sample_size
end

#std_devFloat

Standard deviation

Returns:

  • (Float)

    the current value of std_dev



21
22
23
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 21

def std_dev
  @std_dev
end

Instance Method Details

#anomaly_level(current_count, sensitivity: 2) ⇒ Symbol?

Check if a given count is anomalous compared to this baseline

Parameters:

  • current_count (Integer)

    Current error count to check

  • sensitivity (Integer) (defaults to: 2)

    Number of standard deviations (default: 2)

Returns:

  • (Symbol, nil)

    :elevated, :high, :critical, or nil if normal



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 47

def anomaly_level(current_count, sensitivity: 2)
  return nil if mean.nil? || std_dev.nil?
  return nil if current_count <= mean

  std_devs_above = (current_count - mean) / std_dev

  case std_devs_above
  when sensitivity..(sensitivity + 1)
    :elevated
  when (sensitivity + 1)..(sensitivity + 2)
    :high
  when (sensitivity + 2)..Float::INFINITY
    :critical
  else
    nil
  end
end

#exceeds_baseline?(current_count, sensitivity: 2) ⇒ Boolean

Check if current count is above baseline

Parameters:

  • current_count (Integer)

    Current error count

  • sensitivity (Integer) (defaults to: 2)

    Number of standard deviations (default: 2)

Returns:

  • (Boolean)

    True if count exceeds baseline + (sensitivity * std_dev)



69
70
71
72
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 69

def exceeds_baseline?(current_count, sensitivity: 2)
  return false if mean.nil? || std_dev.nil?
  current_count > (mean + (sensitivity * std_dev))
end

#std_devs_above_mean(current_count) ⇒ Float?

Calculate how many standard deviations above mean

Parameters:

  • current_count (Integer)

    Current error count

Returns:

  • (Float, nil)

    Number of standard deviations or nil



85
86
87
88
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 85

def std_devs_above_mean(current_count)
  return nil if mean.nil? || std_dev.nil? || std_dev.zero?
  (current_count - mean) / std_dev
end

#threshold(sensitivity: 2) ⇒ Float?

Get the threshold for anomaly detection

Parameters:

  • sensitivity (Integer) (defaults to: 2)

    Number of standard deviations (default: 2)

Returns:

  • (Float, nil)

    Threshold value or nil if stats not available



77
78
79
80
# File 'app/models/rails_error_dashboard/error_baseline.rb', line 77

def threshold(sensitivity: 2)
  return nil if mean.nil? || std_dev.nil?
  mean + (sensitivity * std_dev)
end