Class: RailsPulse::Dashboard::Tables::SlowQueries
- Inherits:
-
Object
- Object
- RailsPulse::Dashboard::Tables::SlowQueries
- 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, #time_ago_in_words
Instance Method Details
#to_table_data ⇒ Object
6 7 8 9 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 |
# File 'app/models/rails_pulse/dashboard/tables/slow_queries.rb', line 6 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 for this week query_data = RailsPulse::Operation.joins(:query) .where(occurred_at: this_week_start..this_week_end) .group("rails_pulse_queries.id, rails_pulse_queries.normalized_sql") .select("rails_pulse_queries.id, rails_pulse_queries.normalized_sql, AVG(rails_pulse_operations.duration) as avg_duration, COUNT(*) as request_count, MAX(rails_pulse_operations.occurred_at) 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.id, query_link: "/rails_pulse/queries/#{record.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 |