Class: KBS::Blackboard::AuditLog
- Inherits:
-
Object
- Object
- KBS::Blackboard::AuditLog
- Defined in:
- lib/kbs/blackboard/audit_log.rb
Instance Method Summary collapse
- #fact_history(fact_uuid = nil, limit: 100) ⇒ Object
-
#initialize(db, session_id) ⇒ AuditLog
constructor
A new instance of AuditLog.
- #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
- #setup_tables ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(db, session_id) ⇒ AuditLog
9 10 11 12 13 |
# File 'lib/kbs/blackboard/audit_log.rb', line 9 def initialize(db, session_id) @db = db @session_id = session_id setup_tables end |
Instance Method Details
#fact_history(fact_uuid = nil, limit: 100) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kbs/blackboard/audit_log.rb', line 59 def fact_history(fact_uuid = nil, limit: 100) if fact_uuid results = @db.execute( "SELECT * FROM fact_history WHERE fact_uuid = ? ORDER BY timestamp DESC, id DESC LIMIT ?", [fact_uuid, limit] ) else results = @db.execute( "SELECT * FROM fact_history ORDER BY timestamp DESC, id DESC LIMIT ?", [limit] ) end results.map do |row| { fact_uuid: row['fact_uuid'], fact_type: row['fact_type'].to_sym, attributes: JSON.parse(row['attributes'], symbolize_names: true), action: row['action'], timestamp: Time.parse(row['timestamp']), session_id: row['session_id'] } end end |
#log_fact_change(fact_uuid, fact_type, attributes, action) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/kbs/blackboard/audit_log.rb', line 43 def log_fact_change(fact_uuid, fact_type, attributes, action) attributes_json = attributes.is_a?(String) ? attributes : JSON.generate(attributes) @db.execute( "INSERT INTO fact_history (fact_uuid, fact_type, attributes, action, session_id) VALUES (?, ?, ?, ?, ?)", [fact_uuid, fact_type.to_s, attributes_json, action, @session_id] ) end |
#log_rule_firing(rule_name, fact_uuids, bindings = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/kbs/blackboard/audit_log.rb', line 52 def log_rule_firing(rule_name, fact_uuids, bindings = {}) @db.execute( "INSERT INTO rules_fired (rule_name, fact_uuids, bindings, session_id) VALUES (?, ?, ?, ?)", [rule_name, JSON.generate(fact_uuids), JSON.generate(bindings), @session_id] ) end |
#rule_firings(rule_name = nil, limit: 100) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/kbs/blackboard/audit_log.rb', line 84 def rule_firings(rule_name = nil, limit: 100) if rule_name results = @db.execute( "SELECT * FROM rules_fired WHERE rule_name = ? ORDER BY fired_at DESC LIMIT ?", [rule_name, limit] ) else results = @db.execute( "SELECT * FROM rules_fired ORDER BY fired_at DESC LIMIT ?", [limit] ) end results.map do |row| { rule_name: row['rule_name'], fact_uuids: JSON.parse(row['fact_uuids']), bindings: row['bindings'] ? JSON.parse(row['bindings'], symbolize_names: true) : {}, fired_at: Time.parse(row['fired_at']), session_id: row['session_id'] } end end |
#setup_tables ⇒ 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 40 41 |
# File 'lib/kbs/blackboard/audit_log.rb', line 15 def setup_tables @db.execute_batch " CREATE TABLE IF NOT EXISTS fact_history (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n fact_uuid TEXT NOT NULL,\n fact_type TEXT NOT NULL,\n attributes TEXT NOT NULL,\n action TEXT NOT NULL,\n timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n session_id TEXT\n );\n\n CREATE TABLE IF NOT EXISTS rules_fired (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n rule_name TEXT NOT NULL,\n fact_uuids TEXT NOT NULL,\n bindings TEXT,\n fired_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n session_id TEXT\n );\n SQL\n\n @db.execute_batch <<-SQL\n CREATE INDEX IF NOT EXISTS idx_fact_history_uuid ON fact_history(fact_uuid);\n CREATE INDEX IF NOT EXISTS idx_rules_fired_session ON rules_fired(session_id);\n SQL\nend\n" |
#stats ⇒ Object
108 109 110 111 112 |
# File 'lib/kbs/blackboard/audit_log.rb', line 108 def stats { rules_fired: @db.get_first_value("SELECT COUNT(*) FROM rules_fired") } end |