Class: RailsErrorDashboard::Services::CascadeDetector

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

Overview

Detects cascade patterns by analyzing error occurrences

Runs periodically to find errors that consistently follow other errors, indicating a causal relationship.

Constant Summary collapse

DETECTION_WINDOW =

Time window to look for cascades (errors within this window may be related)

60.seconds
MIN_CASCADE_FREQUENCY =

Minimum times a pattern must occur to be considered a cascade

3
MIN_CASCADE_PROBABILITY =

Minimum probability threshold (% of time parent leads to child)

0.7

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookback_hours: 24) ⇒ CascadeDetector

Returns a new instance of CascadeDetector.



23
24
25
26
# File 'lib/rails_error_dashboard/services/cascade_detector.rb', line 23

def initialize(lookback_hours: 24)
  @lookback_hours = lookback_hours
  @detected_count = 0
end

Class Method Details

.call(lookback_hours: 24) ⇒ Object



19
20
21
# File 'lib/rails_error_dashboard/services/cascade_detector.rb', line 19

def self.call(lookback_hours: 24)
  new(lookback_hours: lookback_hours).detect_cascades
end

Instance Method Details

#detect_cascadesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rails_error_dashboard/services/cascade_detector.rb', line 28

def detect_cascades
  return { detected: 0, updated: 0 } unless can_detect?

  # Get recent error occurrences
  start_time = @lookback_hours.hours.ago
  occurrences = ErrorOccurrence.where("occurred_at >= ?", start_time).order(:occurred_at)

  # For each error occurrence, find potential children
  patterns_found = Hash.new { |h, k| h[k] = { delays: [], count: 0 } }

  occurrences.each do |parent_occ|
    # Find occurrences within detection window
    potential_children = ErrorOccurrence
      .where("occurred_at > ? AND occurred_at <= ?",
             parent_occ.occurred_at,
             parent_occ.occurred_at + DETECTION_WINDOW)
      .where.not(error_log_id: parent_occ.error_log_id)

    potential_children.each do |child_occ|
      key = [ parent_occ.error_log_id, child_occ.error_log_id ]
      delay = (child_occ.occurred_at - parent_occ.occurred_at).to_f

      patterns_found[key][:delays] << delay
      patterns_found[key][:count] += 1
    end
  end

  # Filter and save cascade patterns
  updated_count = 0
  patterns_found.each do |(parent_id, child_id), data|
    next if data[:count] < MIN_CASCADE_FREQUENCY

    # Find or create cascade pattern
    pattern = CascadePattern.find_or_initialize_by(
      parent_error_id: parent_id,
      child_error_id: child_id
    )

    avg_delay = data[:delays].sum / data[:delays].size

    if pattern.new_record?
      pattern.frequency = data[:count]
      pattern.avg_delay_seconds = avg_delay
      pattern.last_detected_at = Time.current
      pattern.save
      @detected_count += 1
    else
      # Update existing pattern
      pattern.increment_detection!(avg_delay)
      updated_count += 1
    end

    # Calculate probability
    pattern.calculate_probability!
  end

  { detected: @detected_count, updated: updated_count }
end