Class: RailsPulse::Route

Inherits:
ApplicationRecord show all
Includes:
Taggable
Defined in:
app/models/rails_pulse/route.rb

Constant Summary

Constants included from Taggable

Taggable::MAX_TAG_LENGTH, Taggable::TAG_NAME_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taggable

#add_tag, #has_tag?, #remove_tag, #tag_list, #tag_list=

Class Method Details

.average_response_timeObject



81
82
83
# File 'app/models/rails_pulse/route.rb', line 81

def self.average_response_time
  joins(:requests).average("rails_pulse_requests.duration") || 0
end

.by_method_and_path(method, path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/rails_pulse/route.rb', line 15

def self.by_method_and_path(method, path)
  # Fast path: route already exists (the common case for recurring requests).
  route = find_by(method: method, path: path)
  return route if route

  # Use INSERT ... ON CONFLICT DO NOTHING rather than create_or_find_by so that
  # concurrent inserts for the same route are silently skipped at the database level.
  # create_or_find_by rescues RecordNotUnique at the Ruby level, but PostgreSQL still
  # logs a constraint violation ERROR before raising — causing noisy logs.
  # unique_by is omitted for cross-database compatibility (MySQL rejects it); there is
  # only one unique constraint on this table so DO NOTHING resolves unambiguously.
  # Tags must be set explicitly here because insert bypasses before_save callbacks.
  insert(
    { method: method, path: path, tags: "[]", created_at: Time.current, updated_at: Time.current }
  )
  find_by!(method: method, path: path)
end

.ransackable_associations(auth_object = nil) ⇒ Object



37
38
39
# File 'app/models/rails_pulse/route.rb', line 37

def self.ransackable_associations(auth_object = nil)
  %w[requests]
end

.ransackable_attributes(auth_object = nil) ⇒ Object



33
34
35
# File 'app/models/rails_pulse/route.rb', line 33

def self.ransackable_attributes(auth_object = nil)
  %w[path average_response_time_ms max_response_time_ms request_count requests_per_minute occurred_at requests_occurred_at error_count error_rate_percentage status_indicator]
end

Instance Method Details

#path_and_methodObject



85
86
87
# File 'app/models/rails_pulse/route.rb', line 85

def path_and_method
  "#{path} #{method}"
end

#to_breadcrumbObject



77
78
79
# File 'app/models/rails_pulse/route.rb', line 77

def to_breadcrumb
  "#{method.upcase} #{path}".truncate(60)
end