Class: RailsPulse::Jobs::Cards::P95Duration

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

Instance Method Summary collapse

Constructor Details

#initialize(job: nil, disabled_tags: [], show_non_tagged: true, period: 14, period_type: "day") ⇒ P95Duration

Returns a new instance of P95Duration.



5
6
7
8
9
10
11
# File 'app/models/rails_pulse/jobs/cards/p95_duration.rb', line 5

def initialize(job: nil, disabled_tags: [], show_non_tagged: true, period: 14, period_type: "day")
  @job = job
  @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
65
66
67
68
69
# File 'app/models/rails_pulse/jobs/cards/p95_duration.rb', line 13

def to_metric_card
  base_query = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Job",
      period_type: @period_type,
      period_start: range_start..now
    )
  base_query = base_query.where(summarizable_id: @job.id) if @job

  metrics = base_query.select(
    "SUM(rails_pulse_summaries.p95_duration * rails_pulse_summaries.count) AS total_weighted_p95",
    "SUM(rails_pulse_summaries.count) AS total_runs",
    "SUM(CASE WHEN rails_pulse_summaries.period_start >= #{quote(current_window_start)} THEN rails_pulse_summaries.p95_duration * rails_pulse_summaries.count ELSE 0 END) AS current_weighted_p95",
    "SUM(CASE WHEN rails_pulse_summaries.period_start >= #{quote(current_window_start)} THEN rails_pulse_summaries.count ELSE 0 END) AS current_runs",
    "SUM(CASE WHEN rails_pulse_summaries.period_start >= #{quote(range_start)} AND rails_pulse_summaries.period_start < #{quote(current_window_start)} THEN rails_pulse_summaries.p95_duration * rails_pulse_summaries.count ELSE 0 END) AS previous_weighted_p95",
    "SUM(CASE WHEN rails_pulse_summaries.period_start >= #{quote(range_start)} AND rails_pulse_summaries.period_start < #{quote(current_window_start)} THEN rails_pulse_summaries.count ELSE 0 END) AS previous_runs"
  ).take

  total_runs = metrics&.total_runs.to_i
  total_weighted_p95 = metrics&.total_weighted_p95.to_f
  current_runs = metrics&.current_runs.to_i
  current_weighted_p95 = metrics&.current_weighted_p95.to_f
  previous_runs = metrics&.previous_runs.to_i
  previous_weighted_p95 = metrics&.previous_weighted_p95.to_f

  p95_duration = weighted_average(total_weighted_p95, total_runs)
  current_p95 = weighted_average(current_weighted_p95, current_runs)
  previous_p95 = weighted_average(previous_weighted_p95, previous_runs)

  trend_icon, trend_amount = trend_for(current_p95, previous_p95) if show_trend?

  grouped_weighted = base_query
    .group_by_date(:period_start)
    .sum(Arel.sql("rails_pulse_summaries.p95_duration * rails_pulse_summaries.count"))

  grouped_counts = base_query
    .group_by_date(:period_start)
    .sum("rails_pulse_summaries.count")

  sparkline_data = sparkline_from_averages(grouped_weighted, grouped_counts)

  {
    id: "jobs_p95_duration",
    chart_color: RailsPulse::ChartColors::P95,
    context: "jobs",
    title: "95th Percentile Duration",
    summary: format_duration(p95_duration),
    chart_data: sparkline_data,
    trend_icon: trend_icon,
    trend_amount: trend_amount,
    trend_text: (show_trend? ? comparison_period_text : nil),
    period_stat: total_runs > 0 ? "Across #{format_number(total_runs)} runs" : period_date_range,
    help_heading: "P95 Duration",
    help_text: "The 95th percentile execution time — 95% of jobs complete faster than this. Weighted by job volume across all classes. A rising P95 indicates jobs are becoming slower, which may cause queue backlog."
  }
end