Class: RailsErrorDashboard::Queries::ErrorCorrelation

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/queries/error_correlation.rb

Overview

Query object for error correlation analysis

Provides analytics for correlating errors with:

  • Releases (app_version, git_sha)

  • Users (affected users, multi-error users)

  • Time patterns (hour-of-day correlation)

Examples:

correlation = ErrorCorrelation.new(days: 30)
correlation.errors_by_version
# => { "1.0.0" => { count: 100, error_types: 15, critical_count: 5 } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days: 30) ⇒ ErrorCorrelation

Returns a new instance of ErrorCorrelation.

Parameters:

  • days (Integer) (defaults to: 30)

    Number of days to analyze (default: 30)



20
21
22
23
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 20

def initialize(days: 30)
  @days = days
  @start_date = days.days.ago
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



17
18
19
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 17

def days
  @days
end

Instance Method Details

#error_type_user_overlap(error_type_a, error_type_b) ⇒ Hash

Calculate user overlap between two error types Returns percentage of users affected by both errors

Parameters:

  • error_type_a (String)

    First error type

  • error_type_b (String)

    Second error type

Returns:

  • (Hash)

    Overlap statistics



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 147

def error_type_user_overlap(error_type_a, error_type_b)
  users_a = base_query
    .where(error_type: error_type_a)
    .where.not(user_id: nil)
    .distinct
    .pluck(:user_id)

  users_b = base_query
    .where(error_type: error_type_b)
    .where.not(user_id: nil)
    .distinct
    .pluck(:user_id)

  overlap = users_a & users_b

  {
    error_type_a: error_type_a,
    error_type_b: error_type_b,
    users_a_count: users_a.count,
    users_b_count: users_b.count,
    overlap_count: overlap.count,
    overlap_percentage: calculate_percentage(overlap.count, [ users_a.count, users_b.count ].min),
    overlapping_user_ids: overlap.first(10) # Sample of overlapping users
  }
end

#errors_by_git_shaHash

Get error statistics grouped by git SHA

Returns:

  • (Hash)

    SHA => { count, error_types, app_version }



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 60

def errors_by_git_sha
  return {} unless has_git_sha_column?

  shas = base_query
    .where.not(git_sha: nil)
    .group(:git_sha)
    .count

  shas.each_with_object({}) do |(sha, count), result|
    errors = base_query.where(git_sha: sha)

    # Get associated version (may be multiple)
    versions = errors.distinct.pluck(:app_version).compact

    result[sha] = {
      count: count,
      error_types: errors.distinct.pluck(:error_type).count,
      app_versions: versions,
      first_seen: errors.minimum(:occurred_at),
      last_seen: errors.maximum(:occurred_at)
    }
  end
end

#errors_by_versionHash

Get error statistics grouped by app version

Returns:

  • (Hash)

    Version => { count, error_types, critical_count, platforms }



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

def errors_by_version
  return {} unless has_version_column?

  versions = base_query
    .where.not(app_version: nil)
    .group(:app_version)
    .count

  versions.each_with_object({}) do |(version, count), result|
    errors = base_query.where(app_version: version)

    # Count unique error types
    error_types = errors.distinct.pluck(:error_type).count

    # Count critical errors
    critical_count = errors.select { |error| error.severity == :critical }.count

    # Get platforms for this version
    platforms = errors.distinct.pluck(:platform).compact

    result[version] = {
      count: count,
      error_types: error_types,
      critical_count: critical_count,
      platforms: platforms,
      first_seen: errors.minimum(:occurred_at),
      last_seen: errors.maximum(:occurred_at)
    }
  end
end

#multi_error_users(min_error_types: 2) ⇒ Array<Hash>

Find users affected by multiple different error types

Parameters:

  • min_error_types (Integer) (defaults to: 2)

    Minimum number of different error types (default: 2)

Returns:

  • (Array<Hash>)

    Users with multiple error type exposure



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 116

def multi_error_users(min_error_types: 2)
  users_with_errors = base_query
    .where.not(user_id: nil)
    .group(:user_id, :error_type)
    .count

  # Group by user_id
  users_by_id = users_with_errors.group_by { |(user_id, _), _| user_id }

  users_by_id
    .select { |_, error_data| error_data.count >= min_error_types }
    .map do |user_id, error_data|
      error_type_names = error_data.map { |(_, type), _| type }
      total_errors = error_data.map { |_, count| count }.sum

      {
        user_id: user_id,
        user_email: find_user_email(user_id),
        error_types: error_type_names,
        error_type_count: error_type_names.count,
        total_errors: total_errors
      }
    end
    .sort_by { |u| -u[:error_type_count] }
end

#period_comparisonHash

Compare error rates across different time periods

Returns:

  • (Hash)

    Comparison of current vs previous period



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 216

def period_comparison
  current_start = (@days / 2).days.ago
  previous_start = @start_date
  previous_end = current_start

  current_errors = ErrorLog
    .where("occurred_at >= ?", current_start)
    .count

  previous_errors = ErrorLog
    .where("occurred_at >= ? AND occurred_at < ?", previous_start, previous_end)
    .count

  change_percentage = if previous_errors > 0
    ((current_errors - previous_errors).to_f / previous_errors * 100).round(1)
  else
    current_errors > 0 ? 100.0 : 0.0
  end

  {
    current_period: {
      start: current_start,
      end: Time.current,
      count: current_errors
    },
    previous_period: {
      start: previous_start,
      end: previous_end,
      count: previous_errors
    },
    change: current_errors - previous_errors,
    change_percentage: change_percentage,
    trend: determine_trend(change_percentage)
  }
end

#platform_specific_errorsHash

Get top error types by platform Shows which errors are platform-specific vs cross-platform

Returns:

  • (Hash)

    Platform => top error types



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 255

def platform_specific_errors
  platforms = base_query.distinct.pluck(:platform).compact

  platforms.each_with_object({}) do |platform, result|
    platform_errors = base_query.where(platform: platform)
    top_errors = platform_errors
      .group(:error_type)
      .count
      .sort_by { |_, count| -count }
      .first(5)

    result[platform] = top_errors.map do |error_type, count|
      # Check if this error occurs on other platforms
      other_platforms = base_query
        .where(error_type: error_type)
        .where.not(platform: platform)
        .distinct
        .pluck(:platform)
        .compact

      {
        error_type: error_type,
        count: count,
        platform_specific: other_platforms.empty?,
        also_on: other_platforms
      }
    end
  end
end

#problematic_releasesArray<Hash>

Find problematic releases (versions with >2x average error rate)

Returns:

  • (Array<Hash>)

    Array of problematic version data



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
111
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 86

def problematic_releases
  return [] unless has_version_column?

  versions_data = errors_by_version
  return [] if versions_data.empty?

  total_errors = versions_data.values.map { |v| v[:count] }.sum
  avg_errors = total_errors.to_f / versions_data.count
  threshold = avg_errors * 2

  versions_data
    .select { |_, data| data[:count] > threshold }
    .map do |version, data|
      deviation = avg_errors > 0 ? ((data[:count] - avg_errors) / avg_errors * 100).round(1) : 0.0

      {
        version: version,
        error_count: data[:count],
        deviation_from_avg: deviation,
        critical_count: data[:critical_count],
        error_types: data[:error_types],
        platforms: data[:platforms]
      }
    end
    .sort_by { |v| -v[:error_count] }
end

#time_correlated_errorsHash

Analyze time-based correlation between error types Finds error types that tend to occur at similar hours of day

Returns:

  • (Hash)

    Error type pairs with correlation scores



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rails_error_dashboard/queries/error_correlation.rb', line 176

def time_correlated_errors
  # Get hourly distribution for each error type
  error_types = base_query.distinct.pluck(:error_type)
  return {} if error_types.count < 2

  hourly_distributions = {}
  error_types.each do |error_type|
    distribution = base_query
      .where(error_type: error_type)
      .group_by { |error| error.occurred_at.hour }
      .transform_values(&:count)

    # Normalize to 0-23 hours
    hourly_distributions[error_type] = (0..23).map { |h| distribution[h] || 0 }
  end

  # Calculate correlation between error type pairs
  correlations = {}
  error_types.combination(2).each do |type_a, type_b|
    correlation = calculate_time_correlation(
      hourly_distributions[type_a],
      hourly_distributions[type_b]
    )

    # Only include significant correlations (>0.5)
    if correlation > 0.5
      correlations["#{type_a} <-> #{type_b}"] = {
        error_type_a: type_a,
        error_type_b: type_b,
        correlation: correlation,
        strength: classify_correlation_strength(correlation)
      }
    end
  end

  correlations.sort_by { |_, v| -v[:correlation] }.to_h
end