Class: RailsPulse::Routes::Cards::PercentileResponseTimes

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PercentileResponseTimes.



5
6
7
8
9
10
11
# File 'app/models/rails_pulse/routes/cards/percentile_response_times.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
# File 'app/models/rails_pulse/routes/cards/percentile_response_times.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(p95_duration * count) / NULLIF(SUM(count), 0) AS overall_p95",
    "SUM(CASE WHEN period_start >= #{quote(current_window_start)} THEN p95_duration * count ELSE 0 END) / NULLIF(SUM(CASE WHEN period_start >= #{quote(current_window_start)} THEN count ELSE 0 END), 0) AS current_p95",
    "SUM(CASE WHEN period_start >= #{quote(range_start)} AND period_start < #{quote(current_window_start)} THEN p95_duration * count ELSE 0 END) / NULLIF(SUM(CASE WHEN period_start >= #{quote(range_start)} AND period_start < #{quote(current_window_start)} THEN count ELSE 0 END), 0) AS previous_p95",
    "SUM(count) AS total_count"
  ).take

  # Calculate metrics from single query result
  p95_response_time = (metrics&.overall_p95 || 0).round(0)
  current_period_p95 = metrics&.current_p95 || 0
  previous_period_p95 = metrics&.previous_p95 || 0

  has_data = metrics.total_count.to_i > 0

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

  # Build sparkline data using base class helpers
  sparkline_query = build_sparkline_query("RailsPulse::Route")
  weighted_sums = group_sparkline_by_period(sparkline_query, "p95_duration * count")
  period_counts = group_sparkline_by_period(sparkline_query, :count)

  # Calculate weighted averages for each period
  averages_by_period = weighted_sums.each_with_object({}) do |(period, weighted), hash|
    count = period_counts[period].to_i
    hash[period] = count > 0 ? (weighted.to_f / count).round(0) : 0
  end

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

  {
    id: "percentile_response_times",
    chart_color: RailsPulse::ChartColors::P95,
    context: "routes",
    title: "P95 Response Time",
    summary: has_data ? "#{p95_response_time} ms" : "",
    chart_data: sparkline_data,
    trend_icon: trend_icon,
    trend_amount: trend_amount,
    trend_text: (show_trend? ? comparison_period_text : nil),
    period_stat: has_data ? "Across #{format_number(metrics.total_count.to_i)} requests" : period_date_range,
    help_heading: "P95 Response Time",
    help_text: "The 95th percentile response time — 95% of requests are faster than this. Weighted by request volume across all routes. A rising P95 indicates increasing slowness affecting your users."
  }
end