Class: RailsErrorDashboard::Commands::UpsertCascadePattern

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/commands/upsert_cascade_pattern.rb

Overview

Command: Create or update a cascade pattern from detection results

This consolidates all cascade pattern writes: find_or_initialize, set frequency/delay, save, and calculate probability. CascadeDetector (the service) handles pure detection logic and delegates all persistence to this command.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_error_id:, child_error_id:, frequency:, avg_delay_seconds:) ⇒ UpsertCascadePattern

Returns a new instance of UpsertCascadePattern.



17
18
19
20
21
22
# File 'lib/rails_error_dashboard/commands/upsert_cascade_pattern.rb', line 17

def initialize(parent_error_id:, child_error_id:, frequency:, avg_delay_seconds:)
  @parent_error_id = parent_error_id
  @child_error_id = child_error_id
  @frequency = frequency
  @avg_delay_seconds = avg_delay_seconds
end

Class Method Details

.call(parent_error_id:, child_error_id:, frequency:, avg_delay_seconds:) ⇒ Object



12
13
14
15
# File 'lib/rails_error_dashboard/commands/upsert_cascade_pattern.rb', line 12

def self.call(parent_error_id:, child_error_id:, frequency:, avg_delay_seconds:)
  new(parent_error_id: parent_error_id, child_error_id: child_error_id,
      frequency: frequency, avg_delay_seconds: avg_delay_seconds).call
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_error_dashboard/commands/upsert_cascade_pattern.rb', line 24

def call
  pattern = CascadePattern.find_or_initialize_by(
    parent_error_id: @parent_error_id,
    child_error_id: @child_error_id
  )

  is_new = pattern.new_record?

  if is_new
    pattern.frequency = @frequency
    pattern.avg_delay_seconds = @avg_delay_seconds
    pattern.last_detected_at = Time.current
    pattern.save
  else
    IncrementCascadeDetection.call(pattern, @avg_delay_seconds)
  end

  CalculateCascadeProbability.call(pattern)

  { pattern: pattern, created: is_new }
end