Class: EventTimeline::RotationService

Inherits:
Object
  • Object
show all
Defined in:
lib/event_timeline/rotation_service.rb

Class Method Summary collapse

Class Method Details

.cleanup_if_neededObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/event_timeline/rotation_service.rb', line 6

def cleanup_if_needed
  return unless EventTimeline.configuration

  total_count = Session.count
  max_total = EventTimeline.configuration.max_total_events
  threshold = (max_total * EventTimeline.configuration.cleanup_threshold).to_i

  return unless total_count >= threshold

  perform_cleanup(total_count, max_total)
end

.enforce_correlation_limit(correlation_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/event_timeline/rotation_service.rb', line 18

def enforce_correlation_limit(correlation_id)
  return unless EventTimeline.configuration

  max_per_correlation = EventTimeline.configuration.max_events_per_correlation
  current_count = Session.where(correlation_id: correlation_id).count

  return unless current_count >= max_per_correlation

  # Remove oldest events for this correlation, keeping last 80%
  keep_count = (max_per_correlation * 0.8).to_i
  oldest_events = Session.where(correlation_id: correlation_id)
                         .order(:occurred_at)
                         .limit(current_count - keep_count)

  Session.where(id: oldest_events.pluck(:id)).delete_all

  Rails.logger.info "EventTimeline: Rotated #{current_count - keep_count} events for correlation #{correlation_id}" if defined?(Rails.logger)
end