Class: RailsPulse::Routes::Cards::ErrorRates

Inherits:
Cards::Base
  • Object
show all
Defined in:
app/models/rails_pulse/routes/cards/error_rates.rb

Instance Method Summary collapse

Constructor Details

#initialize(route: nil, disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day") ⇒ ErrorRates

Returns a new instance of ErrorRates.



5
6
7
8
9
10
11
# File 'app/models/rails_pulse/routes/cards/error_rates.rb', line 5

def initialize(route: nil, disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day")
  @route = route
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
  @period = period
  @period_type = period_type
end

Instance Method Details

#to_metric_cardObject



13
14
15
16
17
18
19
20
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/rails_pulse/routes/cards/error_rates.rb', line 13

def to_metric_card
  # Use base class helper for query construction
  base_query = base_summary_query("RailsPulse::Route")

  metrics = base_query.select(
    "SUM(error_count) AS total_errors",
    "SUM(status_4xx) AS total_4xx",
    "SUM(count) AS total_requests",
    "SUM(CASE WHEN period_start >= #{quote(current_window_start)} THEN error_count + status_4xx ELSE 0 END) AS current_total_errors",
    "SUM(CASE WHEN period_start >= #{quote(range_start)} AND period_start < #{quote(current_window_start)} THEN error_count + status_4xx ELSE 0 END) AS previous_total_errors"
  ).take

  total_errors = metrics.total_errors || 0
  total_4xx = metrics.total_4xx || 0
  total_requests = metrics.total_requests || 0
  current_total = metrics.current_total_errors || 0
  previous_total = metrics.previous_total_errors || 0

  has_data = total_requests > 0

  server_rate = has_data ? (total_errors.to_f / total_requests * 100).round(2) : 0
  client_rate = has_data ? (total_4xx.to_f / total_requests * 100).round(2) : 0

  # Use base class trend calculation
  if show_trend?
    trend_icon, trend_amount = has_data ? trend_for(current_total, previous_total) : [ "move-right", "" ]
  end

  # Build sparkline data using base class helpers
  sparkline_query = build_sparkline_query("RailsPulse::Route")
  grouped_errors = group_sparkline_by_period(sparkline_query, :error_count)
  grouped_4xx = group_sparkline_by_period(sparkline_query, :status_4xx)

  # Combine error counts for each period
  combined_errors = grouped_errors.merge(grouped_4xx) { |_key, errors, fourxx| errors + fourxx }

  # Use base class sparkline generation (handles hour vs day automatically)
  sparkline_data = sparkline_from(combined_errors)

  total_rate = has_data ? (server_rate + client_rate).round(2) : 0
  summary = has_data ? "#{total_rate}% of requests" : ""

  {
    id: "error_rates",
    chart_color: RailsPulse::ChartColors::DEFAULT,
    context: "routes",
    title: "Error Rate",
    summary: summary,
    chart_data: sparkline_data,
    trend_icon: trend_icon,
    trend_amount: trend_amount,
    trend_text: (show_trend? ? comparison_period_text : nil),
    period_stat: has_data ? "#{format_number(total_errors + total_4xx)} errors" : period_date_range,
    help_heading: "Error Rates",
    help_text: "Combined 5xx and 4xx error rate over the last 2 weeks. 5xx server errors indicate bugs or infrastructure issues. 4xx client errors may indicate broken API consumers or deprecated endpoints."
  }
end