forked from terdia/mqttui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
462 lines (383 loc) · 16.6 KB
/
database.py
File metadata and controls
462 lines (383 loc) · 16.6 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""
Database module for MQTT message persistence
"""
import sqlite3
import threading
import logging
import re
import json
from datetime import datetime, timedelta
from typing import List, Dict, Optional, Union
import os
class MessageDatabase:
def __init__(self, db_path: str = "mqtt_messages.db", max_messages: int = 10000):
self.db_path = db_path
self.max_messages = max_messages
self._local = threading.local()
self.init_database()
def get_connection(self):
"""Get thread-local database connection"""
if not hasattr(self._local, 'connection'):
self._local.connection = sqlite3.connect(
self.db_path,
check_same_thread=False,
timeout=30.0
)
self._local.connection.row_factory = sqlite3.Row
# Add REGEXP function for advanced topic filtering
self._local.connection.create_function("REGEXP", 2, self._regexp)
return self._local.connection
def _regexp(self, pattern, value):
"""Custom REGEXP function for SQLite"""
try:
return re.search(pattern, value, re.IGNORECASE) is not None
except Exception:
return False
def init_database(self):
"""Initialize database schema"""
conn = self.get_connection()
try:
cursor = conn.cursor()
# Messages table
cursor.execute('''
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT NOT NULL,
payload TEXT NOT NULL,
timestamp DATETIME NOT NULL,
qos INTEGER DEFAULT 0,
retain BOOLEAN DEFAULT 0,
payload_size INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Topics table for faster topic queries
cursor.execute('''
CREATE TABLE IF NOT EXISTS topics (
topic TEXT PRIMARY KEY,
last_message_at DATETIME NOT NULL,
message_count INTEGER DEFAULT 1,
first_seen DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Filter presets table for saved searches
cursor.execute('''
CREATE TABLE IF NOT EXISTS filter_presets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
description TEXT,
filters TEXT NOT NULL, -- JSON string of filter parameters
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_used DATETIME
)
''')
# Create indexes for better performance
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_messages_topic
ON messages(topic)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_messages_timestamp
ON messages(timestamp DESC)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_messages_topic_timestamp
ON messages(topic, timestamp DESC)
''')
conn.commit()
logging.info("Database initialized successfully")
except Exception as e:
logging.error(f"Database initialization error: {e}")
conn.rollback()
raise
def store_message(self, topic: str, payload: str, timestamp: datetime = None,
qos: int = 0, retain: bool = False) -> bool:
"""Store a message in the database"""
if timestamp is None:
timestamp = datetime.now()
conn = self.get_connection()
try:
cursor = conn.cursor()
# Store message
cursor.execute('''
INSERT INTO messages (topic, payload, timestamp, qos, retain, payload_size)
VALUES (?, ?, ?, ?, ?, ?)
''', (topic, payload, timestamp, qos, retain, len(payload)))
# Update topics table
cursor.execute('''
INSERT OR REPLACE INTO topics (topic, last_message_at, message_count, first_seen)
VALUES (?, ?,
COALESCE((SELECT message_count FROM topics WHERE topic = ?) + 1, 1),
COALESCE((SELECT first_seen FROM topics WHERE topic = ?), ?)
)
''', (topic, timestamp, topic, topic, timestamp))
conn.commit()
# Clean up old messages if needed
self._cleanup_old_messages()
return True
except Exception as e:
logging.error(f"Error storing message: {e}")
conn.rollback()
return False
def get_messages(self, limit: int = 100, offset: int = 0,
topic_filter: str = None, since: datetime = None,
content_search: str = None, regex_topic: str = None,
json_path: str = None, json_value: str = None) -> List[Dict]:
"""Retrieve messages from database with enhanced filtering"""
conn = self.get_connection()
try:
cursor = conn.cursor()
query = '''
SELECT topic, payload, timestamp, qos, retain, payload_size
FROM messages
WHERE 1=1
'''
params = []
# Basic topic filtering (wildcard support)
if topic_filter:
if '%' in topic_filter or '_' in topic_filter:
query += ' AND topic LIKE ?'
else:
query += ' AND topic = ?'
params.append(topic_filter)
# Regex topic filtering (more powerful than LIKE)
if regex_topic:
query += ' AND topic REGEXP ?'
params.append(regex_topic)
# Content search (case-insensitive)
if content_search:
query += ' AND LOWER(payload) LIKE LOWER(?)'
params.append(f'%{content_search}%')
# Time filtering
if since:
query += ' AND timestamp >= ?'
params.append(since)
query += ' ORDER BY timestamp DESC LIMIT ? OFFSET ?'
params.extend([limit, offset])
cursor.execute(query, params)
rows = cursor.fetchall()
messages = [dict(row) for row in rows]
# Apply Python-based filters that can't be done in SQL
if json_path or regex_topic:
messages = self._apply_python_filters(
messages, regex_topic, json_path, json_value
)
return messages
except Exception as e:
logging.error(f"Error retrieving messages: {e}")
return []
def _apply_python_filters(self, messages: List[Dict], regex_topic: str = None,
json_path: str = None, json_value: str = None) -> List[Dict]:
"""Apply filters that require Python processing"""
filtered = []
for msg in messages:
# Regex topic filter (if not handled by SQL REGEXP)
if regex_topic and not re.search(regex_topic, msg['topic'], re.IGNORECASE):
continue
# JSON path filtering
if json_path and json_value:
try:
payload_json = json.loads(msg['payload'])
# Simple JSON path support (e.g., "temperature", "sensors.temp")
value = self._get_json_path_value(payload_json, json_path)
if value is None or str(value).lower() != json_value.lower():
continue
except (json.JSONDecodeError, KeyError):
continue
filtered.append(msg)
return filtered
def _get_json_path_value(self, json_obj: dict, path: str):
"""Extract value from JSON using simple dot notation path"""
try:
keys = path.split('.')
current = json_obj
for key in keys:
if isinstance(current, dict) and key in current:
current = current[key]
else:
return None
return current
except Exception:
return None
def get_topics(self) -> List[Dict]:
"""Get all topics with statistics"""
conn = self.get_connection()
try:
cursor = conn.cursor()
cursor.execute('''
SELECT topic, message_count, last_message_at, first_seen
FROM topics
ORDER BY last_message_at DESC
''')
rows = cursor.fetchall()
return [dict(row) for row in rows]
except Exception as e:
logging.error(f"Error retrieving topics: {e}")
return []
def get_message_count(self, topic_filter: str = None, since: datetime = None) -> int:
"""Get total message count with optional filtering"""
conn = self.get_connection()
try:
cursor = conn.cursor()
query = 'SELECT COUNT(*) FROM messages WHERE 1=1'
params = []
if topic_filter:
if '%' in topic_filter or '_' in topic_filter:
query += ' AND topic LIKE ?'
else:
query += ' AND topic = ?'
params.append(topic_filter)
if since:
query += ' AND timestamp >= ?'
params.append(since)
cursor.execute(query, params)
return cursor.fetchone()[0]
except Exception as e:
logging.error(f"Error counting messages: {e}")
return 0
def _cleanup_old_messages(self):
"""Remove old messages to stay within limit"""
conn = self.get_connection()
try:
cursor = conn.cursor()
# Check if we need cleanup
cursor.execute('SELECT COUNT(*) FROM messages')
count = cursor.fetchone()[0]
if count > self.max_messages:
# Delete oldest messages
messages_to_delete = count - self.max_messages + 1000 # Delete extra to avoid frequent cleanups
cursor.execute('''
DELETE FROM messages
WHERE id IN (
SELECT id FROM messages
ORDER BY timestamp ASC
LIMIT ?
)
''', (messages_to_delete,))
# Update topic counts (this is approximate)
cursor.execute('''
UPDATE topics SET message_count = (
SELECT COUNT(*) FROM messages WHERE messages.topic = topics.topic
)
''')
conn.commit()
logging.info(f"Cleaned up {messages_to_delete} old messages")
except Exception as e:
logging.error(f"Error during cleanup: {e}")
conn.rollback()
def cleanup_old_data(self, days: int = 30):
"""Remove messages older than specified days"""
cutoff_date = datetime.now() - timedelta(days=days)
conn = self.get_connection()
try:
cursor = conn.cursor()
cursor.execute('DELETE FROM messages WHERE timestamp < ?', (cutoff_date,))
deleted = cursor.rowcount
# Clean up topics with no messages
cursor.execute('''
DELETE FROM topics WHERE topic NOT IN (
SELECT DISTINCT topic FROM messages
)
''')
conn.commit()
logging.info(f"Cleaned up {deleted} messages older than {days} days")
return deleted
except Exception as e:
logging.error(f"Error during data cleanup: {e}")
conn.rollback()
return 0
def get_database_size(self) -> Dict:
"""Get database size information"""
try:
size_bytes = os.path.getsize(self.db_path) if os.path.exists(self.db_path) else 0
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM messages')
message_count = cursor.fetchone()[0]
cursor.execute('SELECT COUNT(*) FROM topics')
topic_count = cursor.fetchone()[0]
return {
'size_bytes': size_bytes,
'size_mb': round(size_bytes / 1024 / 1024, 2),
'message_count': message_count,
'topic_count': topic_count
}
except Exception as e:
logging.error(f"Error getting database size: {e}")
return {'size_bytes': 0, 'size_mb': 0, 'message_count': 0, 'topic_count': 0}
def save_filter_preset(self, name: str, filters: Dict, description: str = None) -> bool:
"""Save a filter preset"""
conn = self.get_connection()
try:
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO filter_presets (name, description, filters, last_used)
VALUES (?, ?, ?, ?)
''', (name, description, json.dumps(filters), datetime.now()))
conn.commit()
logging.info(f"Saved filter preset: {name}")
return True
except Exception as e:
logging.error(f"Error saving filter preset: {e}")
conn.rollback()
return False
def get_filter_presets(self) -> List[Dict]:
"""Get all filter presets"""
conn = self.get_connection()
try:
cursor = conn.cursor()
cursor.execute('''
SELECT name, description, filters, created_at, last_used
FROM filter_presets
ORDER BY last_used DESC, created_at DESC
''')
rows = cursor.fetchall()
presets = []
for row in rows:
preset = dict(row)
preset['filters'] = json.loads(preset['filters'])
presets.append(preset)
return presets
except Exception as e:
logging.error(f"Error getting filter presets: {e}")
return []
def delete_filter_preset(self, name: str) -> bool:
"""Delete a filter preset"""
conn = self.get_connection()
try:
cursor = conn.cursor()
cursor.execute('DELETE FROM filter_presets WHERE name = ?', (name,))
conn.commit()
if cursor.rowcount > 0:
logging.info(f"Deleted filter preset: {name}")
return True
return False
except Exception as e:
logging.error(f"Error deleting filter preset: {e}")
conn.rollback()
return False
def use_filter_preset(self, name: str) -> Optional[Dict]:
"""Load and mark a filter preset as used"""
conn = self.get_connection()
try:
cursor = conn.cursor()
# Get the preset
cursor.execute('''
SELECT filters FROM filter_presets WHERE name = ?
''', (name,))
row = cursor.fetchone()
if not row:
return None
# Update last_used timestamp
cursor.execute('''
UPDATE filter_presets SET last_used = ? WHERE name = ?
''', (datetime.now(), name))
conn.commit()
return json.loads(row['filters'])
except Exception as e:
logging.error(f"Error using filter preset: {e}")
return None
def close(self):
"""Close database connection"""
if hasattr(self._local, 'connection'):
self._local.connection.close()