Class: RailsErrorDashboard::Queries::DashboardStats

Inherits:
Object
  • Object
show all
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

Class Method Details

.callObject



8
9
10
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 8

def self.call
  new.call
end

Instance Method Details

#cache_keyObject



41
42
43
44
45
46
47
48
49
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 41

def cache_key
  # Cache key includes last error update timestamp for auto-invalidation
  # Also includes current hour to ensure fresh data
  [
    "dashboard_stats",
    ErrorLog.maximum(:updated_at)&.to_i || 0,
    Time.current.hour
  ].join("/")
end

#callObject



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
# File 'lib/rails_error_dashboard/queries/dashboard_stats.rb', line 12

def call
  # Cache dashboard stats for 1 minute to reduce database load
  # Dashboard is viewed frequently, so short cache prevents stale data
  Rails.cache.fetch(cache_key, expires_in: 1.minute) do
    {
      total_today: ErrorLog.where("occurred_at >= ?", Time.current.beginning_of_day).count,
      total_week: ErrorLog.where("occurred_at >= ?", 7.days.ago).count,
      total_month: ErrorLog.where("occurred_at >= ?", 30.days.ago).count,
      unresolved: ErrorLog.unresolved.count,
      resolved: ErrorLog.resolved.count,
      by_platform: ErrorLog.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
    }
  end
end