Class: RailsErrorDashboard::Queries::DashboardStats
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Queries::DashboardStats
- Defined in:
- lib/rails_error_dashboard/queries/dashboard_stats.rb
Overview
Query: Fetch dashboard statistics This is a read operation that aggregates error data for the dashboard
Class Method Summary collapse
Instance Method Summary collapse
- #cache_key ⇒ Object
- #call ⇒ Object
-
#initialize(application_id: nil) ⇒ DashboardStats
constructor
A new instance of DashboardStats.
Constructor Details
#initialize(application_id: nil) ⇒ DashboardStats
Returns a new instance of DashboardStats.
8 9 10 |
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 8 def initialize(application_id: nil) @application_id = application_id end |
Class Method Details
.call(application_id: nil) ⇒ Object
12 13 14 |
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 12 def self.call(application_id: nil) new(application_id: application_id).call end |
Instance Method Details
#cache_key ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 76 def cache_key # Cache key includes last error update timestamp for auto-invalidation # Also includes current hour to ensure fresh data # Uses base_scope to respect application_id filter for proper cache isolation [ "dashboard_stats", @application_id || "all", base_scope.maximum(:updated_at)&.to_i || 0, Time.current.hour ].join("/") end |
#call ⇒ Object
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 |
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 16 def call # Cache dashboard stats for 1 minute to reduce database load # Dashboard is viewed frequently, so short cache prevents stale data begin Rails.cache.fetch(cache_key, expires_in: 1.minute) do { total_today: base_scope.where("occurred_at >= ?", Time.current.beginning_of_day).count, total_week: base_scope.where("occurred_at >= ?", 7.days.ago).count, total_month: base_scope.where("occurred_at >= ?", 30.days.ago).count, unresolved: base_scope.unresolved.count, resolved: base_scope.resolved.count, by_platform: base_scope.group(:platform).count, top_errors: top_errors, # Trend visualizations errors_trend_7d: errors_trend_7d, errors_by_severity_7d: errors_by_severity_7d, spike_detected: spike_detected?, spike_info: spike_info, # New metrics for Overview dashboard error_rate: error_rate, affected_users_today: affected_users_today, affected_users_yesterday: affected_users_yesterday, affected_users_change: affected_users_change, trend_percentage: trend_percentage, trend_direction: trend_direction, top_errors_by_impact: top_errors_by_impact, average_resolution_time: average_resolution_time } end rescue => e # If Rails.cache or any stats query fails, return empty stats hash # This prevents broadcast failures in API-only mode or when cache is unavailable RailsErrorDashboard::Logger.error("[RailsErrorDashboard] DashboardStats failed: #{e.class} - #{e.}") RailsErrorDashboard::Logger.debug("[RailsErrorDashboard] Backtrace: #{e.backtrace&.first(3)&.join("\n")}") # Return minimal stats hash to prevent nil errors in views { total_today: 0, total_week: 0, total_month: 0, unresolved: 0, resolved: 0, by_platform: {}, top_errors: {}, errors_trend_7d: {}, errors_by_severity_7d: { critical: 0, high: 0, medium: 0, low: 0 }, spike_detected: false, spike_info: nil, error_rate: 0.0, affected_users_today: 0, affected_users_yesterday: 0, affected_users_change: 0, trend_percentage: 0.0, trend_direction: :stable, top_errors_by_impact: [], average_resolution_time: nil } end end |