Class: RailsPulse::Dashboard::Charts::AverageResponseTime

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AverageResponseTime.



5
6
7
8
# File 'app/models/rails_pulse/dashboard/charts/average_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/rails_pulse/dashboard/charts/average_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 (routes)
  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
    )

  # Group by day manually for cross-database compatibility
  actual_data = {}
  summaries.each do |summary|
    date = summary.period_start.to_date

    if actual_data[date]
      actual_data[date][:total_weighted] += (summary.avg_duration || 0) * (summary.count || 0)
      actual_data[date][:total_count] += (summary.count || 0)
    else
      actual_data[date] = {
        total_weighted: (summary.avg_duration || 0) * (summary.count || 0),
        total_count: (summary.count || 0)
      }
    end
  end

  # Convert to final values
  actual_data = actual_data.transform_values do |data|
    data[:total_count] > 0 ? (data[:total_weighted] / data[:total_count]).round(0) : 0
  end

  # 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