Class: RailsPulse::Queries::Cards::ExecutionRate
- Inherits:
-
Object
- Object
- RailsPulse::Queries::Cards::ExecutionRate
- Defined in:
- app/models/rails_pulse/queries/cards/execution_rate.rb
Instance Method Summary collapse
-
#initialize(query: nil, disabled_tags: [], show_non_tagged: true) ⇒ ExecutionRate
constructor
A new instance of ExecutionRate.
- #to_metric_card ⇒ Object
Constructor Details
#initialize(query: nil, disabled_tags: [], show_non_tagged: true) ⇒ ExecutionRate
Returns a new instance of ExecutionRate.
5 6 7 8 9 |
# File 'app/models/rails_pulse/queries/cards/execution_rate.rb', line 5 def initialize(query: nil, disabled_tags: [], show_non_tagged: true) @query = query @disabled_tags = @show_non_tagged = show_non_tagged end |
Instance Method Details
#to_metric_card ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/rails_pulse/queries/cards/execution_rate.rb', line 11 def to_metric_card last_7_days = 7.days.ago.beginning_of_day previous_7_days = 14.days.ago.beginning_of_day # Get the most common period type for this query, or fall back to "day" period_type = if @query RailsPulse::Summary .with_tag_filters(@disabled_tags, @show_non_tagged) .where( summarizable_type: "RailsPulse::Query", summarizable_id: @query.id ).group(:period_type).count.max_by(&:last)&.first || "day" else "day" end # Single query to get all count metrics with conditional aggregation base_query = RailsPulse::Summary .with_tag_filters(@disabled_tags, @show_non_tagged) .where( summarizable_type: "RailsPulse::Query", period_type: period_type, period_start: 2.weeks.ago.beginning_of_day..Time.current ) base_query = base_query.where(summarizable_id: @query.id) if @query metrics = base_query.select( "SUM(count) AS total_count", "SUM(CASE WHEN period_start >= '#{last_7_days.strftime('%Y-%m-%d %H:%M:%S')}' THEN count ELSE 0 END) AS current_count", "SUM(CASE WHEN period_start >= '#{previous_7_days.strftime('%Y-%m-%d %H:%M:%S')}' AND period_start < '#{last_7_days.strftime('%Y-%m-%d %H:%M:%S')}' THEN count ELSE 0 END) AS previous_count" ).take # Calculate metrics from single query result total_execution_count = metrics.total_count || 0 current_period_count = metrics.current_count || 0 previous_period_count = metrics.previous_count || 0 percentage = previous_period_count.zero? ? 0 : ((previous_period_count - current_period_count) / previous_period_count.to_f * 100).abs.round(1) trend_icon = percentage < 0.1 ? "move-right" : current_period_count < previous_period_count ? "trending-down" : "trending-up" trend_amount = previous_period_count.zero? ? "0%" : "#{percentage}%" # Sparkline data with zero-filled periods over the last 14 days if period_type == "day" grouped_data = base_query .group_by_day(:period_start, time_zone: "UTC") .sum(:count) start_period = 2.weeks.ago.beginning_of_day.to_date end_period = Time.current.to_date sparkline_data = {} (start_period..end_period).each do |day| total = grouped_data[day] || 0 label = day.strftime("%b %-d") sparkline_data[label] = { value: total } end else # For hourly data, group by day for sparkline display grouped_data = base_query .group("DATE(period_start)") .sum(:count) start_period = 2.weeks.ago.beginning_of_day.to_date end_period = Time.current.to_date sparkline_data = {} (start_period..end_period).each do |day| date_key = day.strftime("%Y-%m-%d") total = grouped_data[date_key] || 0 label = day.strftime("%b %-d") sparkline_data[label] = { value: total } end end # Calculate appropriate rate display based on frequency total_minutes = 2.weeks / 1.minute.to_f executions_per_minute = total_execution_count.to_f / total_minutes # Choose appropriate time unit for display if executions_per_minute >= 1 summary = "#{executions_per_minute.round(2)} / min" elsif executions_per_minute * 60 >= 1 executions_per_hour = executions_per_minute * 60 summary = "#{executions_per_hour.round(2)} / hour" else executions_per_day = executions_per_minute * 60 * 24 summary = "#{executions_per_day.round(2)} / day" end { id: "execution_rate", context: "queries", title: "Execution Rate", summary: summary, chart_data: sparkline_data, trend_icon: trend_icon, trend_amount: trend_amount, trend_text: "Compared to last week" } end |