-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
433 lines (370 loc) · 16.1 KB
/
database.py
File metadata and controls
433 lines (370 loc) · 16.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
"""
ReconRaven Database Module - SIMPLIFIED FLAT SCHEMA
All data in minimal tables, complexity moved to frontend
"""
import sqlite3
import json
import os
from datetime import datetime
from typing import Dict, List, Optional, Any
class ReconRavenDB:
"""Simplified SQLite database for ReconRaven"""
def __init__(self, db_path='reconraven.db'):
"""Initialize database connection"""
self.db_path = db_path
self.conn = sqlite3.connect(db_path, check_same_thread=False)
self.conn.row_factory = sqlite3.Row
self._create_tables()
def _create_tables(self):
"""Create simplified flat schema"""
cursor = self.conn.cursor()
# SINGLE FLAT TABLE for all detected signals
cursor.execute('''
CREATE TABLE IF NOT EXISTS signals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
frequency_hz REAL NOT NULL,
band TEXT NOT NULL,
power_dbm REAL NOT NULL,
-- Baseline comparison
baseline_power_dbm REAL,
delta_db REAL,
is_anomaly BOOLEAN DEFAULT 1,
is_baseline BOOLEAN DEFAULT 0,
-- Recording info (if recorded)
recording_file TEXT,
-- Device identification (if identified)
device_name TEXT,
device_type TEXT,
manufacturer TEXT,
-- Analysis results (if analyzed)
modulation TEXT,
bit_rate INTEGER,
confidence REAL,
analysis_data TEXT,
-- Timestamps
detected_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Simple baseline table (just freq + power averages)
cursor.execute('''
CREATE TABLE IF NOT EXISTS baseline (
id INTEGER PRIMARY KEY AUTOINCREMENT,
frequency_hz REAL NOT NULL UNIQUE,
band TEXT NOT NULL,
power_dbm REAL NOT NULL,
std_dbm REAL DEFAULT 0,
sample_count INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Simple recordings table (just metadata)
cursor.execute('''
CREATE TABLE IF NOT EXISTS recordings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL UNIQUE,
frequency_hz REAL NOT NULL,
band TEXT,
signal_id INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# DF Array Calibration table
cursor.execute('''
CREATE TABLE IF NOT EXISTS df_calibration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
num_sdrs INTEGER NOT NULL,
calibration_freq_hz REAL NOT NULL,
reference_sdr INTEGER DEFAULT 0,
-- Phase offsets for each SDR (JSON array)
phase_offsets TEXT NOT NULL,
-- Array geometry (JSON)
array_geometry TEXT,
antenna_type TEXT DEFAULT 'omnidirectional',
element_spacing_m REAL DEFAULT 0.5,
-- Calibration quality metrics
coherence_score REAL,
snr_db REAL,
-- Metadata
calibration_method TEXT,
notes TEXT,
is_active BOOLEAN DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Create indexes
cursor.execute('CREATE INDEX IF NOT EXISTS idx_signals_freq ON signals(frequency_hz)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_signals_anomaly ON signals(is_anomaly)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_signals_time ON signals(detected_at)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_baseline_freq ON baseline(frequency_hz)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_df_cal_active ON df_calibration(is_active)')
self.conn.commit()
# ========== BASELINE MANAGEMENT (SIMPLE) ==========
def add_baseline_frequency(self, freq: float, band: str, power: float, std: float = 0):
"""Add or update baseline frequency"""
cursor = self.conn.cursor()
cursor.execute('''
INSERT INTO baseline (frequency_hz, band, power_dbm, std_dbm, sample_count)
VALUES (?, ?, ?, ?, 1)
ON CONFLICT(frequency_hz) DO UPDATE SET
power_dbm = ((power_dbm * sample_count) + ?) / (sample_count + 1),
std_dbm = ?,
sample_count = sample_count + 1
''', (freq, band, power, std, power, std))
self.conn.commit()
def get_baseline(self, freq: float = None) -> Optional[Dict]:
"""Get baseline for a frequency, or all baselines if freq is None"""
cursor = self.conn.cursor()
if freq is not None:
cursor.execute('SELECT * FROM baseline WHERE frequency_hz = ?', (freq,))
row = cursor.fetchone()
return dict(row) if row else None
else:
# Return all baselines (for loading)
cursor.execute('SELECT * FROM baseline ORDER BY frequency_hz')
return [dict(row) for row in cursor.fetchall()]
def get_all_baseline(self) -> List[Dict]:
"""Get all baseline frequencies"""
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM baseline ORDER BY frequency_hz')
return [dict(row) for row in cursor.fetchall()]
# ========== SIGNAL MANAGEMENT (SIMPLE) ==========
def add_signal(self, freq: float, band: str, power: float,
baseline_power: float = None, is_anomaly: bool = True,
recording_file: str = None, **kwargs) -> int:
"""Add a detected signal (flat, simple insert)"""
cursor = self.conn.cursor()
# Calculate delta if we have baseline
delta = None
if baseline_power is not None:
delta = power - baseline_power
cursor.execute('''
INSERT INTO signals (
frequency_hz, band, power_dbm, baseline_power_dbm, delta_db,
is_anomaly, recording_file, device_name, device_type,
manufacturer, modulation, bit_rate, confidence
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
freq, band, power, baseline_power, delta, is_anomaly, recording_file,
kwargs.get('device_name'), kwargs.get('device_type'),
kwargs.get('manufacturer'), kwargs.get('modulation'),
kwargs.get('bit_rate'), kwargs.get('confidence')
))
self.conn.commit()
return cursor.lastrowid
def get_all_signals(self, limit: int = 1000) -> List[Dict]:
"""Get ALL signals (let frontend filter)"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM signals
ORDER BY detected_at DESC
LIMIT ?
''', (limit,))
return [dict(row) for row in cursor.fetchall()]
def get_anomalies(self, limit: int = 100) -> List[Dict]:
"""Get signals marked as anomalies"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM signals
WHERE is_anomaly = 1 AND is_baseline = 0
ORDER BY detected_at DESC
LIMIT ?
''', (limit,))
return [dict(row) for row in cursor.fetchall()]
def get_identified_signals(self, limit: int = 100) -> List[Dict]:
"""Get signals that have been identified (have device info)"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM signals
WHERE device_name IS NOT NULL
ORDER BY detected_at DESC
LIMIT ?
''', (limit,))
return [dict(row) for row in cursor.fetchall()]
def update_signal_device(self, signal_id: int, device_name: str,
device_type: str = None, manufacturer: str = None):
"""Add device identification to a signal"""
cursor = self.conn.cursor()
cursor.execute('''
UPDATE signals
SET device_name = ?, device_type = ?, manufacturer = ?
WHERE id = ?
''', (device_name, device_type, manufacturer, signal_id))
self.conn.commit()
def update_signal_analysis(self, signal_id: int, modulation: str = None,
bit_rate: int = None, confidence: float = None,
analysis_data: str = None):
"""Add analysis results to a signal"""
cursor = self.conn.cursor()
cursor.execute('''
UPDATE signals
SET modulation = ?, bit_rate = ?, confidence = ?, analysis_data = ?
WHERE id = ?
''', (modulation, bit_rate, confidence, analysis_data, signal_id))
self.conn.commit()
def promote_to_baseline(self, freq: float):
"""Mark frequency as baseline (suppress future anomalies)"""
cursor = self.conn.cursor()
# Mark all signals at this frequency as baseline
cursor.execute('''
UPDATE signals
SET is_baseline = 1, is_anomaly = 0
WHERE frequency_hz = ?
''', (freq,))
self.conn.commit()
# ========== DF CALIBRATION MANAGEMENT ==========
def save_df_calibration(self, num_sdrs: int, calibration_freq_hz: float,
phase_offsets: List[float], array_geometry: Dict = None,
antenna_type: str = 'omnidirectional',
element_spacing_m: float = 0.5,
coherence_score: float = None, snr_db: float = None,
calibration_method: str = None, notes: str = None):
"""Save DF array calibration data"""
cursor = self.conn.cursor()
# Mark all previous calibrations as inactive
cursor.execute('UPDATE df_calibration SET is_active = 0')
# Insert new calibration
cursor.execute('''
INSERT INTO df_calibration
(num_sdrs, calibration_freq_hz, phase_offsets, array_geometry,
antenna_type, element_spacing_m, coherence_score, snr_db,
calibration_method, notes, is_active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
''', (
num_sdrs,
calibration_freq_hz,
json.dumps(phase_offsets),
json.dumps(array_geometry) if array_geometry else None,
antenna_type,
element_spacing_m,
coherence_score,
snr_db,
calibration_method,
notes
))
self.conn.commit()
return cursor.lastrowid
def get_active_df_calibration(self) -> Optional[Dict]:
"""Get currently active DF calibration"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM df_calibration
WHERE is_active = 1
ORDER BY created_at DESC
LIMIT 1
''')
row = cursor.fetchone()
if row:
cal = dict(row)
cal['phase_offsets'] = json.loads(cal['phase_offsets'])
if cal['array_geometry']:
cal['array_geometry'] = json.loads(cal['array_geometry'])
return cal
return None
def get_df_calibration_history(self, limit: int = 10) -> List[Dict]:
"""Get DF calibration history"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM df_calibration
ORDER BY created_at DESC
LIMIT ?
''', (limit,))
calibrations = []
for row in cursor.fetchall():
cal = dict(row)
cal['phase_offsets'] = json.loads(cal['phase_offsets'])
if cal['array_geometry']:
cal['array_geometry'] = json.loads(cal['array_geometry'])
calibrations.append(cal)
return calibrations
# ========== RECORDINGS (SIMPLE) ==========
def add_recording(self, filename: str, freq: float, band: str, signal_id: int = None):
"""Add recording metadata"""
cursor = self.conn.cursor()
try:
cursor.execute('''
INSERT INTO recordings (filename, frequency_hz, band, signal_id)
VALUES (?, ?, ?, ?)
''', (filename, freq, band, signal_id))
self.conn.commit()
return cursor.lastrowid
except sqlite3.IntegrityError:
# Recording already exists
return None
def get_recordings(self, limit: int = 100) -> List[Dict]:
"""Get all recordings"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT * FROM recordings
ORDER BY created_at DESC
LIMIT ?
''', (limit,))
return [dict(row) for row in cursor.fetchall()]
# ========== STATISTICS (SIMPLE COUNTS) ==========
def get_statistics(self) -> Dict[str, int]:
"""Get simple statistics"""
cursor = self.conn.cursor()
stats = {}
# Baseline count
cursor.execute('SELECT COUNT(*) as count FROM baseline')
stats['baseline_frequencies'] = cursor.fetchone()['count']
# Total signals
cursor.execute('SELECT COUNT(*) as count FROM signals')
stats['total_signals'] = cursor.fetchone()['count']
# Anomalies (not promoted to baseline)
cursor.execute('SELECT COUNT(*) as count FROM signals WHERE is_anomaly = 1 AND is_baseline = 0')
stats['anomalies'] = cursor.fetchone()['count']
# Identified devices
cursor.execute('SELECT COUNT(DISTINCT device_name) as count FROM signals WHERE device_name IS NOT NULL')
stats['identified_devices'] = cursor.fetchone()['count']
# Recordings
cursor.execute('SELECT COUNT(*) as count FROM recordings')
stats['total_recordings'] = cursor.fetchone()['count']
return stats
# ========== LEGACY COMPATIBILITY ==========
def get_devices(self) -> List[Dict]:
"""Get unique identified devices (for backwards compat)"""
cursor = self.conn.cursor()
cursor.execute('''
SELECT
frequency_hz,
device_name as name,
device_type,
manufacturer,
modulation,
bit_rate,
confidence,
MAX(detected_at) as last_seen,
COUNT(*) as detection_count
FROM signals
WHERE device_name IS NOT NULL
GROUP BY frequency_hz, device_name
ORDER BY MAX(detected_at) DESC
''')
return [dict(row) for row in cursor.fetchall()]
def get_all_transcripts(self) -> List[Dict]:
"""Get all transcripts (stub for compatibility)"""
# Transcripts not supported in simplified schema yet
return []
def clear_anomalies(self):
"""Clear all anomaly signals (for fresh start)"""
cursor = self.conn.cursor()
cursor.execute('DELETE FROM signals WHERE is_anomaly = 1')
self.conn.commit()
def clear_all_data(self):
"""Nuclear option - clear everything except baseline"""
cursor = self.conn.cursor()
cursor.execute('DELETE FROM signals')
cursor.execute('DELETE FROM recordings')
self.conn.commit()
def close(self):
"""Close database connection"""
self.conn.close()
# Singleton pattern for easy access
_db_instance = None
def get_db(db_path='reconraven.db') -> ReconRavenDB:
"""Get database singleton"""
global _db_instance
if _db_instance is None:
_db_instance = ReconRavenDB(db_path)
return _db_instance