-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize_database.py
More file actions
118 lines (95 loc) · 3.66 KB
/
optimize_database.py
File metadata and controls
118 lines (95 loc) · 3.66 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
"""
Database optimization script for OpenSensor API
Adds indexes and optimizations for the FreeTier collection
"""
from pymongo import ASCENDING, DESCENDING
from opensensor.db import get_open_sensor_db
def create_indexes():
"""Create optimized indexes for the FreeTier collection"""
db = get_open_sensor_db()
collection = db["FreeTier"]
print("Creating indexes for FreeTier collection...")
# Primary compound index for time-series queries
# This covers the most common query pattern: device + time range
collection.create_index(
[
("metadata.device_id", ASCENDING),
("metadata.name", ASCENDING),
("timestamp", DESCENDING),
],
name="device_time_idx",
)
print("✓ Created device_time_idx")
# Regular indexes for specific sensor types with timestamp
# Note: Sparse indexes are not supported on time-series collections
sensor_fields = [
"temp",
"rh",
"ppm_CO2",
"moisture_readings",
"pH",
"pressure",
"lux",
"liquid",
"relays",
]
for field in sensor_fields:
try:
collection.create_index(
[(field, ASCENDING), ("timestamp", DESCENDING)], name=f"{field}_time_idx"
)
print(f"✓ Created {field}_time_idx")
except Exception as e:
# Skip if field doesn't exist or index creation fails
print(f"⚠️ Skipped {field}_time_idx: {str(e)}")
# User-based queries optimization
try:
collection.create_index(
[("metadata.user_id", ASCENDING), ("timestamp", DESCENDING)], name="user_time_idx"
)
print("✓ Created user_time_idx")
except Exception as e:
print(f"⚠️ Skipped user_time_idx: {str(e)}")
# Optimize Users collection
users_collection = db["Users"]
# API key lookup optimization
users_collection.create_index(
[("api_keys.device_id", ASCENDING), ("api_keys.device_name", ASCENDING)],
name="api_keys_device_idx",
)
print("✓ Created api_keys_device_idx")
# API key validation optimization
users_collection.create_index("api_keys.key", name="api_key_lookup_idx")
print("✓ Created api_key_lookup_idx")
print("\nIndexes created successfully!")
# Print index statistics
print("\nCurrent indexes:")
for index in collection.list_indexes():
print(f" - {index['name']}: {index.get('key', {})}")
def analyze_collection_stats():
"""Analyze collection statistics for optimization insights"""
db = get_open_sensor_db()
collection = db["FreeTier"]
print("\n=== Collection Statistics ===")
stats = db.command("collStats", "FreeTier")
print(f"Document count: {stats.get('count', 0):,}")
print(f"Average document size: {stats.get('avgObjSize', 0):,} bytes")
print(f"Total collection size: {stats.get('size', 0) / (1024*1024):.2f} MB")
print(f"Storage size: {stats.get('storageSize', 0) / (1024*1024):.2f} MB")
print(f"Total index size: {stats.get('totalIndexSize', 0) / (1024*1024):.2f} MB")
# Sample document analysis
sample_doc = collection.find_one()
if sample_doc:
print(f"\nSample document fields: {list(sample_doc.keys())}")
if "metadata" in sample_doc:
print(f"Metadata fields: {list(sample_doc['metadata'].keys())}")
if __name__ == "__main__":
print("OpenSensor Database Optimization")
print("=" * 40)
try:
analyze_collection_stats()
create_indexes()
print("\n✅ Database optimization completed successfully!")
except Exception as e:
print(f"\n❌ Error during optimization: {e}")
raise