-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_drift_tracker.py
More file actions
334 lines (263 loc) · 11.9 KB
/
advanced_drift_tracker.py
File metadata and controls
334 lines (263 loc) · 11.9 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
"""
Automated Architecture Discovery System
Copyright (c) 2025 Abhishek Datta
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
This file is part of the Automated Architecture Discovery System,
an educational project demonstrating microservices architecture discovery.
"""
"""
Advanced Drift Tracker
Tracks architecture changes over time with historical analysis
"""
import json
import os
import hashlib
from datetime import datetime
from collections import defaultdict
class AdvancedDriftTracker:
"""
Tracks architecture drift over time
Maintains history, calculates trends, generates alerts
"""
def __init__(self, history_dir='architecture_history'):
self.history_dir = history_dir
self.history_file = os.path.join(history_dir, 'drift_history.json')
self.history = []
# Create history directory if it doesn't exist
os.makedirs(history_dir, exist_ok=True)
# Load existing history
self._load_history()
def _load_history(self):
"""Load drift history from file"""
if os.path.exists(self.history_file):
try:
with open(self.history_file, 'r') as f:
self.history = json.load(f)
print(f"✅ Loaded {len(self.history)} historical snapshots")
except:
print("⚠️ No previous history found - starting fresh")
self.history = []
else:
print("📝 Creating new drift history")
self.history = []
def _save_history(self):
"""Save drift history to file"""
with open(self.history_file, 'w') as f:
json.dump(self.history, f, indent=2)
def calculate_architecture_hash(self, architecture):
"""Calculate hash of architecture for change detection"""
# Create a stable string representation
arch_string = json.dumps({
'services': sorted(architecture.get('services', [])),
'dependencies': {k: sorted(v) for k, v in architecture.get('service_dependencies', {}).items()},
'endpoints': {k: sorted(v) for k, v in architecture.get('service_endpoints', {}).items()}
}, sort_keys=True)
return hashlib.md5(arch_string.encode()).hexdigest()
def capture_snapshot(self):
"""Capture current architecture state as a snapshot"""
print("\n" + "="*60)
print("📸 CAPTURING ARCHITECTURE SNAPSHOT")
print("="*60 + "\n")
# Load current architecture
try:
with open('discovered_architecture.json', 'r') as f:
current_arch = json.load(f)
print("✅ Current architecture loaded\n")
except FileNotFoundError:
print("❌ Error: discovered_architecture.json not found")
print(" Please run architecture_tracer.py first")
return None
# Calculate hash
arch_hash = self.calculate_architecture_hash(current_arch)
# Check if this is a duplicate
if self.history and self.history[-1].get('hash') == arch_hash:
print("✅ No changes detected - architecture is stable")
print(f" Hash: {arch_hash}")
return None
# Create snapshot
snapshot = {
'timestamp': datetime.utcnow().isoformat(),
'hash': arch_hash,
'architecture': current_arch,
'metrics': current_arch.get('metrics', {})
}
# Detect changes if we have previous snapshots
changes = {}
if self.history:
changes = self._detect_changes(self.history[-1]['architecture'], current_arch)
snapshot['changes'] = changes
snapshot['drift_score'] = self._calculate_drift_score(changes)
else:
snapshot['changes'] = {'initial_snapshot': True}
snapshot['drift_score'] = 0
# Add to history
self.history.append(snapshot)
# Save snapshot to individual file
snapshot_filename = os.path.join(
self.history_dir,
f"snapshot_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.json"
)
with open(snapshot_filename, 'w') as f:
json.dump(snapshot, f, indent=2)
print(f"✅ Snapshot captured: {snapshot_filename}")
print(f" Hash: {arch_hash}")
if changes and 'initial_snapshot' not in changes:
print(f" Drift Score: {snapshot['drift_score']}/100")
print()
# Save updated history
self._save_history()
return snapshot
def _detect_changes(self, previous, current):
"""Detect changes between two architectures"""
changes = {}
# Services
prev_services = set(previous.get('services', []))
curr_services = set(current.get('services', []))
changes['services_added'] = list(curr_services - prev_services)
changes['services_removed'] = list(prev_services - curr_services)
# Dependencies
prev_deps = self._flatten_dependencies(previous.get('service_dependencies', {}))
curr_deps = self._flatten_dependencies(current.get('service_dependencies', {}))
changes['dependencies_added'] = list(curr_deps - prev_deps)
changes['dependencies_removed'] = list(prev_deps - curr_deps)
# Endpoints
prev_endpoints = self._flatten_endpoints(previous.get('service_endpoints', {}))
curr_endpoints = self._flatten_endpoints(current.get('service_endpoints', {}))
changes['endpoints_added'] = list(curr_endpoints - prev_endpoints)
changes['endpoints_removed'] = list(prev_endpoints - curr_endpoints)
return changes
def _flatten_dependencies(self, dependencies_dict):
"""Flatten dependencies into a set"""
deps = set()
for src, targets in dependencies_dict.items():
for target in targets:
deps.add(f"{src} -> {target}")
return deps
def _flatten_endpoints(self, endpoints_dict):
"""Flatten endpoints into a set"""
eps = set()
for service, endpoints in endpoints_dict.items():
for endpoint in endpoints:
eps.add(f"{service}:{endpoint}")
return eps
def _calculate_drift_score(self, changes):
"""Calculate drift score from changes"""
score = 0
score += len(changes.get('services_added', [])) * 15
score += len(changes.get('services_removed', [])) * 20
score += len(changes.get('dependencies_added', [])) * 7
score += len(changes.get('dependencies_removed', [])) * 10
score += len(changes.get('endpoints_added', [])) * 3
score += len(changes.get('endpoints_removed', [])) * 5
return min(score, 100)
def generate_trend_report(self):
"""Generate trend analysis report"""
print("\n" + "="*60)
print("📈 DRIFT TREND ANALYSIS")
print("="*60 + "\n")
if len(self.history) < 2:
print("⚠️ Not enough historical data for trend analysis")
print(f" Current snapshots: {len(self.history)}")
print(" Need at least 2 snapshots to detect trends")
return
report = []
report.append("="*60)
report.append("📊 ARCHITECTURE DRIFT TRENDS")
report.append("="*60)
report.append("")
# Timeline
first = self.history[0]['timestamp']
last = self.history[-1]['timestamp']
report.append(f"Analysis Period: {first} to {last}")
report.append(f"Total Snapshots: {len(self.history)}")
report.append("")
# Calculate change frequency
changes_count = sum(1 for snapshot in self.history if snapshot.get('drift_score', 0) > 0)
report.append(f"Total Architecture Changes: {changes_count}")
report.append(f"Stability Rate: {((len(self.history) - changes_count) / len(self.history) * 100):.1f}%")
report.append("")
# Metrics over time
report.append("="*60)
report.append("📈 METRICS TRENDS")
report.append("="*60)
report.append("")
first_metrics = self.history[0]['metrics']
last_metrics = self.history[-1]['metrics']
metrics_comparison = [
('Services', 'total_services'),
('Dependencies', 'total_dependencies'),
('Endpoints', 'total_endpoints'),
('Journeys', 'total_journeys')
]
report.append("Metric Growth Analysis:")
report.append("")
for label, key in metrics_comparison:
first_val = first_metrics.get(key, 0)
last_val = last_metrics.get(key, 0)
change = last_val - first_val
if change > 0:
report.append(f" {label}: {first_val} → {last_val} (+{change})")
elif change < 0:
report.append(f" {label}: {first_val} → {last_val} ({change})")
else:
report.append(f" {label}: {first_val} (no change)")
report.append("")
# Recent changes
if len(self.history) >= 2:
report.append("="*60)
report.append("🔍 RECENT CHANGES")
report.append("="*60)
report.append("")
recent = self.history[-1]
if 'changes' in recent and 'initial_snapshot' not in recent['changes']:
changes = recent['changes']
if changes.get('services_added'):
report.append("✅ Services Added:")
for service in changes['services_added']:
report.append(f" • {service}")
report.append("")
if changes.get('services_removed'):
report.append("❌ Services Removed:")
for service in changes['services_removed']:
report.append(f" • {service}")
report.append("")
if changes.get('dependencies_added'):
report.append("✅ Dependencies Added:")
for dep in changes['dependencies_added']:
report.append(f" • {dep}")
report.append("")
if changes.get('dependencies_removed'):
report.append("❌ Dependencies Removed:")
for dep in changes['dependencies_removed']:
report.append(f" • {dep}")
report.append("")
report.append("="*60)
report_text = "\n".join(report)
print(report_text)
# Save report
report_filename = os.path.join(self.history_dir, 'drift_trend_report.txt')
with open(report_filename, 'w') as f:
f.write(report_text)
print(f"\n💾 Trend report saved to: {report_filename}\n")
def main():
"""Main execution"""
print("\n" + "="*60)
print("📊 ADVANCED DRIFT TRACKER")
print("Historical Architecture Analysis")
print("="*60)
tracker = AdvancedDriftTracker()
# Capture current snapshot
snapshot = tracker.capture_snapshot()
# Generate trend report
tracker.generate_trend_report()
print("="*60)
print("✅ DRIFT TRACKING COMPLETE")
print("="*60)
print(f"\n📁 History Directory: architecture_history/")
print(f" • drift_history.json - Complete history")
print(f" • snapshot_*.json - Individual snapshots")
print(f" • drift_trend_report.txt - Latest trends")
print("\n" + "="*60 + "\n")
if __name__ == '__main__':
main()