Class: RailsErrorDashboard::CascadePattern

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

Overview

Tracks cascade patterns where one error causes another

A cascade pattern represents a causal relationship between errors: Parent Error → Child Error

For example: DatabaseConnectionError → NoMethodError When a database connection fails, subsequent code may try to call methods on nil objects, causing NoMethodError.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#avg_delay_secondsFloat

Average time between parent and child

Returns:

  • (Float)

    the current value of avg_delay_seconds



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def avg_delay_seconds
  @avg_delay_seconds
end

#cascade_probabilityFloat

Likelihood (0.0-1.0) that parent causes child

Returns:

  • (Float)

    the current value of cascade_probability



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def cascade_probability
  @cascade_probability
end

#child_error_idInteger

The error that happens after (potential effect)

Returns:

  • (Integer)

    the current value of child_error_id



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def child_error_id
  @child_error_id
end

#frequencyInteger

How many times this cascade has been observed

Returns:

  • (Integer)

    the current value of frequency



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def frequency
  @frequency
end

#last_detected_atDateTime

When this cascade was last observed

Returns:

  • (DateTime)

    the current value of last_detected_at



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def last_detected_at
  @last_detected_at
end

#parent_error_idInteger

The error that happens first (potential cause)

Returns:

  • (Integer)

    the current value of parent_error_id



19
20
21
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 19

def parent_error_id
  @parent_error_id
end

Instance Method Details

#calculate_probability!Object

Calculate cascade probability based on frequency Probability = (times child follows parent) / (total parent occurrences)



53
54
55
56
57
58
59
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 53

def calculate_probability!
  parent_occurrence_count = parent_error.error_occurrences.count
  return if parent_occurrence_count.zero?

  self.cascade_probability = (frequency.to_f / parent_occurrence_count).round(3)
  save
end

#increment_detection!(delay_seconds) ⇒ Object

Update cascade pattern stats



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 37

def increment_detection!(delay_seconds)
  self.frequency += 1

  # Update average delay using incremental formula
  if avg_delay_seconds.present?
    self.avg_delay_seconds = ((avg_delay_seconds * (frequency - 1)) + delay_seconds) / frequency
  else
    self.avg_delay_seconds = delay_seconds
  end

  self.last_detected_at = Time.current
  save
end

#strong_cascade?Boolean

Check if this is a strong cascade pattern

Returns:

  • (Boolean)


62
63
64
# File 'app/models/rails_error_dashboard/cascade_pattern.rb', line 62

def strong_cascade?
  cascade_probability.present? && cascade_probability >= 0.7 && frequency >= 3
end