-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite2json.py
More file actions
506 lines (407 loc) · 18 KB
/
sqlite2json.py
File metadata and controls
506 lines (407 loc) · 18 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import sqlite3
import json
from datetime import datetime, timedelta
from collections import defaultdict
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
data = "data" # data directory
def get_node_info(cursor, node_id):
"""Get shortname and role for a node ID from the nodes table"""
cursor.execute("SELECT shortname, role FROM nodes WHERE id = ?", (node_id,))
result = cursor.fetchone()
if result:
return result[0], result[1] # shortname, role
return f"!{node_id:x}", None # Return hex format if no shortname found, None for role
def export_neighbors_to_json(db_path, json_output_path, time_limit_minutes):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cutoff_time = int((datetime.now() - timedelta(minutes=time_limit_minutes)).timestamp())
cursor.execute(
"""SELECT node_id, neighbor_id, snr, COUNT(*) as count
FROM neighbors
WHERE timestamp >= ?
GROUP BY node_id, neighbor_id""",
(cutoff_time,),
)
rows = cursor.fetchall()
connection_counts = defaultdict(int)
valid_connections = []
for row in rows:
node_id, neighbor_id, snr, count = row
if node_id < 2 or neighbor_id < 2 or node_id > 4294967294 or neighbor_id > 4294967294:
continue
valid_connections.append((node_id, neighbor_id, snr, count))
connection_counts[node_id] += 1
connection_counts[neighbor_id] += 1
cytoscape_data = []
processed_nodes = set()
for node_id, connections in connection_counts.items():
node_hex = f"!{node_id:x}"
node_label, node_role = get_node_info(cursor, node_id)
node_label = node_label if node_label else node_hex
cytoscape_data.append({"data": {"id": node_hex, "label": node_label, "role": node_role, "connections": connections}})
processed_nodes.add(node_hex)
for node_id, neighbor_id, snr, count in valid_connections:
node_hex = f"!{node_id:x}"
neighbor_hex = f"!{neighbor_id:x}"
cytoscape_data.append(
{
"data": {
"id": f"{node_hex}_{neighbor_hex}",
"source": node_hex,
"target": neighbor_hex,
"snr": snr,
"weight": 2 if snr > 0 else 1,
"count": count,
}
}
)
with open(json_output_path, "w") as json_file:
json.dump(cytoscape_data, json_file, indent=2)
print(f"Neighbor data successfully exported to {json_output_path}")
except sqlite3.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if conn:
conn.close()
def export_to_json(db_path, json_output_path, time_limit_minutes, use_physical_sender=False):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
current_time = int(datetime.now().timestamp())
cutoff_time = int((datetime.now() - timedelta(minutes=time_limit_minutes)).timestamp())
# Use physical_sender instead of sender if specified
sender_field = "physical_sender" if use_physical_sender else "sender"
cursor.execute(
f"""SELECT {sender_field}, receiver, COUNT(*) as count, rssi
FROM messages
WHERE timestamp >= ? AND timestamp <= ?
GROUP BY {sender_field}, receiver""",
(cutoff_time, current_time),
)
rows = cursor.fetchall()
connection_counts = defaultdict(int)
valid_connections = []
for row in rows:
sender, receiver, count, rssi = row
if sender < 2 or receiver < 2 or sender > 4294967294 or receiver > 4294967294:
continue
valid_connections.append((sender, receiver, count, rssi))
connection_counts[sender] += 1
connection_counts[receiver] += 1
cytoscape_data = []
processed_nodes = set()
for node_id, connections in connection_counts.items():
node_hex = f"!{node_id:x}"
node_label, node_role = get_node_info(cursor, node_id)
node_label = node_label if node_label else node_hex
cytoscape_data.append({"data": {"id": node_hex, "label": node_label, "role": node_role, "connections": connections}})
processed_nodes.add(node_hex)
for sender, receiver, count, rssi in valid_connections:
sender_hex = f"!{sender:x}"
receiver_hex = f"!{receiver:x}"
cytoscape_data.append(
{
"data": {
"id": f"{sender_hex}_{receiver_hex}",
"source": sender_hex,
"target": receiver_hex,
"rssi": rssi,
"count": count,
}
}
)
with open(json_output_path, "w") as json_file:
json.dump(cytoscape_data, json_file, indent=2)
print(f"Data successfully exported to {json_output_path}")
except sqlite3.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if conn:
conn.close()
def get_node_shortname_and_role(cursor, longname):
"""Get shortname and role for a node by its longname from the nodes table"""
cursor.execute("SELECT shortname, role FROM nodes WHERE longname = ?", (longname,))
result = cursor.fetchone()
if result:
return result[0], result[1] # shortname, role
return longname, None # Return original longname if no match found
def export_traceroutes_to_json(db_path, json_output_path, time_limit_minutes):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cutoff_time = int((datetime.now() - timedelta(minutes=time_limit_minutes)).timestamp())
current_time = int(datetime.now().timestamp())
cursor.execute(
"""SELECT from_node, to_node, COUNT(*) as count
FROM traceroutes
WHERE timestamp >= ? AND timestamp <= ?
GROUP BY from_node, to_node""",
(cutoff_time,current_time),
)
rows = cursor.fetchall()
connection_counts = defaultdict(int)
valid_connections = []
for row in rows:
from_node, to_node, count = row
valid_connections.append((from_node, to_node, count))
connection_counts[from_node] += 1
connection_counts[to_node] += 1
cytoscape_data = []
processed_nodes = set()
# Add nodes
for node_name in connection_counts.keys():
if node_name not in processed_nodes:
shortname, role = get_node_shortname_and_role(cursor, node_name)
node_id = shortname # Use shortname as node ID
label = shortname if shortname else node_name
cytoscape_data.append({
"data": {
"id": node_id,
"label": label,
"role": role,
"connections": connection_counts[node_name],
"original_name": node_name
}
})
processed_nodes.add(node_name)
# Add edges
for from_node, to_node, count in valid_connections:
from_shortname, _ = get_node_shortname_and_role(cursor, from_node)
to_shortname, _ = get_node_shortname_and_role(cursor, to_node)
cytoscape_data.append({
"data": {
"id": f"{from_shortname}_{to_shortname}",
"source": from_shortname,
"target": to_shortname,
"count": count,
"original_source": from_node,
"original_target": to_node
}
})
with open(json_output_path, "w") as json_file:
json.dump(cytoscape_data, json_file, indent=2)
print(f"Traceroute data successfully exported to {json_output_path}")
except sqlite3.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if conn:
conn.close()
def export_hourly_messages(db_path, distilled_db_path, days=1):
"""
Export the number of messages received in each hour for the past N days to a JSON file,
broken down by message type. Uses pre-aggregated data from distilled database.
Parameters:
db_path (str): Path to the main SQLite database (for traceroutes and neighbors)
distilled_db_path (str): Path to the distilled database with aggregated statistics
days (int): Number of days to look back (default: 1)
"""
try:
conn = sqlite3.connect(distilled_db_path)
cursor = conn.cursor()
# Calculate the cutoff hour
cutoff_time = (datetime.now() - timedelta(days=days)).replace(minute=0, second=0, microsecond=0)
cutoff_hour = cutoff_time.strftime('%Y-%m-%d %H:00')
# Query to get message counts by hour and type from pre-aggregated data
cursor.execute("""
SELECT
hour,
message_type as type,
count as message_count
FROM hourly_message_counts
WHERE hour >= ?
ORDER BY hour, type
""", (cutoff_hour,))
rows = cursor.fetchall()
# Process data into format suitable for Plotly
hours = []
message_types = set()
hour_type_counts = defaultdict(lambda: defaultdict(int))
for row in rows:
hour_str, msg_type, count = row
hours.append(hour_str)
message_types.add(msg_type)
hour_type_counts[hour_str][msg_type] = count
hours = sorted(set(hours))
message_types = sorted(message_types)
# Create data structure for Plotly
plotly_data = {
"x": hours,
"types": list(message_types),
"data": {},
"metadata": {
"days": days,
"generated_at": datetime.now().isoformat(),
"total_messages": 0,
"messages_by_type": defaultdict(int)
}
}
# Fill in the data for each type
for msg_type in message_types:
plotly_data["data"][msg_type] = []
for hour in hours:
count = hour_type_counts[hour][msg_type]
plotly_data["data"][msg_type].append(count)
plotly_data["metadata"]["total_messages"] += count
plotly_data["metadata"]["messages_by_type"][msg_type] += count
# Calculate percentages for each type
total = plotly_data["metadata"]["total_messages"]
if total > 0:
for msg_type in message_types:
plotly_data["metadata"]["messages_by_type"][f"{msg_type}_percentage"] = (
plotly_data["metadata"]["messages_by_type"][msg_type] / total * 100
)
# Save to JSON file
output_path = f"{data}/hourly_messages_by_type_{days}d.json"
with open(output_path, "w") as f:
json.dump(plotly_data, f, indent=2)
print(f"Hourly message data by type exported to {output_path}")
except sqlite3.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if conn:
conn.close()
def export_hourly_unique_senders(db_path, distilled_db_path, days=1):
"""
Export the number of unique senders and physical senders in each hour for the past N days.
Uses pre-aggregated data from distilled database.
Parameters:
db_path (str): Path to the main SQLite database (for traceroutes and neighbors)
distilled_db_path (str): Path to the distilled database with aggregated statistics
days (int): Number of days to look back (default: 1)
"""
try:
conn = sqlite3.connect(distilled_db_path)
cursor = conn.cursor()
# Calculate the cutoff hour
cutoff_time = (datetime.now() - timedelta(days=days)).replace(minute=0, second=0, microsecond=0)
cutoff_hour = cutoff_time.strftime('%Y-%m-%d %H:00')
# Query to get unique sender counts by hour from pre-aggregated data
cursor.execute("""
SELECT
hour,
unique_senders,
unique_physical_senders
FROM hourly_unique_senders
WHERE hour >= ?
ORDER BY hour
""", (cutoff_hour,))
rows = cursor.fetchall()
# Process data into format suitable for Plotly
plotly_data = {
"x": [], # hours
"unique_senders": [],
"unique_physical_senders": [],
"metadata": {
"days": days,
"generated_at": datetime.now().isoformat(),
"total_unique_senders": 0,
"total_unique_physical_senders": 0,
"average_unique_senders_per_hour": 0,
"average_unique_physical_senders_per_hour": 0
}
}
total_unique_senders = 0
total_unique_physical_senders = 0
hour_count = 0
for row in rows:
hour_str, unique_senders, unique_physical_senders = row
plotly_data["x"].append(hour_str)
plotly_data["unique_senders"].append(unique_senders)
plotly_data["unique_physical_senders"].append(unique_physical_senders)
total_unique_senders += unique_senders
total_unique_physical_senders += unique_physical_senders
hour_count += 1
# Calculate averages
if hour_count > 0:
plotly_data["metadata"]["average_unique_senders_per_hour"] = total_unique_senders / hour_count
plotly_data["metadata"]["average_unique_physical_senders_per_hour"] = total_unique_physical_senders / hour_count
# Get total unique senders for the entire period from daily aggregates
cutoff_date = cutoff_time.strftime('%Y-%m-%d')
cursor.execute("""
SELECT
MAX(unique_senders) as total_unique_senders,
MAX(unique_physical_senders) as total_unique_physical_senders
FROM daily_unique_senders
WHERE date >= ?
""", (cutoff_date,))
total_row = cursor.fetchone()
if total_row:
plotly_data["metadata"]["total_unique_senders"] = total_row[0]
plotly_data["metadata"]["total_unique_physical_senders"] = total_row[1]
# Save to JSON file
output_path = f"{data}/hourly_unique_senders_{days}d.json"
with open(output_path, "w") as f:
json.dump(plotly_data, f, indent=2)
print(f"Hourly unique senders data exported to {output_path}")
except sqlite3.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if conn:
conn.close()
if __name__ == "__main__":
db_path = "mqtt_messages.db"
distilled_db_path = "mqtt_messages_distilled.db"
time_windows = [15, 30, 60, 3 * 60, 24 * 60]
for minutes in time_windows:
if minutes == 60:
time_str = "1h"
elif minutes == 3 * 60:
time_str = "3h"
elif minutes == 24 * 60:
time_str = "24h"
elif minutes == 30:
time_str = "30min"
elif minutes == 15:
time_str = "15min"
# Generate filenames with time window
messages_json = f"{data}/cytoscape_messages_{time_str}.json"
messages_physical_json = f"{data}/cytoscape_messages_physical_{time_str}.json"
neighbors_json = f"{data}/cytoscape_neighbors_{time_str}.json"
traceroutes_json = f"{data}/cytoscape_traceroutes_{time_str}.json"
print(f"\nExporting data for {time_str} time window...")
# Export topology data from main database
export_to_json(db_path, messages_json, minutes, use_physical_sender=False)
export_to_json(db_path, messages_physical_json, minutes, use_physical_sender=True)
export_neighbors_to_json(db_path, neighbors_json, minutes)
export_traceroutes_to_json(db_path, traceroutes_json, minutes)
if __name__ == "__main__":
db_path = "mqtt_messages.db"
time_windows = [15, 30, 60, 3 * 60, 24 * 60]
for minutes in time_windows:
if minutes == 60:
time_str = "1h"
elif minutes == 3 * 60:
time_str = "3h"
elif minutes == 24 * 60:
time_str = "24h"
elif minutes == 30:
time_str = "30min"
elif minutes == 15:
time_str = "15min"
# Generate filenames with time window
messages_json = f"{data}/cytoscape_messages_{time_str}.json"
messages_physical_json = f"{data}/cytoscape_messages_physical_{time_str}.json"
neighbors_json = f"{data}/cytoscape_neighbors_{time_str}.json"
traceroutes_json = f"{data}/cytoscape_traceroutes_{time_str}.json" # New file for traceroutes
print(f"\nExporting data for {time_str} time window...")
# Export all data types
export_to_json(db_path, messages_json, minutes, use_physical_sender=False)
export_to_json(db_path, messages_physical_json, minutes, use_physical_sender=True)
export_neighbors_to_json(db_path, neighbors_json, minutes)
export_traceroutes_to_json(db_path, traceroutes_json, minutes)
print("\nGenerating message count plots...")
for days in [1, 7, 14, 30]:
export_hourly_messages(db_path, distilled_db_path, days=days)
export_hourly_unique_senders(db_path, distilled_db_path, days=days)