Class: RailsPulse::Dashboard::Charts::P95ResponseTime

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_pulse/dashboard/charts/p95_response_time.rb

Instance Method Summary collapse

Constructor Details

#initialize(disabled_tags: [], show_non_tagged: true) ⇒ P95ResponseTime

Returns a new instance of P95ResponseTime.



5
6
7
8
# File 'app/models/rails_pulse/dashboard/charts/p95_response_time.rb', line 5

def initialize(disabled_tags: [], show_non_tagged: true)
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
end

Instance Method Details

#to_chart_dataObject



10
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
# File 'app/models/rails_pulse/dashboard/charts/p95_response_time.rb', line 10

def to_chart_data
  # Create a range of all dates in the past 2 weeks
  start_date = 2.weeks.ago.beginning_of_day.to_date
  end_date = Time.current.to_date
  date_range = (start_date..end_date)

  # Get the actual data from Summary records (queries for P95)
  summaries = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Query",
      period_type: "day",
      period_start: start_date.beginning_of_day..end_date.end_of_day
    )

  actual_data = summaries
    .group_by_day(:period_start, time_zone: Time.zone)
    .average(:p95_duration)
    .transform_keys { |date| date.to_date }
    .transform_values { |avg| avg&.round(0) || 0 }

  # Fill in all dates with zero values for missing days
  date_range.each_with_object({}) do |date, result|
    formatted_date = date.strftime("%b %-d")
    result[formatted_date] = actual_data[date] || 0
  end
end