Class: RailsErrorDashboard::Services::DigestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/digest_builder.rb

Overview

Pure algorithm: Build a digest summary of error activity for a time period. Aggregates stats from existing queries into a single hash suitable for email templates.

Examples:

DigestBuilder.call(period: :daily)
# => { period: :daily, stats: { new_errors: 12, ... }, top_errors: [...], ... }

Constant Summary collapse

PERIODS =

Labels are keys, not literals: they leak into both the mail subject and the body, so a translated digest with an English "Last 24 hours" in its subject line would be obviously half-done (P4-T2 REQ-4).

{
  daily: { days: 1, label_key: "red.mailers.digest.periods.daily" },
  weekly: { days: 7, label_key: "red.mailers.digest.periods.weekly" }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period: :daily, application_id: nil, locale: I18nStore::DEFAULT_LOCALE) ⇒ DigestBuilder

locale is carried from the enqueue site (P4-T1) and is what #period_label resolves against. Never Current: a digest is built inside a job, where Current is nil or belongs to an unrelated request.



27
28
29
30
31
32
33
# File 'lib/rails_error_dashboard/services/digest_builder.rb', line 27

def initialize(period: :daily, application_id: nil, locale: I18nStore::DEFAULT_LOCALE)
  @period = PERIODS.key?(period) ? period : :daily
  @days = PERIODS[@period][:days]
  @application_id = application_id
  @locale = locale
  @start_date = @days.days.ago
end

Class Method Details

.call(period: :daily, application_id: nil, locale: I18nStore::DEFAULT_LOCALE) ⇒ Object



20
21
22
# File 'lib/rails_error_dashboard/services/digest_builder.rb', line 20

def self.call(period: :daily, application_id: nil, locale: I18nStore::DEFAULT_LOCALE)
  new(period: period, application_id: application_id, locale: locale).call
end

Instance Method Details

#callObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_error_dashboard/services/digest_builder.rb', line 35

def call
  {
    period: @period,
    period_label: period_label,
    generated_at: Time.current,
    stats: build_stats,
    top_errors: top_errors,
    critical_unresolved: critical_unresolved,
    comparison: build_comparison
  }
rescue => e
  Rails.logger.error("[RailsErrorDashboard] DigestBuilder failed: #{e.class}: #{e.message}")
  empty_result
end