-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistence.py
More file actions
330 lines (299 loc) · 10.1 KB
/
persistence.py
File metadata and controls
330 lines (299 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import os
import sqlite3
import threading
from contextlib import contextmanager
DB_LOCK = threading.Lock()
DB_PATH = os.getenv("DATABASE_PATH", os.path.join("data", "app.db"))
def ensure_parent_dir(path):
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
def init_db():
ensure_parent_dir(DB_PATH)
with get_connection() as conn:
conn.executescript(
"""
CREATE TABLE IF NOT EXISTS incidents (
incident_id INTEGER PRIMARY KEY AUTOINCREMENT,
source TEXT NOT NULL,
detected_at TEXT NOT NULL,
stream_started_at TEXT,
active_source_label TEXT,
status TEXT NOT NULL DEFAULT 'detected',
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS evidence_artifacts (
artifact_id INTEGER PRIMARY KEY AUTOINCREMENT,
incident_id INTEGER NOT NULL,
artifact_type TEXT NOT NULL,
file_path TEXT NOT NULL,
metadata_path TEXT NOT NULL,
file_sha256 TEXT NOT NULL,
metadata_sha256 TEXT NOT NULL,
file_size INTEGER NOT NULL,
mime_type TEXT NOT NULL,
storage_uri TEXT NOT NULL,
verify_status TEXT NOT NULL DEFAULT 'verified',
created_at TEXT NOT NULL,
FOREIGN KEY (incident_id) REFERENCES incidents(incident_id)
);
CREATE TABLE IF NOT EXISTS blockchain_anchors (
anchor_id INTEGER PRIMARY KEY AUTOINCREMENT,
incident_id INTEGER NOT NULL,
anchor_mode TEXT NOT NULL,
anchor_status TEXT NOT NULL,
tx_hash TEXT,
block_number INTEGER,
chain_id TEXT,
ledger_path TEXT,
anchor_payload_hash TEXT NOT NULL,
error_message TEXT,
anchored_at TEXT NOT NULL,
FOREIGN KEY (incident_id) REFERENCES incidents(incident_id)
);
CREATE TABLE IF NOT EXISTS audit_events (
audit_id INTEGER PRIMARY KEY AUTOINCREMENT,
incident_id INTEGER NOT NULL,
event_type TEXT NOT NULL,
event_status TEXT NOT NULL,
event_at TEXT NOT NULL,
actor TEXT NOT NULL,
detail_json TEXT NOT NULL,
FOREIGN KEY (incident_id) REFERENCES incidents(incident_id)
);
"""
)
@contextmanager
def get_connection():
with DB_LOCK:
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
try:
yield conn
conn.commit()
finally:
conn.close()
def create_incident(source, detected_at, stream_started_at=None, active_source_label=None, created_at=None):
with get_connection() as conn:
cursor = conn.execute(
"""
INSERT INTO incidents (source, detected_at, stream_started_at, active_source_label, status, created_at)
VALUES (?, ?, ?, ?, 'detected', ?)
""",
(source, detected_at, stream_started_at, active_source_label, created_at or detected_at),
)
return cursor.lastrowid
def add_evidence_artifact(
incident_id,
artifact_type,
file_path,
metadata_path,
file_sha256,
metadata_sha256,
file_size,
mime_type,
storage_uri,
verify_status,
created_at,
):
with get_connection() as conn:
conn.execute(
"""
INSERT INTO evidence_artifacts (
incident_id, artifact_type, file_path, metadata_path, file_sha256,
metadata_sha256, file_size, mime_type, storage_uri, verify_status, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
incident_id,
artifact_type,
file_path,
metadata_path,
file_sha256,
metadata_sha256,
file_size,
mime_type,
storage_uri,
verify_status,
created_at,
),
)
def update_evidence_verify_status(incident_id, verify_status):
with get_connection() as conn:
conn.execute(
"""
UPDATE evidence_artifacts
SET verify_status = ?
WHERE artifact_id = (
SELECT artifact_id FROM evidence_artifacts
WHERE incident_id = ?
ORDER BY artifact_id DESC
LIMIT 1
)
""",
(verify_status, incident_id),
)
def add_anchor_record(
incident_id,
anchor_mode,
anchor_status,
tx_hash,
block_number,
chain_id,
ledger_path,
anchor_payload_hash,
error_message,
anchored_at,
):
with get_connection() as conn:
conn.execute(
"""
INSERT INTO blockchain_anchors (
incident_id, anchor_mode, anchor_status, tx_hash, block_number, chain_id,
ledger_path, anchor_payload_hash, error_message, anchored_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
incident_id,
anchor_mode,
anchor_status,
tx_hash,
block_number,
chain_id,
ledger_path,
anchor_payload_hash,
error_message,
anchored_at,
),
)
def add_audit_event(incident_id, event_type, event_status, event_at, actor, detail_json):
with get_connection() as conn:
conn.execute(
"""
INSERT INTO audit_events (incident_id, event_type, event_status, event_at, actor, detail_json)
VALUES (?, ?, ?, ?, ?, ?)
""",
(incident_id, event_type, event_status, event_at, actor, detail_json),
)
def list_recent_incidents(limit=10):
with get_connection() as conn:
rows = conn.execute(
"""
SELECT
i.incident_id,
i.source,
i.detected_at,
i.stream_started_at,
i.active_source_label,
e.artifact_type,
e.file_path,
e.metadata_path,
e.file_sha256,
e.metadata_sha256,
e.storage_uri,
e.verify_status,
a.anchor_mode,
a.anchor_status,
a.tx_hash,
a.block_number,
a.chain_id,
a.ledger_path,
a.anchor_payload_hash,
a.error_message,
a.anchored_at
FROM incidents i
LEFT JOIN evidence_artifacts e
ON e.artifact_id = (
SELECT artifact_id FROM evidence_artifacts
WHERE incident_id = i.incident_id
ORDER BY artifact_id DESC
LIMIT 1
)
LEFT JOIN blockchain_anchors a
ON a.anchor_id = (
SELECT anchor_id FROM blockchain_anchors
WHERE incident_id = i.incident_id
ORDER BY anchor_id DESC
LIMIT 1
)
ORDER BY i.incident_id DESC
LIMIT ?
""",
(limit,),
).fetchall()
return [dict(row) for row in rows]
def get_incident_detail(incident_id):
with get_connection() as conn:
row = conn.execute(
"""
SELECT
i.incident_id,
i.source,
i.detected_at,
i.stream_started_at,
i.active_source_label,
i.status,
e.artifact_type,
e.file_path,
e.metadata_path,
e.file_sha256,
e.metadata_sha256,
e.file_size,
e.mime_type,
e.storage_uri,
e.verify_status,
a.anchor_mode,
a.anchor_status,
a.tx_hash,
a.block_number,
a.chain_id,
a.ledger_path,
a.anchor_payload_hash,
a.error_message,
a.anchored_at
FROM incidents i
LEFT JOIN evidence_artifacts e
ON e.artifact_id = (
SELECT artifact_id FROM evidence_artifacts
WHERE incident_id = i.incident_id
ORDER BY artifact_id DESC
LIMIT 1
)
LEFT JOIN blockchain_anchors a
ON a.anchor_id = (
SELECT anchor_id FROM blockchain_anchors
WHERE incident_id = i.incident_id
ORDER BY anchor_id DESC
LIMIT 1
)
WHERE i.incident_id = ?
""",
(incident_id,),
).fetchone()
return dict(row) if row else None
def list_audit_events(incident_id):
with get_connection() as conn:
rows = conn.execute(
"""
SELECT audit_id, incident_id, event_type, event_status, event_at, actor, detail_json
FROM audit_events
WHERE incident_id = ?
ORDER BY audit_id ASC
""",
(incident_id,),
).fetchall()
return [dict(row) for row in rows]
def summarize_anchors():
with get_connection() as conn:
rows = conn.execute(
"""
SELECT anchor_status, COUNT(*) AS total
FROM blockchain_anchors
GROUP BY anchor_status
"""
).fetchall()
return {row["anchor_status"]: row["total"] for row in rows}
def count_incidents():
with get_connection() as conn:
row = conn.execute("SELECT COUNT(*) AS total FROM incidents").fetchone()
return int(row["total"]) if row else 0