Class: RailsPulse::Dashboard::Tables::SlowQueries

Inherits:
Object
  • Object
show all
Includes:
FormattingHelper
Defined in:
app/models/rails_pulse/dashboard/tables/slow_queries.rb

Instance Method Summary collapse

Methods included from FormattingHelper

#human_readable_occurred_at, #human_readable_summary_period, #time_ago_in_words

Constructor Details

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

Returns a new instance of SlowQueries.



7
8
9
10
# File 'app/models/rails_pulse/dashboard/tables/slow_queries.rb', line 7

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

Instance Method Details

#to_table_dataObject



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
52
53
54
55
56
57
58
59
# File 'app/models/rails_pulse/dashboard/tables/slow_queries.rb', line 12

def to_table_data
  # Get data for this week
  this_week_start = 1.week.ago.beginning_of_week
  this_week_end = Time.current.end_of_week

  # Fetch query data from Summary records for this week
  query_data = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .joins("INNER JOIN rails_pulse_queries ON rails_pulse_queries.id = rails_pulse_summaries.summarizable_id")
    .where(
      summarizable_type: "RailsPulse::Query",
      period_type: "day",
      period_start: this_week_start..this_week_end
    )
    .group("rails_pulse_summaries.summarizable_id, rails_pulse_queries.normalized_sql")
    .select(
      "rails_pulse_summaries.summarizable_id as query_id",
      "rails_pulse_queries.normalized_sql",
      "SUM(rails_pulse_summaries.avg_duration * rails_pulse_summaries.count) / SUM(rails_pulse_summaries.count) as avg_duration",
      "SUM(rails_pulse_summaries.count) as request_count",
      "MAX(rails_pulse_summaries.period_end) as last_seen"
    )
    .order("avg_duration DESC")
    .limit(5)

  # Build data rows
  data_rows = query_data.map do |record|
    {
      query_text: truncate_query(record.normalized_sql),
      query_id: record.query_id,
      query_link: RailsPulse::Engine.routes.url_helpers.query_path(record.query_id),
      average_time: record.avg_duration.to_f.round(0),
      request_count: record.request_count,
      last_request: time_ago_in_words(record.last_seen)
    }
  end

  # Return new structure with columns and data
  {
    columns: [
      { field: :query_text, label: "Query", link_to: :query_link, class: "w-auto" },
      { field: :average_time, label: "Average Time", class: "w-32" },
      { field: :request_count, label: "Requests", class: "w-24" },
      { field: :last_request, label: "Last Request", class: "w-32" }
    ],
    data: data_rows
  }
end