Class: RailsPulse::Routes::Charts::AverageResponseTimes

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_pulse/routes/charts/average_response_times.rb

Instance Method Summary collapse

Constructor Details

#initialize(ransack_query:, period_type: nil, route: nil, start_time: nil, end_time: nil, start_duration: nil, disabled_tags: [], show_non_tagged: true) ⇒ AverageResponseTimes

Returns a new instance of AverageResponseTimes.



5
6
7
8
9
10
11
12
13
14
# File 'app/models/rails_pulse/routes/charts/average_response_times.rb', line 5

def initialize(ransack_query:, period_type: nil, route: nil, start_time: nil, end_time: nil, start_duration: nil, disabled_tags: [], show_non_tagged: true)
  @ransack_query = ransack_query
  @period_type = period_type
  @route = route
  @start_time = start_time
  @end_time = end_time
  @start_duration = start_duration
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
end

Instance Method Details

#to_chart_dataObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/rails_pulse/routes/charts/average_response_times.rb', line 16

def to_chart_data
  summaries = @ransack_query.result(distinct: false)
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Route",
      period_type: @period_type
    )

  summaries = summaries.where(summarizable_id: @route.id) if @route
  summaries = summaries
    .group(:period_start)
    .having("AVG(avg_duration) > ?", @start_duration || 0)
    .average(:avg_duration)
    .transform_keys(&:to_i)

  # Pad missing data points with zeros
  step = @period_type == :hour ? 1.hour : 1.day
  data = {}
  (@start_time.to_i..@end_time.to_i).step(step) do |timestamp|
    data[timestamp.to_i] = summaries[timestamp.to_i].to_f.round(2)
  end
  data
end