Class: RailsPulse::Dashboard::Charts::ResponseTimePercentiles
- Inherits:
-
Object
- Object
- RailsPulse::Dashboard::Charts::ResponseTimePercentiles
- Defined in:
- app/models/rails_pulse/dashboard/charts/response_time_percentiles.rb
Instance Method Summary collapse
-
#initialize(disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day") ⇒ ResponseTimePercentiles
constructor
A new instance of ResponseTimePercentiles.
- #to_chart_data ⇒ Object
Constructor Details
#initialize(disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day") ⇒ ResponseTimePercentiles
Returns a new instance of ResponseTimePercentiles.
5 6 7 8 9 10 |
# File 'app/models/rails_pulse/dashboard/charts/response_time_percentiles.rb', line 5 def initialize(disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day") @disabled_tags = @show_non_tagged = show_non_tagged @period = period @period_type = period_type end |
Instance Method Details
#to_chart_data ⇒ Object
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/rails_pulse/dashboard/charts/response_time_percentiles.rb', line 12 def to_chart_data if @period_type == "hour" # Create a range of all hours in the selected period start_time = (@period * 24).hours.ago.beginning_of_hour end_time = Time.current.beginning_of_hour # Get the actual data from Summary records (routes) summaries = RailsPulse::Summary .with_tag_filters(@disabled_tags, @show_non_tagged) .where( summarizable_type: "RailsPulse::Route", period_type: "hour", period_start: start_time..end_time ) return nil if summaries.empty? # Group by hour and calculate weighted percentiles hourly_data = {} summaries.each do |summary| time_key = summary.period_start.beginning_of_hour count = summary.count || 0 if hourly_data[time_key] hourly_data[time_key][:total_weighted_p50] += (summary.p50_duration || 0) * count hourly_data[time_key][:total_weighted_p95] += (summary.p95_duration || 0) * count hourly_data[time_key][:total_weighted_p99] += (summary.p99_duration || 0) * count hourly_data[time_key][:total_count] += count else hourly_data[time_key] = { total_weighted_p50: (summary.p50_duration || 0) * count, total_weighted_p95: (summary.p95_duration || 0) * count, total_weighted_p99: (summary.p99_duration || 0) * count, total_count: count } end end # Convert to final values (weighted averages) final_data = hourly_data.transform_values do |data| { p50: data[:total_count] > 0 ? (data[:total_weighted_p50] / data[:total_count]).round(0) : nil, p95: data[:total_count] > 0 ? (data[:total_weighted_p95] / data[:total_count]).round(0) : nil, p99: data[:total_count] > 0 ? (data[:total_weighted_p99] / data[:total_count]).round(0) : nil } end # Build hourly time range time_range = [] current_time = start_time while current_time <= end_time time_range << current_time current_time += 1.hour end # Build labels array (hourly format) labels = time_range.map { |time| time.strftime("%H:%M") } # Build series data p50_data = time_range.map { |time| final_data[time]&.[](:p50) } p95_data = time_range.map { |time| final_data[time]&.[](:p95) } p99_data = time_range.map { |time| final_data[time]&.[](:p99) } else # Daily grouping (existing logic) start_date = @period.days.ago.beginning_of_day.to_date end_date = Time.current.to_date date_range = (start_date..end_date) summaries = RailsPulse::Summary .with_tag_filters(@disabled_tags, @show_non_tagged) .where( summarizable_type: "RailsPulse::Route", period_type: "day", period_start: start_date.beginning_of_day..end_date.end_of_day ) return nil if summaries.empty? daily_data = {} summaries.each do |summary| date = summary.period_start.to_date count = summary.count || 0 if daily_data[date] daily_data[date][:total_weighted_p50] += (summary.p50_duration || 0) * count daily_data[date][:total_weighted_p95] += (summary.p95_duration || 0) * count daily_data[date][:total_weighted_p99] += (summary.p99_duration || 0) * count daily_data[date][:total_count] += count else daily_data[date] = { total_weighted_p50: (summary.p50_duration || 0) * count, total_weighted_p95: (summary.p95_duration || 0) * count, total_weighted_p99: (summary.p99_duration || 0) * count, total_count: count } end end # Convert to final values final_data = daily_data.transform_values do |data| { p50: data[:total_count] > 0 ? (data[:total_weighted_p50] / data[:total_count]).round(0) : nil, p95: data[:total_count] > 0 ? (data[:total_weighted_p95] / data[:total_count]).round(0) : nil, p99: data[:total_count] > 0 ? (data[:total_weighted_p99] / data[:total_count]).round(0) : nil } end # Build labels array labels = date_range.map { |date| date.strftime("%b %-d") } # Build series data p50_data = date_range.map { |date| final_data[date]&.[](:p50) } p95_data = date_range.map { |date| final_data[date]&.[](:p95) } p99_data = date_range.map { |date| final_data[date]&.[](:p99) } end # Build series (common for both hourly and daily) series = [] series << { name: "P50", data: p50_data, type: "line", color: RailsPulse::ChartColors::DEFAULT } series << { name: "P95", data: p95_data, type: "line", color: RailsPulse::ChartColors::P95 } series << { name: "P99", data: p99_data, type: "line", color: RailsPulse::ChartColors::P99 } # Add Service Level Objective series if configured slo_configs = RailsPulse.configuration.service_level_objectives slo_configs.each do |slo| color = slo[:percentile] == 95 ? RailsPulse::ChartColors::P95 : RailsPulse::ChartColors::P99 series.unshift({ name: "P#{slo[:percentile]} SLO (#{slo[:threshold]}ms)", data: Array.new(labels.length, slo[:threshold]), type: "line", lineStyle: { type: "dashed", width: 2 }, color: color, symbol: "none" }) end { labels: labels, series: series } end |