Class: MnoEnterprise::EventLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/mno_enterprise/event_logger.rb

Overview

EventLogger to log various action performed by the end users (eg: sign in, add an app, …) The EventLogger will enqueue notifications and dispatch them to the various listeners. The listeners can then process these event in any way they see fit (Audit Log, Analytics, …)

Constant Summary collapse

@@listeners =
[AuditEventsListener.new]

Class Method Summary collapse

Class Method Details

.format_metadata(metadata, object) ⇒ Object

Get the metadata from the object if not provided



42
43
44
45
46
47
48
# File 'lib/mno_enterprise/event_logger.rb', line 42

def self.(, object)
  if .blank? && object.respond_to?(:to_audit_event)
    object.to_audit_event
  else
    
  end
end

.info(key, current_user_id, description, object, metadata = {}) ⇒ Object

Enqueue a logging job to be performed later

Parameters:

  • key (String)

    unique key identifying the event type

  • current_user_id (Integer)

    user_id of the user triggering the event

  • description (String)

    humanised description

  • metadata (Object) (defaults to: {})
  • object (Object)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mno_enterprise/event_logger.rb', line 18

def self.info(key, current_user_id, description, object,  = {})
   = (, object)
  subject_type = object.class.name
  subject_id = object.id
  # TODO: improve
  # Bypass Job queuing in specs or we'd have to stub lots of Her call for the deserialization
  if Rails.env.test?
    self.send_info(key, current_user_id, description, subject_type, subject_id, )
  else
    MnoEnterprise::EventLoggerJob.perform_later('info', key, current_user_id, description, subject_type, subject_id, )
  end
rescue ActiveJob::SerializationError
  Rails.logger.warn "[MnoEnterprise::EventLogger] Serialization error, skipping #{key} event"
end

.send_info(key, current_user_id, description, subject_type, subject_id, metadata) ⇒ Object

Send the event to the listeners



35
36
37
38
39
# File 'lib/mno_enterprise/event_logger.rb', line 35

def self.send_info(key, current_user_id, description, subject_type, subject_id, )
  @@listeners.each do |listener|
    listener.info(key, current_user_id, description, subject_type, subject_id, )
  end
end