Class: Ask::Agent::Extensions::AuditLog::ActiveRecordWriter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/ask/agent/extensions/audit_log/active_record_writer.rb

Overview

ActiveRecord adapter for the audit log. Auto-creates the ask_audit_logs table on first write using CREATE TABLE IF NOT EXISTS, so it works with or without Rails migrations. Rails users can also run:

rails generate ask_rails:install

to get a proper migration file. The migration uses if_not_exists: true so it won't conflict with auto-creation.

Constant Summary collapse

TABLE_NAME =
"ask_audit_logs"

Instance Method Summary collapse

Constructor Details

#initializeActiveRecordWriter

Returns a new instance of ActiveRecordWriter.



19
20
21
22
# File 'lib/ask/agent/extensions/audit_log/active_record_writer.rb', line 19

def initialize
  @table_checked = false
  @mutex = Mutex.new
end

Instance Method Details

#write(entry) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ask/agent/extensions/audit_log/active_record_writer.rb', line 24

def write(entry)
  return unless defined?(ActiveRecord::Base)

  ensure_table!
  conn = ActiveRecord::Base.connection
  conn.execute(
    "INSERT INTO #{TABLE_NAME} (session_id, event_type, data, timestamp, created_at, updated_at) " \
    "VALUES (#{quote(entry[:session_id])}, #{quote(entry[:event_type])}, " \
    "#{quote(entry[:data].to_json)}, #{quote(entry[:timestamp])}, " \
    "#{quote(Time.now.utc.iso8601(3))}, #{quote(Time.now.utc.iso8601(3))})"
  )
rescue ActiveRecord::ActiveRecordError => e
  warn "[ask-agent] AuditLog::ActiveRecordWriter write failed: #{e.message}"
end