Class: RailsPulse::Routes::Cards::ErrorRatePerRoute

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

Instance Method Summary collapse

Constructor Details

#initialize(route: nil, disabled_tags: [], show_non_tagged: true) ⇒ ErrorRatePerRoute

Returns a new instance of ErrorRatePerRoute.



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

def initialize(route: nil, disabled_tags: [], show_non_tagged: true)
  @route = route
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
end

Instance Method Details

#to_metric_cardObject



11
12
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
70
71
# File 'app/models/rails_pulse/routes/cards/error_rate_per_route.rb', line 11

def to_metric_card
  last_7_days = 7.days.ago.beginning_of_day
  previous_7_days = 14.days.ago.beginning_of_day

  # Single query to get all error metrics with conditional aggregation
  base_query = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Route",
      period_type: "day",
      period_start: 2.weeks.ago.beginning_of_day..Time.current
    )
  base_query = base_query.where(summarizable_id: @route.id) if @route

  metrics = base_query.select(
    "SUM(error_count) AS total_errors",
    "SUM(count) AS total_requests",
    "SUM(CASE WHEN period_start >= '#{last_7_days.strftime('%Y-%m-%d %H:%M:%S')}' THEN error_count ELSE 0 END) AS current_errors",
    "SUM(CASE WHEN period_start >= '#{previous_7_days.strftime('%Y-%m-%d %H:%M:%S')}' AND period_start < '#{last_7_days.strftime('%Y-%m-%d %H:%M:%S')}' THEN error_count ELSE 0 END) AS previous_errors"
  ).take

  # Calculate metrics from single query result
  total_errors = metrics.total_errors || 0
  total_requests = metrics.total_requests || 0
  current_period_errors = metrics.current_errors || 0
  previous_period_errors = metrics.previous_errors || 0

  # Calculate overall error rate percentage
  overall_error_rate = total_requests > 0 ? (total_errors.to_f / total_requests * 100).round(2) : 0

  # Calculate trend
  percentage = previous_period_errors.zero? ? 0 : ((previous_period_errors - current_period_errors) / previous_period_errors.to_f * 100).abs.round(1)
  trend_icon = percentage < 0.1 ? "move-right" : current_period_errors < previous_period_errors ? "trending-down" : "trending-up"
  trend_amount = previous_period_errors.zero? ? "0%" : "#{percentage}%"

  # Sparkline data by day with zero-filled days over the last 14 days
  grouped_daily = base_query
    .group_by_day(:period_start, time_zone: "UTC")
    .sum(:error_count)

  start_day = 2.weeks.ago.beginning_of_day.to_date
  end_day = Time.current.to_date

  sparkline_data = {}
  (start_day..end_day).each do |day|
    total = grouped_daily[day] || 0
    label = day.strftime("%b %-d")
    sparkline_data[label] = { value: total }
  end

  {
    id: "error_rate_per_route",
    context: "routes",
    title: "Error Rate Per Route",
    summary: "#{overall_error_rate}%",
    chart_data: sparkline_data,
    trend_icon: trend_icon,
    trend_amount: trend_amount,
    trend_text: "Compared to last week"
  }
end