Module: Analytics::Maintenance

Included in:
Analytics
Defined in:
lib/analytics_logger/analytics/maintenance.rb

Overview

The maintenance methods associated with the analytics

Constant Summary collapse

CLEAR_EVENTS_OBJECT_OPTIONS =
{
  :all    => option_value_must_be_a(TrueClass),
  :audit  => option_value_must_be_a(TrueClass),
  :no_log => option_value_must_be_a(TrueClass),
  :since  => option_value_must_be_a(Time),
  :before => option_value_must_be_a(Time)
}
CLEAR_EVENTS_OBJECT_OR_ARRAY_OPTIONS =
{
  :level    => lambda { |option_value| AnalyticsEventType::LEVELS.include?(option_value) },
  :type     => option_value_must_be_a(AnalyticsEventType),
  :user_id  => option_value_must_be_a(Fixnum)
}
VALID_CLEAR_EVENTS_OPTIONS =
CLEAR_EVENTS_OBJECT_OPTIONS.keys + CLEAR_EVENTS_OBJECT_OR_ARRAY_OPTIONS.keys
CLEAR_EVENTS_HELP =
<<-CLEAR_EVENTS_HELP_TEXT
At least one of the following options must be specified:
:all      => true
  Clear all analytics events; if specified, must be the only option.

:audit    => true
  Clear audit level analytics events, which by default are not cleared.

:no_log   => true
  Do not log an analytics event indicating that the log was cleared.

:since    => <time>
  Clear only analytics events occurring since the specified time.

:before   => <time>
  Clear only analytics events occurring before the specified time.

:level    => <level or array of levels>
  Clear only analytics events belonging to the specified level(s).

:type     => <event type or array of event types>
  Clear only analytics events belonging to the specified type(s).

:user_id  => <user id or array of user ids>
  Clear only analytics events associated with the specified user(s).

CLEAR_EVENTS_HELP_TEXT

Instance Method Summary collapse

Instance Method Details

#clean_up_eventsObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/analytics_logger/analytics/maintenance.rb', line 55

def clean_up_events
  AnalyticsEventType.all.each do |event_type|
    # Make sure the counter cache is correct in case it became out-of-sync.
    AnalyticsEventType.reset_counters(event_type.id, :analytics_events)

    # The event type is no longer needed if there are no associated events.
    if event_type.analytics_events_count == 0
      event_type.destroy
    end
  end
  true
end

#clear_events(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/analytics_logger/analytics/maintenance.rb', line 68

def clear_events(options = {})
  begin
    validate_clear_events_options(options)

    events_to_clear = AnalyticsEvent
    events_to_clear = events_to_clear.is_not_audit_level            unless options[:audit]
    events_to_clear = events_to_clear.since_time(options[:since])   if options[:since]
    events_to_clear = events_to_clear.before_time(options[:before]) if options[:before]
    events_to_clear = events_to_clear.in_level(options[:level])     if options[:level]
    events_to_clear = events_to_clear.in_type(options[:type])       if options[:type]
    events_to_clear = events_to_clear.for_users(options[:user_id])  if options[:user_id]
    events_to_clear = events_to_clear.all

    puts "Clearing #{events_to_clear.count} events..."
    events_to_clear.each(&:destroy)

    if !options[:audit] && AnalyticsEvent.in_level(AnalyticsEventType::AUDIT).count > 0
      puts
      puts 'Note: Audit level events are not cleared unless :audit => true is specified.'
    end

    Analytics.audit('Analytics: Cleared analytics events', :count => events_to_clear.count, :options => options) unless options[:no_log] || events_to_clear.count == 0

    # Clean up the state of analytics objects after clearing.
    clean_up_events
  rescue ArgumentError => error
    puts "#{error.class.name}: #{error.message}"
    puts
    puts CLEAR_EVENTS_HELP
    false
  end
end