Class: RailsPulse::Charts::PercentileChartBase
- Inherits:
-
Object
- Object
- RailsPulse::Charts::PercentileChartBase
- Defined in:
- app/models/rails_pulse/charts/percentile_chart_base.rb
Direct Known Subclasses
Queries::Charts::QueryPerformance, Routes::Charts::ResponseTimePercentiles
Instance Method Summary collapse
-
#initialize(ransack_query:, period_type: nil, subject: nil, start_time: nil, end_time: nil, start_duration: nil, disabled_tags: [], show_non_tagged: true) ⇒ PercentileChartBase
constructor
A new instance of PercentileChartBase.
- #to_chart_data ⇒ Object
Constructor Details
#initialize(ransack_query:, period_type: nil, subject: nil, start_time: nil, end_time: nil, start_duration: nil, disabled_tags: [], show_non_tagged: true) ⇒ PercentileChartBase
Returns a new instance of PercentileChartBase.
4 5 6 7 8 9 10 11 12 13 |
# File 'app/models/rails_pulse/charts/percentile_chart_base.rb', line 4 def initialize(ransack_query:, period_type: nil, subject: nil, start_time: nil, end_time: nil, start_duration: nil, disabled_tags: [], show_non_tagged: true) @ransack_query = ransack_query @period_type = period_type @subject = subject @start_time = start_time @end_time = end_time @start_duration = start_duration @disabled_tags = @show_non_tagged = show_non_tagged end |
Instance Method Details
#to_chart_data ⇒ Object
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 |
# File 'app/models/rails_pulse/charts/percentile_chart_base.rb', line 15 def to_chart_data summaries = @ransack_query.result(distinct: false) .with_tag_filters(@disabled_tags, @show_non_tagged) .where( summarizable_type: summarizable_type, period_type: @period_type ) summaries = summaries.where(summarizable_id: @subject.id) if @subject # Group by period_start at database level and calculate weighted aggregates summaries = summaries .group(:period_start) .select( :period_start, "SUM(count) as total_count", "SUM(p50_duration * count) as total_weighted_p50", "SUM(p95_duration * count) as total_weighted_p95", "SUM(p99_duration * count) as total_weighted_p99" ) # Build raw_data hash from grouped results raw_data = {} summaries.each do |summary| = summary.period_start.to_i count = summary.total_count || 0 raw_data[] = { total_weighted_p50: summary.total_weighted_p50 || 0, total_weighted_p95: summary.total_weighted_p95 || 0, total_weighted_p99: summary.total_weighted_p99 || 0, total_count: count } end # Convert to final values (weighted averages) and pad missing data step = @period_type.to_s == "hour" ? 3600 : 86400 daily_data = {} (@start_time.to_i..@end_time.to_i).step(step) do || if raw_data[] count = raw_data[][:total_count] daily_data[] = { p50: count > 0 ? (raw_data[][:total_weighted_p50] / count).round(2) : nil, p95: count > 0 ? (raw_data[][:total_weighted_p95] / count).round(2) : nil, p99: count > 0 ? (raw_data[][:total_weighted_p99] / count).round(2) : nil } else daily_data[] = { p50: nil, p95: nil, p99: nil } end end # Build labels array (timestamps in milliseconds for JavaScript) labels = daily_data.keys.map { || * 1000 } # Build series data series = [] p50_data = daily_data.values.map { |data| data[:p50] } series << { name: "P50", data: p50_data, type: "line", color: RailsPulse::ChartColors::DEFAULT } p95_data = daily_data.values.map { |data| data[:p95] } series << { name: "P95", data: p95_data, type: "line", color: RailsPulse::ChartColors::P95 } p99_data = daily_data.values.map { |data| data[:p99] } series << { name: "P99", data: p99_data, type: "line", color: RailsPulse::ChartColors::P99 } slo_configs = RailsPulse.configuration.public_send(slo_config_key) 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 |