Class: RailsPulse::Routes::Cards::RequestCountTotals
- Inherits:
-
Object
- Object
- RailsPulse::Routes::Cards::RequestCountTotals
- Defined in:
- app/models/rails_pulse/routes/cards/request_count_totals.rb
Instance Method Summary collapse
-
#initialize(route: nil, disabled_tags: [], show_non_tagged: true) ⇒ RequestCountTotals
constructor
A new instance of RequestCountTotals.
- #to_metric_card ⇒ Object
Constructor Details
#initialize(route: nil, disabled_tags: [], show_non_tagged: true) ⇒ RequestCountTotals
Returns a new instance of RequestCountTotals.
5 6 7 8 9 |
# File 'app/models/rails_pulse/routes/cards/request_count_totals.rb', line 5 def initialize(route: nil, disabled_tags: [], show_non_tagged: true) @route = route @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 |
# File 'app/models/rails_pulse/routes/cards/request_count_totals.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 # 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::Route", period_type: "day", period_start: 2.weeks.ago.beginning_of_day..Time.current ) base_query = base_query.where(summarizable_id: @route.id) if @route 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_request_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 by day with zero-filled days over the last 14 days grouped_daily = base_query .group_by_day(:period_start, time_zone: "UTC") .sum(:count) start_day = 2.weeks.ago.beginning_of_day.to_date end_day = Time.current.to_date sparkline_data = {} (start_day..end_day).each do |day| total = grouped_daily[day] || 0 label = day.strftime("%b %-d") sparkline_data[label] = { value: total } end # Calculate appropriate rate display based on frequency total_minutes = 2.weeks / 1.minute.to_f requests_per_minute = total_request_count.to_f / total_minutes # Choose appropriate time unit for display if requests_per_minute >= 1 summary = "#{requests_per_minute.round(2)} / min" elsif requests_per_minute * 60 >= 1 requests_per_hour = requests_per_minute * 60 summary = "#{requests_per_hour.round(2)} / hour" else requests_per_day = requests_per_minute * 60 * 24 summary = "#{requests_per_day.round(2)} / day" end { id: "request_count_totals", context: "routes", title: "Request Count Total", summary: summary, chart_data: sparkline_data, trend_icon: trend_icon, trend_amount: trend_amount, trend_text: "Compared to last week" } end |