Class: Anomonitor::Digester
- Inherits:
-
Object
- Object
- Anomonitor::Digester
- Defined in:
- lib/anomonitor/digester.rb
Overview
Batches anomaly.detected notifications into a single anomaly.digest payload.
Class Method Summary collapse
Instance Method Summary collapse
- #flush! ⇒ Object
- #flush_if_due! ⇒ Object
-
#initialize(notifier: nil) ⇒ Digester
constructor
A new instance of Digester.
- #record(anomaly) ⇒ Object
Constructor Details
#initialize(notifier: nil) ⇒ Digester
Returns a new instance of Digester.
14 15 16 |
# File 'lib/anomonitor/digester.rb', line 14 def initialize(notifier: nil) @notifier = notifier || Anomonitor.config.build_notifier end |
Class Method Details
.flush_if_due!(notifier: nil) ⇒ Object
10 11 12 |
# File 'lib/anomonitor/digester.rb', line 10 def self.flush_if_due!(notifier: nil) new(notifier: notifier).flush_if_due! end |
.record(anomaly) ⇒ Object
6 7 8 |
# File 'lib/anomonitor/digester.rb', line 6 def self.record(anomaly) new.record(anomaly) end |
Instance Method Details
#flush! ⇒ Object
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 |
# File 'lib/anomonitor/digester.rb', line 36 def flush! queued = Anomonitor::Anomaly.where(webhook_status: "queued").order(:created_at).limit(100).to_a return 0 if queued.empty? path = Anomonitor.config.dashboard_path.to_s.sub(%r{/+\z}, "") base = Anomonitor.config.dashboard_base_url.to_s.strip.sub(%r{/+\z}, "") dash = base.empty? ? "#{path}/anomalies" : "#{base}#{path}/anomalies" payload = { gem: "anomonitor", event: Notifiers::DIGEST, count: queued.size, dashboard_url: dash, anomalies: queued.map { |a| Notifiers.payload(a, event: Notifiers::DETECTED) } } delivered = deliver_digest(payload) queued.each do |anomaly| anomaly.update!( webhook_status: delivered ? "delivered" : "failed", webhook_delivered_at: delivered ? Time.current : nil ) end Anomonitor.config.digest_last_flushed_at = Time.current queued.size rescue StandardError => e Anomonitor.logger.warn("[Anomonitor] Digester flush failed: #{e.}") 0 end |
#flush_if_due! ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/anomonitor/digester.rb', line 26 def flush_if_due! interval = Anomonitor.config.digest_interval.to_i return 0 if interval <= 0 last = Anomonitor.config.digest_last_flushed_at return 0 if last && Time.current - last < interval flush! end |
#record(anomaly) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/anomonitor/digester.rb', line 18 def record(anomaly) anomaly.update!(webhook_status: "queued") true rescue StandardError => e Anomonitor.logger.warn("[Anomonitor] Digester record failed: #{e.}") false end |