Module: SirTracksAlot

Defined in:
lib/sir_tracks_alot.rb,
lib/sir_tracks_alot/clock.rb,
lib/sir_tracks_alot/count.rb,
lib/sir_tracks_alot/summary.rb,
lib/sir_tracks_alot/activity.rb,
lib/sir_tracks_alot/persistable.rb,
lib/sir_tracks_alot/event_helper.rb,
lib/sir_tracks_alot/filter_helper.rb,
lib/sir_tracks_alot/reports/report.rb,
lib/sir_tracks_alot/queue/queue_helper.rb,
lib/sir_tracks_alot/queue/report_cache.rb,
lib/sir_tracks_alot/queue/report_queue.rb,
lib/sir_tracks_alot/queue/report_config.rb,
lib/sir_tracks_alot/reports/actor_report.rb,
lib/sir_tracks_alot/reports/basic_report.rb,
lib/sir_tracks_alot/reports/filter_report.rb,
lib/sir_tracks_alot/reports/simple_report.rb,
lib/sir_tracks_alot/reports/target_report.rb,
lib/sir_tracks_alot/reports/activity_report.rb,
lib/sir_tracks_alot/reports/root_stem_report.rb,
lib/sir_tracks_alot/reports/trackable_report.rb,
lib/sir_tracks_alot/reports/actor_activity_report.rb

Defined Under Namespace

Modules: EventHelper, FilterHelper, Helper, Queue, Reports Classes: Activity, Clock, Count, Persistable, RecordInvalidError, SirTracksAlotError, Summary

Constant Summary collapse

TRACKABLE_ROOT =
"#{File.dirname(__FILE__)}/.."

Class Method Summary collapse

Class Method Details

.clear!Object


90
91
92
# File 'lib/sir_tracks_alot.rb', line 90

def self.clear!
  @@filters = []
end

.exclude(row) ⇒ Object

add a hash of attribute/filter pairs to ignore when recording activities e.g. => /google/, => '/hidden', :actor => 'spy'


70
71
72
73
# File 'lib/sir_tracks_alot.rb', line 70

def self.exclude(row)
  @@filters ||= []
  @@filters << row
end

.exclude?(attributes) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sir_tracks_alot.rb', line 75

def self.exclude?(attributes)
  @@filters ||= []
  
  return false if @@filters.empty? || attributes.nil?
  
  @@filters.each do |filter|
    filter.each do |key, matcher|
      matcher = /#{matcher}/ if matcher.kind_of?(String)
      return true if attributes[key] =~ matcher
    end
  end
  
  false
end

.logObject


94
95
96
97
98
99
100
# File 'lib/sir_tracks_alot.rb', line 94

def log
  return @log if @log
  
  @log = Logging::Logger[self]
  @log.level = ENV['SIR_TRACKS_ALOT_LOG_LEVEL'] || 'warn'
  @log
end

.record(attributes) ⇒ Object

Creates new activity with attribues:

:owner    => string to roll up all activities by e.g. /site/1
:actor    => string representing who done it e.g. /profiles/peter-brown
:target   => string representing where they done it e.g. /messages/some-subject
:action   => what they done e.g. view or create
:event    => Time.utc.to_i of when the action activity, defaults to now

Raises:


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
65
66
# File 'lib/sir_tracks_alot.rb', line 40

def self.record(attributes)
  # event defaults to now unless overwritten
  event = (attributes.delete(:event) || Clock.now)
  request = attributes.delete(:request)
  
  # try to convert to trackable paths if possible
  [:target, :actor, :owner].each {|key| attributes[key] = attributes[key].respond_to?(:to_trackable_id) ? 
    attributes[key].to_trackable_id : 
    attributes[key].to_s}
  
  # automatically extract the root, assign to "category"
  attributes[:category] = attributes[:target].split('/')[1]
  
  # assign the user_agent from the request unless overritten 
  attributes[:user_agent] ||= (request.nil? ? nil : request.try(:user_agent))
  
  return if exclude?(attributes)
  
  # find or create
  activity = Activity.find_or_create(attributes)
  
  raise RecordInvalidError.new("Activity not valid: #{activity.errors.inspect}") unless activity.valid?

  activity.update(:last_event => event, :counted => '0')
  activity.events << event
  activity
end