-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorchestrate.py
More file actions
executable file
Β·131 lines (110 loc) Β· 3.99 KB
/
orchestrate.py
File metadata and controls
executable file
Β·131 lines (110 loc) Β· 3.99 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
#!/usr/bin/env python3
"""
Orchestrator for database_management.
Demonstrates the improved database management module with a working example.
"""
import sys
import tempfile
from pathlib import Path
# Ensure codomyrmex is in path
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
from codomyrmex.database_management import (
BackupManager,
DatabasePerformanceMonitor,
MigrationManager,
manage_databases,
)
def main():
# Auto-injected: Load configuration
from pathlib import Path
import yaml
config_path = (
Path(__file__).resolve().parent.parent.parent
/ "config"
/ "database_management"
/ "config.yaml"
)
if config_path.exists():
with open(config_path) as f:
yaml.safe_load(f) or {}
print(f"Loaded config from {config_path.name}")
print("--- Codomyrmex Database Management Orchestrator ---")
# 1. Setup workspace and database
with tempfile.TemporaryDirectory() as tmpdir:
workspace = Path(tmpdir)
db_file = workspace / "orchestrator_demo.db"
db_url = f"sqlite:///{db_file}"
print(f"[*] Initializing database: {db_url}")
manager = manage_databases(db_url)
# 2. Run Migrations
print("[*] Setting up migrations...")
mig_manager = MigrationManager(
workspace_dir=str(workspace), database_url=db_url
)
create_table_sql = """
CREATE TABLE sensors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT,
last_value REAL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""
mig_manager.create_migration(
name="create_sensors_table",
description="Initial schema for sensors",
sql=create_table_sql,
rollback_sql="DROP TABLE sensors;",
)
print("[*] Applying migrations...")
results = mig_manager.apply_pending_migrations()
for res in results:
if res.success:
print(f" [+] Migration {res.migration_id} applied successfully.")
# 3. Execute Queries
print("[*] Seeding data...")
with manager.transaction() as tx:
tx.execute(
"INSERT INTO sensors (name, type, last_value) VALUES (?, ?, ?)",
("Temp01", "temperature", 22.5),
commit=False,
)
tx.execute(
"INSERT INTO sensors (name, type, last_value) VALUES (?, ?, ?)",
("Hum01", "humidity", 45.0),
commit=False,
)
print("[*] Querying data...")
result = manager.execute("SELECT * FROM sensors")
if result.success:
for row in result.to_dict_list():
print(
f" - Sensor: {row['name']} ({row['type']}) = {row['last_value']}"
)
# 4. Performance Monitoring
print("[*] Recording performance metrics...")
monitor = DatabasePerformanceMonitor(workspace_dir=str(workspace))
monitor.record_database_metrics(
"demo_db",
{
"connections_active": 1,
"queries_per_second": 10.5,
"average_query_time_ms": result.execution_time * 1000,
},
)
report = monitor.analyze_database_performance("demo_db")
print(f" [+] Health status: {report.get('performance_issues', 'Healthy')}")
# 5. Backup
print("[*] Creating database backup...")
backup_manager = BackupManager(
workspace_dir=str(workspace), database_url=db_url
)
backup_res = backup_manager.create_backup("orchestrator_demo")
if backup_res.success:
print(
f" [+] Backup created: {backup_res.backup_id} ({backup_res.file_size_mb:.2f} MB)"
)
print("\n--- Orchestration Complete ---")
if __name__ == "__main__":
main()