Class: RailsPulse::Routes::Cards::PercentileResponseTimes
- Inherits:
-
Object
- Object
- RailsPulse::Routes::Cards::PercentileResponseTimes
- Defined in:
- app/models/rails_pulse/routes/cards/percentile_response_times.rb
Instance Method Summary collapse
-
#initialize(route: nil, disabled_tags: [], show_non_tagged: true) ⇒ PercentileResponseTimes
constructor
A new instance of PercentileResponseTimes.
- #to_metric_card ⇒ Object
Constructor Details
#initialize(route: nil, disabled_tags: [], show_non_tagged: true) ⇒ PercentileResponseTimes
Returns a new instance of PercentileResponseTimes.
5 6 7 8 9 |
# File 'app/models/rails_pulse/routes/cards/percentile_response_times.rb', line 5 def initialize(route: nil, disabled_tags: [], show_non_tagged: true) @route = route @disabled_tags = @show_non_tagged = show_non_tagged end |
Instance Method Details
#to_metric_card ⇒ Object
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 |
# File 'app/models/rails_pulse/routes/cards/percentile_response_times.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 P95 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( "AVG(p95_duration) AS overall_p95", "AVG(CASE WHEN period_start >= '#{last_7_days.strftime('%Y-%m-%d %H:%M:%S')}' THEN p95_duration ELSE NULL END) AS current_p95", "AVG(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 p95_duration ELSE NULL END) AS previous_p95" ).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 percentage = previous_period_p95.zero? ? 0 : ((previous_period_p95 - current_period_p95) / previous_period_p95 * 100).abs.round(1) trend_icon = percentage < 0.1 ? "move-right" : current_period_p95 < previous_period_p95 ? "trending-down" : "trending-up" trend_amount = previous_period_p95.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") .average(:p95_duration) 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| avg = grouped_daily[day]&.round(0) || 0 label = day.strftime("%b %-d") sparkline_data[label] = { value: avg } end { id: "percentile_response_times", context: "routes", title: "95th Percentile Response Time", summary: "#{p95_response_time} ms", chart_data: sparkline_data, trend_icon: trend_icon, trend_amount: trend_amount, trend_text: "Compared to last week" } end |