Class: KBS::Blackboard::RedisAuditLog
- Inherits:
-
Object
- Object
- KBS::Blackboard::RedisAuditLog
- Defined in:
- lib/kbs/blackboard/redis_audit_log.rb
Overview
Redis-based audit log using lists for history
Instance Method Summary collapse
- #fact_history(fact_uuid = nil, limit: 100) ⇒ Object
-
#initialize(redis, session_id) ⇒ RedisAuditLog
constructor
A new instance of RedisAuditLog.
- #log_fact_change(fact_uuid, fact_type, attributes, action) ⇒ Object
- #log_rule_firing(rule_name, fact_uuids, bindings = {}) ⇒ Object
- #rule_firings(rule_name = nil, limit: 100) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(redis, session_id) ⇒ RedisAuditLog
Returns a new instance of RedisAuditLog.
10 11 12 13 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 10 def initialize(redis, session_id) @redis = redis @session_id = session_id end |
Instance Method Details
#fact_history(fact_uuid = nil, limit: 100) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 65 def fact_history(fact_uuid = nil, limit: 100) key = fact_uuid ? "fact_history:#{fact_uuid}" : 'fact_history:all' entries_json = @redis.lrange(key, 0, limit - 1) entries_json.map do |entry_json| entry = JSON.parse(entry_json, symbolize_names: true) { fact_uuid: entry[:fact_uuid], fact_type: entry[:fact_type].to_sym, attributes: JSON.parse(entry[:attributes], symbolize_names: true), action: entry[:action], timestamp: Time.at(entry[:timestamp]), session_id: entry[:session_id] } end end |
#log_fact_change(fact_uuid, fact_type, attributes, action) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 15 def log_fact_change(fact_uuid, fact_type, attributes, action) attributes_json = attributes.is_a?(String) ? attributes : JSON.generate(attributes) = Time.now.to_f entry = { 'fact_uuid' => fact_uuid, 'fact_type' => fact_type.to_s, 'attributes' => attributes_json, 'action' => action, 'timestamp' => , 'session_id' => @session_id } entry_json = JSON.generate(entry) # Add to global history (as list - newest first) @redis.lpush('fact_history:all', entry_json) # Add to fact-specific history @redis.lpush("fact_history:#{fact_uuid}", entry_json) # Optionally limit history size (e.g., keep last 10000 entries) @redis.ltrim('fact_history:all', 0, 9999) @redis.ltrim("fact_history:#{fact_uuid}", 0, 999) end |
#log_rule_firing(rule_name, fact_uuids, bindings = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 41 def log_rule_firing(rule_name, fact_uuids, bindings = {}) = Time.now.to_f entry = { 'rule_name' => rule_name, 'fact_uuids' => JSON.generate(fact_uuids), 'bindings' => JSON.generate(bindings), 'fired_at' => , 'session_id' => @session_id } entry_json = JSON.generate(entry) # Add to global rules fired list @redis.lpush('rules_fired:all', entry_json) # Add to rule-specific history @redis.lpush("rules_fired:#{rule_name}", entry_json) # Limit size @redis.ltrim('rules_fired:all', 0, 9999) @redis.ltrim("rules_fired:#{rule_name}", 0, 999) end |
#rule_firings(rule_name = nil, limit: 100) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 82 def rule_firings(rule_name = nil, limit: 100) key = rule_name ? "rules_fired:#{rule_name}" : 'rules_fired:all' entries_json = @redis.lrange(key, 0, limit - 1) entries_json.map do |entry_json| entry = JSON.parse(entry_json, symbolize_names: true) { rule_name: entry[:rule_name], fact_uuids: JSON.parse(entry[:fact_uuids]), bindings: JSON.parse(entry[:bindings], symbolize_names: true), fired_at: Time.at(entry[:fired_at]), session_id: entry[:session_id] } end end |
#stats ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/kbs/blackboard/redis_audit_log.rb', line 98 def stats rules_fired_count = @redis.llen('rules_fired:all') { rules_fired: rules_fired_count } end |