whodunit-chronicles 0.1.0.pre → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +50 -0
- data/.rubocop.yml +2 -1
- data/.yardopts +7 -5
- data/CHANGELOG.md +76 -1
- data/README.md +408 -22
- data/examples/images/campaign-performance-analytics.png +0 -0
- data/examples/images/candidate-journey-analytics.png +0 -0
- data/examples/images/recruitment-funnel-analytics.png +0 -0
- data/lib/whodunit/chronicles/adapters/mysql.rb +261 -0
- data/lib/whodunit/chronicles/configuration.rb +23 -12
- data/lib/whodunit/chronicles/connection.rb +88 -0
- data/lib/whodunit/chronicles/persistence.rb +129 -0
- data/lib/whodunit/chronicles/processor.rb +127 -0
- data/lib/whodunit/chronicles/service.rb +23 -21
- data/lib/whodunit/chronicles/table.rb +120 -0
- data/lib/whodunit/chronicles/version.rb +1 -1
- data/lib/whodunit/chronicles.rb +11 -1
- data/whodunit-chronicles.gemspec +6 -2
- metadata +68 -4
- data/lib/whodunit/chronicles/audit_processor.rb +0 -270
@@ -1,270 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Whodunit
|
4
|
-
module Chronicles
|
5
|
-
# Processes database change events and creates audit records
|
6
|
-
#
|
7
|
-
# Transforms ChangeEvent objects into structured audit records
|
8
|
-
# with complete object serialization and metadata.
|
9
|
-
class AuditProcessor
|
10
|
-
attr_reader :logger, :audit_connection
|
11
|
-
|
12
|
-
def initialize(
|
13
|
-
audit_database_url: Chronicles.config.audit_database_url,
|
14
|
-
logger: Chronicles.logger
|
15
|
-
)
|
16
|
-
@audit_database_url = audit_database_url
|
17
|
-
@logger = logger
|
18
|
-
@audit_connection = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
# Process a change event and create audit record
|
22
|
-
#
|
23
|
-
# @param change_event [ChangeEvent] The database change to audit
|
24
|
-
# @return [Hash] The created audit record
|
25
|
-
def process(change_event)
|
26
|
-
ensure_audit_connection
|
27
|
-
|
28
|
-
audit_record = build_audit_record(change_event)
|
29
|
-
persist_audit_record(audit_record)
|
30
|
-
|
31
|
-
log(:debug, 'Processed change event',
|
32
|
-
table: change_event.qualified_table_name,
|
33
|
-
action: change_event.action,
|
34
|
-
audit_id: audit_record[:id])
|
35
|
-
|
36
|
-
audit_record
|
37
|
-
rescue StandardError => e
|
38
|
-
log(:error, 'Failed to process change event',
|
39
|
-
error: e.message,
|
40
|
-
event: change_event.to_s)
|
41
|
-
raise
|
42
|
-
end
|
43
|
-
|
44
|
-
# Process multiple change events in a batch
|
45
|
-
#
|
46
|
-
# @param change_events [Array<ChangeEvent>] Array of change events
|
47
|
-
# @return [Array<Hash>] Array of created audit records
|
48
|
-
def process_batch(change_events)
|
49
|
-
return [] if change_events.empty?
|
50
|
-
|
51
|
-
ensure_audit_connection
|
52
|
-
|
53
|
-
audit_records = change_events.map { |event| build_audit_record(event) }
|
54
|
-
persist_audit_records_batch(audit_records)
|
55
|
-
|
56
|
-
log(:info, 'Processed batch of change events', count: change_events.size)
|
57
|
-
|
58
|
-
audit_records
|
59
|
-
rescue StandardError => e
|
60
|
-
log(:error, 'Failed to process batch',
|
61
|
-
error: e.message,
|
62
|
-
count: change_events.size)
|
63
|
-
raise
|
64
|
-
end
|
65
|
-
|
66
|
-
# Close audit database connection
|
67
|
-
def close
|
68
|
-
@audit_connection&.close
|
69
|
-
@audit_connection = nil
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
def ensure_audit_connection
|
75
|
-
return if @audit_connection && !@audit_connection.finished?
|
76
|
-
|
77
|
-
@audit_connection = PG.connect(@audit_database_url || Chronicles.config.database_url)
|
78
|
-
@audit_connection.type_map_for_results = PG::BasicTypeMapForResults.new(@audit_connection)
|
79
|
-
|
80
|
-
ensure_audit_table_exists
|
81
|
-
end
|
82
|
-
|
83
|
-
def ensure_audit_table_exists
|
84
|
-
create_sql = <<~SQL
|
85
|
-
CREATE TABLE IF NOT EXISTS whodunit_chronicles_audits (
|
86
|
-
id BIGSERIAL PRIMARY KEY,
|
87
|
-
table_name TEXT NOT NULL,
|
88
|
-
schema_name TEXT NOT NULL DEFAULT 'public',
|
89
|
-
record_id JSONB,
|
90
|
-
action TEXT NOT NULL CHECK (action IN ('INSERT', 'UPDATE', 'DELETE')),
|
91
|
-
old_data JSONB,
|
92
|
-
new_data JSONB,
|
93
|
-
changes JSONB,
|
94
|
-
user_id BIGINT,
|
95
|
-
user_type TEXT,
|
96
|
-
transaction_id TEXT,
|
97
|
-
sequence_number INTEGER,
|
98
|
-
occurred_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
99
|
-
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
100
|
-
metadata JSONB DEFAULT '{}'::jsonb,
|
101
|
-
#{' '}
|
102
|
-
-- Indexes for performance
|
103
|
-
CONSTRAINT valid_data_for_action CHECK (
|
104
|
-
(action = 'INSERT' AND old_data IS NULL AND new_data IS NOT NULL) OR
|
105
|
-
(action = 'UPDATE' AND old_data IS NOT NULL AND new_data IS NOT NULL) OR#{' '}
|
106
|
-
(action = 'DELETE' AND old_data IS NOT NULL AND new_data IS NULL)
|
107
|
-
)
|
108
|
-
);
|
109
|
-
|
110
|
-
-- Performance indexes
|
111
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_table_record#{' '}
|
112
|
-
ON whodunit_chronicles_audits (table_name, (record_id->>'id'));
|
113
|
-
|
114
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_occurred_at#{' '}
|
115
|
-
ON whodunit_chronicles_audits (occurred_at DESC);
|
116
|
-
|
117
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_user#{' '}
|
118
|
-
ON whodunit_chronicles_audits (user_id, user_type);
|
119
|
-
|
120
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_action#{' '}
|
121
|
-
ON whodunit_chronicles_audits (action);
|
122
|
-
|
123
|
-
-- GIN index for JSONB columns
|
124
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_record_id_gin#{' '}
|
125
|
-
ON whodunit_chronicles_audits USING GIN (record_id);
|
126
|
-
|
127
|
-
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chronicles_audits_changes_gin#{' '}
|
128
|
-
ON whodunit_chronicles_audits USING GIN (changes);
|
129
|
-
SQL
|
130
|
-
|
131
|
-
@audit_connection.exec(create_sql)
|
132
|
-
rescue PG::Error => e
|
133
|
-
# Ignore "already exists" errors from CONCURRENTLY
|
134
|
-
raise unless e.message.include?('already exists')
|
135
|
-
end
|
136
|
-
|
137
|
-
def build_audit_record(change_event)
|
138
|
-
user_info = extract_user_info(change_event)
|
139
|
-
|
140
|
-
{
|
141
|
-
id: nil, # Will be set by database
|
142
|
-
table_name: change_event.table_name,
|
143
|
-
schema_name: change_event.schema_name,
|
144
|
-
record_id: change_event.primary_key,
|
145
|
-
action: change_event.action,
|
146
|
-
old_data: change_event.old_data,
|
147
|
-
new_data: change_event.new_data,
|
148
|
-
changes: change_event.changes,
|
149
|
-
user_id: user_info[:user_id],
|
150
|
-
user_type: user_info[:user_type],
|
151
|
-
transaction_id: change_event.transaction_id,
|
152
|
-
sequence_number: change_event.sequence_number,
|
153
|
-
occurred_at: change_event.timestamp,
|
154
|
-
created_at: Time.now,
|
155
|
-
metadata: build_metadata(change_event),
|
156
|
-
}
|
157
|
-
end
|
158
|
-
|
159
|
-
def extract_user_info(change_event)
|
160
|
-
data = change_event.current_data || {}
|
161
|
-
|
162
|
-
# Look for Whodunit user attribution fields
|
163
|
-
user_id = data['creator_id'] || data['updater_id'] || data['deleter_id']
|
164
|
-
|
165
|
-
{
|
166
|
-
user_id: user_id,
|
167
|
-
user_type: user_id ? 'User' : nil,
|
168
|
-
}
|
169
|
-
end
|
170
|
-
|
171
|
-
def build_metadata(change_event)
|
172
|
-
{
|
173
|
-
table_schema: change_event.schema_name,
|
174
|
-
qualified_table_name: change_event.qualified_table_name,
|
175
|
-
changed_columns: change_event.changed_columns,
|
176
|
-
adapter_metadata: change_event.metadata,
|
177
|
-
chronicles_version: Chronicles::VERSION,
|
178
|
-
}
|
179
|
-
end
|
180
|
-
|
181
|
-
def persist_audit_record(audit_record)
|
182
|
-
sql = <<~SQL
|
183
|
-
INSERT INTO whodunit_chronicles_audits (
|
184
|
-
table_name, schema_name, record_id, action, old_data, new_data, changes,
|
185
|
-
user_id, user_type, transaction_id, sequence_number, occurred_at, created_at, metadata
|
186
|
-
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
187
|
-
RETURNING id
|
188
|
-
SQL
|
189
|
-
|
190
|
-
params = [
|
191
|
-
audit_record[:table_name],
|
192
|
-
audit_record[:schema_name],
|
193
|
-
audit_record[:record_id].to_json,
|
194
|
-
audit_record[:action],
|
195
|
-
audit_record[:old_data]&.to_json,
|
196
|
-
audit_record[:new_data]&.to_json,
|
197
|
-
audit_record[:changes].to_json,
|
198
|
-
audit_record[:user_id],
|
199
|
-
audit_record[:user_type],
|
200
|
-
audit_record[:transaction_id],
|
201
|
-
audit_record[:sequence_number],
|
202
|
-
audit_record[:occurred_at],
|
203
|
-
audit_record[:created_at],
|
204
|
-
audit_record[:metadata].to_json,
|
205
|
-
]
|
206
|
-
|
207
|
-
result = @audit_connection.exec_params(sql, params)
|
208
|
-
audit_record[:id] = result.first['id'].to_i
|
209
|
-
result.clear
|
210
|
-
|
211
|
-
audit_record
|
212
|
-
end
|
213
|
-
|
214
|
-
def persist_audit_records_batch(audit_records)
|
215
|
-
return audit_records if audit_records.empty?
|
216
|
-
|
217
|
-
# Use multi-row INSERT for better performance
|
218
|
-
values_clauses = []
|
219
|
-
all_params = []
|
220
|
-
param_index = 1
|
221
|
-
|
222
|
-
audit_records.each do |record|
|
223
|
-
param_positions = (param_index..(param_index + 13)).map { |i| "$#{i}" }.join(', ')
|
224
|
-
values_clauses << "(#{param_positions})"
|
225
|
-
|
226
|
-
all_params.push(
|
227
|
-
record[:table_name],
|
228
|
-
record[:schema_name],
|
229
|
-
record[:record_id].to_json,
|
230
|
-
record[:action],
|
231
|
-
record[:old_data]&.to_json,
|
232
|
-
record[:new_data]&.to_json,
|
233
|
-
record[:changes].to_json,
|
234
|
-
record[:user_id],
|
235
|
-
record[:user_type],
|
236
|
-
record[:transaction_id],
|
237
|
-
record[:sequence_number],
|
238
|
-
record[:occurred_at],
|
239
|
-
record[:created_at],
|
240
|
-
record[:metadata].to_json,
|
241
|
-
)
|
242
|
-
|
243
|
-
param_index += 14
|
244
|
-
end
|
245
|
-
|
246
|
-
sql = <<~SQL
|
247
|
-
INSERT INTO whodunit_chronicles_audits (
|
248
|
-
table_name, schema_name, record_id, action, old_data, new_data, changes,
|
249
|
-
user_id, user_type, transaction_id, sequence_number, occurred_at, created_at, metadata
|
250
|
-
) VALUES #{values_clauses.join(', ')}
|
251
|
-
RETURNING id
|
252
|
-
SQL
|
253
|
-
|
254
|
-
result = @audit_connection.exec_params(sql, all_params)
|
255
|
-
|
256
|
-
# Set IDs on the audit records
|
257
|
-
result.each_with_index do |row, index|
|
258
|
-
audit_records[index][:id] = row['id'].to_i
|
259
|
-
end
|
260
|
-
|
261
|
-
result.clear
|
262
|
-
audit_records
|
263
|
-
end
|
264
|
-
|
265
|
-
def log(level, message, context = {})
|
266
|
-
logger.public_send(level, message, processor: 'AuditProcessor', **context)
|
267
|
-
end
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|