Class: Legion::Extensions::Exec::Helpers::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/exec/helpers/audit_log.rb

Constant Summary collapse

MAX_ENTRIES =
1000

Instance Method Summary collapse

Constructor Details

#initializeAuditLog

Returns a new instance of AuditLog.



10
11
12
13
# File 'lib/legion/extensions/exec/helpers/audit_log.rb', line 10

def initialize
  @entries = []
  @mutex   = Mutex.new
end

Instance Method Details

#clearObject



46
47
48
# File 'lib/legion/extensions/exec/helpers/audit_log.rb', line 46

def clear
  @mutex.synchronize { @entries.clear }
end

#entries(limit: 50) ⇒ Object



31
32
33
# File 'lib/legion/extensions/exec/helpers/audit_log.rb', line 31

def entries(limit: 50)
  @mutex.synchronize { @entries.last(limit) }
end

#record(command:, cwd:, exit_code:, duration_ms:, truncated: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/exec/helpers/audit_log.rb', line 15

def record(command:, cwd:, exit_code:, duration_ms:, truncated: false)
  entry = {
    command:     command,
    cwd:         cwd,
    exit_code:   exit_code,
    duration_ms: duration_ms,
    truncated:   truncated,
    executed_at: Time.now.utc.iso8601
  }

  @mutex.synchronize do
    @entries << entry
    @entries.shift while @entries.size > MAX_ENTRIES
  end
end

#statsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/exec/helpers/audit_log.rb', line 35

def stats
  @mutex.synchronize do
    total    = @entries.size
    success  = @entries.count { |e| e[:exit_code].zero? }
    failure  = total - success
    avg_dur  = total.zero? ? 0 : (@entries.sum { |e| e[:duration_ms] } / total.to_f).round(2)

    { total: total, success: success, failure: failure, avg_duration_ms: avg_dur }
  end
end