Class: Lutaml::Store::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/monitor.rb

Instance Method Summary collapse

Constructor Details

#initializeMonitor

Returns a new instance of Monitor.



6
7
8
9
10
11
12
13
14
15
# File 'lib/lutaml/store/monitor.rb', line 6

def initialize
  @stats = {
    operations: Hash.new(0),
    errors: Hash.new(0),
    access_times: {},
    total_access_count: 0
  }
  @mutex = Mutex.new
  @start_time = Time.now
end

Instance Method Details

#operation_stats(operation) ⇒ Hash

Get statistics for a specific operation

Parameters:

  • operation (Symbol)

    the operation to get stats for

Returns:

  • (Hash)

    operation-specific statistics



66
67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/store/monitor.rb', line 66

def operation_stats(operation)
  @mutex.synchronize do
    {
      count: @stats[:operations][operation],
      errors: @stats[:errors][operation],
      error_rate: calculate_operation_error_rate(operation),
      performance: calculate_operation_performance(operation)
    }
  end
end

#record_error(operation, _error) ⇒ Object

Record an error

Parameters:

  • operation (Symbol)

    the operation that failed

  • error (Exception)

    the error that occurred



41
42
43
44
45
46
# File 'lib/lutaml/store/monitor.rb', line 41

def record_error(operation, _error)
  @mutex.synchronize do
    @stats[:errors][operation] += 1
    @stats[:errors][:total] += 1
  end
end

#record_operation(operation, duration: nil, success: true) ⇒ Object

Record an operation

Parameters:

  • operation (Symbol)

    the operation type (:get, :set, :delete, :clear, etc.)

  • duration (Float) (defaults to: nil)

    operation duration in seconds

  • success (Boolean) (defaults to: true)

    whether the operation succeeded



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lutaml/store/monitor.rb', line 21

def record_operation(operation, duration: nil, success: true)
  @mutex.synchronize do
    @stats[:operations][operation] += 1
    @stats[:total_access_count] += 1

    @stats[:errors][operation] += 1 unless success

    if duration
      @stats[:access_times][operation] ||= []
      @stats[:access_times][operation] << duration

      # Keep only last 1000 measurements to prevent memory bloat
      @stats[:access_times][operation].shift if @stats[:access_times][operation].size > 1000
    end
  end
end

#reportString

Get a summary report

Returns:

  • (String)

    formatted statistics report



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lutaml/store/monitor.rb', line 90

def report
  current_stats = stats

  <<~REPORT
    Lutaml::Store Monitor Report
    ============================

    Uptime: #{format_duration(current_stats[:uptime])}
    Total Operations: #{current_stats[:total_operations]}
    Overall Error Rate: #{(current_stats[:error_rate] * 100).round(2)}%

    Operations:
    #{format_operations(current_stats[:operations])}

    Errors:
    #{format_errors(current_stats[:errors])}

    Performance:
    #{format_performance(current_stats[:performance])}
  REPORT
end

#resetObject

Reset all statistics



78
79
80
81
82
83
84
85
86
# File 'lib/lutaml/store/monitor.rb', line 78

def reset
  @mutex.synchronize do
    @stats[:operations].clear
    @stats[:errors].clear
    @stats[:access_times].clear
    @stats[:total_access_count] = 0
    @start_time = Time.now
  end
end

#statsHash

Get current statistics

Returns:

  • (Hash)

    current monitoring statistics



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lutaml/store/monitor.rb', line 50

def stats
  @mutex.synchronize do
    {
      uptime: Time.now - @start_time,
      total_operations: @stats[:total_access_count],
      operations: @stats[:operations].dup,
      errors: @stats[:errors].dup,
      performance: calculate_performance_stats,
      error_rate: calculate_error_rate
    }
  end
end