Class: Legion::Gaia::NotificationGate

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/gaia/notification_gate.rb,
lib/legion/gaia/notification_gate/delay_queue.rb,
lib/legion/gaia/notification_gate/presence_evaluator.rb,
lib/legion/gaia/notification_gate/schedule_evaluator.rb,
lib/legion/gaia/notification_gate/behavioral_evaluator.rb

Defined Under Namespace

Classes: BehavioralEvaluator, DelayQueue, PresenceEvaluator, ScheduleEvaluator

Constant Summary collapse

PRIORITY_VALUES =
{ critical: 1.0, urgent: 0.8, normal: 0.5, low: 0.2, ambient: 0.0 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings: {}) ⇒ NotificationGate

Returns a new instance of NotificationGate.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/gaia/notification_gate.rb', line 15

def initialize(settings: {})
  notification_settings = settings[:notifications] || {}
  @enabled = notification_settings[:enabled] != false
  @priority_override = notification_settings[:priority_override] || :urgent

  schedule = notification_settings.dig(:quiet_hours, :schedule) || []
  quiet_enabled = notification_settings.dig(:quiet_hours, :enabled) != false
  @schedule_evaluator = ScheduleEvaluator.new(schedule: quiet_enabled ? schedule : [])

  max_size = notification_settings[:delay_queue_max] || 100
  max_delay = notification_settings[:max_delay] || 14_400
  @delay_queue = DelayQueue.new(max_size: max_size, max_delay: max_delay)
  @presence_evaluator = PresenceEvaluator.new
  @behavioral_evaluator = BehavioralEvaluator.new
end

Instance Attribute Details

#behavioral_evaluatorObject (readonly)

Returns the value of attribute behavioral_evaluator.



13
14
15
# File 'lib/legion/gaia/notification_gate.rb', line 13

def behavioral_evaluator
  @behavioral_evaluator
end

#delay_queueObject (readonly)

Returns the value of attribute delay_queue.



13
14
15
# File 'lib/legion/gaia/notification_gate.rb', line 13

def delay_queue
  @delay_queue
end

#presence_evaluatorObject (readonly)

Returns the value of attribute presence_evaluator.



13
14
15
# File 'lib/legion/gaia/notification_gate.rb', line 13

def presence_evaluator
  @presence_evaluator
end

#schedule_evaluatorObject (readonly)

Returns the value of attribute schedule_evaluator.



13
14
15
# File 'lib/legion/gaia/notification_gate.rb', line 13

def schedule_evaluator
  @schedule_evaluator
end

Instance Method Details

#enqueue(frame) ⇒ Object



52
53
54
# File 'lib/legion/gaia/notification_gate.rb', line 52

def enqueue(frame)
  @delay_queue.enqueue(frame)
end

#evaluate(frame) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/gaia/notification_gate.rb', line 31

def evaluate(frame)
  return :deliver unless @enabled
  return :deliver if priority_overrides?(frame)
  return :delay if @schedule_evaluator.quiet?

  priority = frame.[:priority] || :normal
  return :delay unless @presence_evaluator.delivery_allowed?(priority: priority)
  return :delay unless @behavioral_evaluator.should_deliver?(priority: priority)

  :deliver
end

#flushObject



68
69
70
# File 'lib/legion/gaia/notification_gate.rb', line 68

def flush
  @delay_queue.flush.map { |e| e[:frame] }
end

#pending_countObject



72
73
74
# File 'lib/legion/gaia/notification_gate.rb', line 72

def pending_count
  @delay_queue.size
end

#process_delayedObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/gaia/notification_gate.rb', line 56

def process_delayed
  expired = @delay_queue.drain_expired
  deliverable = expired.map { |e| e[:frame] }

  unless @schedule_evaluator.quiet?
    flushed = @delay_queue.flush
    deliverable.concat(flushed.map { |e| e[:frame] })
  end

  deliverable
end

#update_behavioral(arousal: nil, idle_seconds: nil) ⇒ Object



43
44
45
46
# File 'lib/legion/gaia/notification_gate.rb', line 43

def update_behavioral(arousal: nil, idle_seconds: nil)
  @behavioral_evaluator.update_arousal(arousal) if arousal
  @behavioral_evaluator.update_idle_seconds(idle_seconds) if idle_seconds
end

#update_presence(availability:, activity: nil) ⇒ Object



48
49
50
# File 'lib/legion/gaia/notification_gate.rb', line 48

def update_presence(availability:, activity: nil)
  @presence_evaluator.update(availability: availability, activity: activity)
end