Class: RailsErrorDashboard::Commands::FlushRackAttackEvents

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

Overview

Command: Upsert buffered Rack::Attack event counts into the database.

Receives a snapshot hash from RackAttackTracker and merges it into hourly-bucketed rows. Uses find_or_initialize_by + increment for cross-database compatibility (no raw SQL upsert).

counts keys: "rule\x1Fmatch_type\x1Fdiscriminator\x1Fpath\x1Fhttp_method"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counts:) ⇒ FlushRackAttackEvents

Returns a new instance of FlushRackAttackEvents.



17
18
19
# File 'lib/rails_error_dashboard/commands/flush_rack_attack_events.rb', line 17

def initialize(counts:)
  @counts = counts || {}
end

Class Method Details

.call(counts:) ⇒ Object



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

def self.call(counts:)
  new(counts: counts).call
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_error_dashboard/commands/flush_rack_attack_events.rb', line 21

def call
  return if @counts.empty?

  period = Time.current.beginning_of_hour
  app_id = current_application_id

  @counts.each do |key, count|
    rule, match_type, discriminator, path, http_method =
      Services::RackAttackTracker.parse_key(key)

    next if rule.blank? || match_type.blank?

    upsert_event(
      rule: rule,
      match_type: match_type,
      discriminator: discriminator,
      path: path,
      http_method: http_method,
      period: period,
      app_id: app_id,
      count: count
    )
  end
rescue => e
  RailsErrorDashboard::Logger.debug(
    "[RailsErrorDashboard] FlushRackAttackEvents failed: #{e.class} - #{e.message}"
  )
end