Class: DecisionAgent::Monitoring::Storage::MemoryAdapter

Inherits:
BaseAdapter
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/decision_agent/monitoring/storage/memory_adapter.rb

Overview

In-memory adapter for metrics storage (default, no dependencies)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window_size: 3600) ⇒ MemoryAdapter

Returns a new instance of MemoryAdapter.



13
14
15
16
17
18
19
20
21
22
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 13

def initialize(window_size: 3600)
  super()
  @window_size = window_size
  @metrics = {
    decisions: [],
    evaluations: [],
    performance: [],
    errors: []
  }
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 142

def self.available?
  true # Always available, no dependencies
end

Instance Method Details

#cleanup(older_than:) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 127

def cleanup(older_than:)
  synchronize do
    cutoff = Time.now - older_than
    count = 0

    @metrics.each_value do |metric_array|
      before_size = metric_array.size
      metric_array.reject! { |m| m[:timestamp] < cutoff }
      count += before_size - metric_array.size
    end

    count
  end
end

#metrics_countObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 116

def metrics_count
  synchronize do
    {
      decisions: @metrics[:decisions].size,
      evaluations: @metrics[:evaluations].size,
      performance: @metrics[:performance].size,
      errors: @metrics[:errors].size
    }
  end
end

#record_decision(decision, context, confidence: nil, evaluations_count: 0, duration_ms: nil, status: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 24

def record_decision(decision, context, confidence: nil, evaluations_count: 0, duration_ms: nil, status: nil)
  synchronize do
    @metrics[:decisions] << {
      decision: decision,
      context: context,
      confidence: confidence,
      evaluations_count: evaluations_count,
      duration_ms: duration_ms,
      status: status,
      timestamp: Time.now
    }
    cleanup_old_metrics
  end
end

#record_error(error_type, message: nil, stack_trace: nil, severity: nil, context: {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 66

def record_error(error_type, message: nil, stack_trace: nil, severity: nil, context: {})
  synchronize do
    @metrics[:errors] << {
      error_type: error_type,
      message: message,
      stack_trace: stack_trace,
      severity: severity,
      context: context,
      timestamp: Time.now
    }
    cleanup_old_metrics
  end
end

#record_evaluation(evaluator_name, score: nil, success: nil, duration_ms: nil, details: {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 39

def record_evaluation(evaluator_name, score: nil, success: nil, duration_ms: nil, details: {})
  synchronize do
    @metrics[:evaluations] << {
      evaluator_name: evaluator_name,
      score: score,
      success: success,
      duration_ms: duration_ms,
      details: details,
      timestamp: Time.now
    }
    cleanup_old_metrics
  end
end

#record_performance(operation, duration_ms: nil, status: nil, metadata: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 53

def record_performance(operation, duration_ms: nil, status: nil, metadata: {})
  synchronize do
    @metrics[:performance] << {
      operation: operation,
      duration_ms: duration_ms,
      status: status,
      metadata: ,
      timestamp: Time.now
    }
    cleanup_old_metrics
  end
end

#statistics(time_range: 3600) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 80

def statistics(time_range: 3600)
  synchronize do
    cutoff = Time.now - time_range
    recent_decisions = @metrics[:decisions].select { |m| m[:timestamp] >= cutoff }
    recent_evaluations = @metrics[:evaluations].select { |m| m[:timestamp] >= cutoff }
    recent_performance = @metrics[:performance].select { |m| m[:timestamp] >= cutoff }
    recent_errors = @metrics[:errors].select { |m| m[:timestamp] >= cutoff }

    {
      decisions: decision_statistics(recent_decisions),
      evaluations: evaluation_statistics(recent_evaluations),
      performance: performance_statistics(recent_performance),
      errors: error_statistics(recent_errors)
    }
  end
end

#time_series(metric_type, bucket_size: 60, time_range: 3600) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/decision_agent/monitoring/storage/memory_adapter.rb', line 97

def time_series(metric_type, bucket_size: 60, time_range: 3600)
  synchronize do
    cutoff = Time.now - time_range
    metrics = (@metrics[metric_type] || []).select { |m| m[:timestamp] >= cutoff }

    buckets = Hash.new(0)
    metrics.each do |metric|
      bucket = (metric[:timestamp].to_i / bucket_size) * bucket_size
      buckets[bucket] += 1
    end

    timestamps = buckets.keys.sort
    {
      timestamps: timestamps.map { |ts| Time.at(ts).iso8601 },
      data: timestamps.map { |ts| buckets[ts] }
    }
  end
end