File size: 18,773 Bytes
fd357f4 |
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
#!/usr/bin/env python3
"""
DTO Confluence Client - Automated post-run report generation and publishing
Creates detailed analysis reports for completed data transfers
"""
import os
import json
from typing import Dict, Any, Optional, List
from datetime import datetime, timezone
import requests
from requests.auth import HTTPBasicAuth
import base64
from pathlib import Path
class DTOConfluenceClient:
def __init__(self, server_url: Optional[str] = None, username: Optional[str] = None,
api_token: Optional[str] = None, space_key: str = "DTO"):
self.server_url = server_url or os.getenv('CONFLUENCE_SERVER_URL')
self.username = username or os.getenv('CONFLUENCE_USERNAME')
self.api_token = api_token or os.getenv('CONFLUENCE_API_TOKEN')
self.space_key = space_key
self.auth = HTTPBasicAuth(self.username, self.api_token) if self.username and self.api_token else None
self.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
def test_connection(self) -> bool:
"""Test Confluence API connectivity"""
if not self.auth or not self.server_url:
print("β Confluence credentials not configured")
return False
try:
response = requests.get(
f"{self.server_url}/rest/api/user/current",
auth=self.auth,
headers=self.headers,
timeout=10
)
if response.status_code == 200:
user_info = response.json()
print(f"β
Connected to Confluence as {user_info.get('displayName')}")
return True
else:
print(f"β Confluence connection failed: {response.status_code}")
return False
except Exception as e:
print(f"β Error connecting to Confluence: {e}")
return False
def get_or_create_parent_page(self) -> Optional[str]:
"""Get or create parent page for DTO reports"""
if not self.auth:
return None
try:
# Check if DTO Reports page exists
search_response = requests.get(
f"{self.server_url}/rest/api/content",
auth=self.auth,
headers=self.headers,
params={
'spaceKey': self.space_key,
'title': 'DTO Transfer Reports',
'type': 'page'
}
)
if search_response.status_code == 200:
results = search_response.json()['results']
if results:
page_id = results[0]['id']
print(f"β
Found existing DTO Reports page: {page_id}")
return page_id
# Create parent page if it doesn't exist
parent_content = {
"type": "page",
"title": "DTO Transfer Reports",
"space": {"key": self.space_key},
"body": {
"storage": {
"value": """
<h2>Data Transfer Operations - Automated Reports</h2>
<p>This page contains automated post-run analysis reports for all DTO transfers.</p>
<p>Reports are automatically generated after each transfer completion and include:</p>
<ul>
<li>Performance metrics and throughput analysis</li>
<li>Data integrity validation results</li>
<li>Infrastructure utilization statistics</li>
<li>Error analysis and recommendations</li>
<li>Cost analysis and optimization suggestions</li>
</ul>
<p><strong>Latest Reports:</strong></p>
<ac:structured-macro ac:name="children">
<ac:parameter ac:name="sort">creation</ac:parameter>
<ac:parameter ac:name="reverse">true</ac:parameter>
<ac:parameter ac:name="max">20</ac:parameter>
</ac:structured-macro>
""",
"representation": "storage"
}
}
}
create_response = requests.post(
f"{self.server_url}/rest/api/content",
auth=self.auth,
headers=self.headers,
data=json.dumps(parent_content)
)
if create_response.status_code == 200:
page_id = create_response.json()['id']
print(f"β
Created DTO Reports parent page: {page_id}")
return page_id
else:
print(f"β Failed to create parent page: {create_response.status_code}")
return None
except Exception as e:
print(f"β Error managing parent page: {e}")
return None
def generate_run_report_content(self, run_data: Dict[str, Any],
metrics: Dict[str, Any]) -> str:
"""Generate comprehensive run report content"""
run_id = run_data.get('run_id', 'unknown')
status = run_data.get('final_status', 'unknown')
data_class = run_data.get('data_class', 'unknown')
environment = run_data.get('environment', 'unknown')
# Performance metrics
throughput = metrics.get('average_throughput_mbps', 0)
duration_seconds = metrics.get('total_duration_seconds', 0)
data_size_gb = metrics.get('data_size_gb', 0)
# Convert duration to human readable
hours = duration_seconds // 3600
minutes = (duration_seconds % 3600) // 60
seconds = duration_seconds % 60
duration_str = f"{hours}h {minutes}m {seconds}s"
# Status color
status_color = {
'SUCCESS': '#00875A',
'FAILED': '#DE350B',
'PARTIAL': '#FF8B00'
}.get(status, '#6B778C')
content = f"""
<h1>DTO Transfer Report: {run_id}</h1>
<table class="wrapped">
<tr>
<th>Run ID</th>
<td><strong>{run_id}</strong></td>
<th>Data Class</th>
<td><ac:structured-macro ac:name="status">
<ac:parameter ac:name="colour">Blue</ac:parameter>
<ac:parameter ac:name="title">{data_class}</ac:parameter>
</ac:structured-macro></td>
</tr>
<tr>
<th>Status</th>
<td><ac:structured-macro ac:name="status">
<ac:parameter ac:name="colour">{status_color}</ac:parameter>
<ac:parameter ac:name="title">{status}</ac:parameter>
</ac:structured-macro></td>
<th>Environment</th>
<td>{environment.upper()}</td>
</tr>
<tr>
<th>Data Size</th>
<td>{data_size_gb:.2f} GB</td>
<th>Duration</th>
<td>{duration_str}</td>
</tr>
<tr>
<th>Average Throughput</th>
<td><strong>{throughput:.1f} MB/s</strong></td>
<th>Transfer Method</th>
<td>{metrics.get('transfer_method', 'unknown')}</td>
</tr>
</table>
<h2>π Performance Analysis</h2>
<h3>Throughput Performance</h3>
<p>The transfer achieved an average throughput of <strong>{throughput:.1f} MB/s</strong>, transferring {data_size_gb:.2f} GB in {duration_str}.</p>
<table class="wrapped">
<tr>
<th>Metric</th>
<th>Value</th>
<th>Baseline</th>
<th>Performance</th>
</tr>
<tr>
<td>Throughput</td>
<td>{throughput:.1f} MB/s</td>
<td>500 MB/s</td>
<td>{'β
Above baseline' if throughput >= 500 else 'β οΈ Below baseline'}</td>
</tr>
<tr>
<td>Efficiency</td>
<td>{(throughput/10000*100):.1f}%</td>
<td>5%</td>
<td>{'β
Efficient' if throughput/10000 >= 0.05 else 'β οΈ Inefficient'}</td>
</tr>
</table>
<h3>Infrastructure Utilization</h3>
<ul>
<li><strong>Source Host:</strong> {metrics.get('source_host', 'unknown')}</li>
<li><strong>Target Host:</strong> {metrics.get('target_host', 'unknown')}</li>
<li><strong>Network Path:</strong> {metrics.get('network_path', 'Direct connection')}</li>
<li><strong>Compression:</strong> {metrics.get('compression_enabled', 'No')}</li>
</ul>
<h2>π Data Integrity Validation</h2>
"""
# Add validation results if available
validation_results = metrics.get('validation_results', {})
if validation_results:
files_validated = validation_results.get('files_validated', 0)
files_passed = validation_results.get('files_passed', 0)
content += f"""
<table class="wrapped">
<tr>
<th>Files Validated</th>
<td>{files_validated}</td>
</tr>
<tr>
<th>Files Passed</th>
<td>{files_passed}</td>
</tr>
<tr>
<th>Validation Rate</th>
<td>{(files_passed/files_validated*100) if files_validated > 0 else 0:.1f}%</td>
</tr>
<tr>
<th>Checksum Algorithm</th>
<td>{validation_results.get('checksum_algorithm', 'SHA-256')}</td>
</tr>
</table>
"""
# Add error analysis if any issues
errors = metrics.get('errors', [])
if errors:
content += f"""
<h2>β οΈ Issues and Errors</h2>
<ac:structured-macro ac:name="warning">
<ac:parameter ac:name="title">Transfer Issues Detected</ac:parameter>
<ac:rich-text-body>
<ul>
"""
for error in errors[:5]: # Limit to 5 errors
content += f" <li>{error}</li>\n"
content += """ </ul>
</ac:rich-text-body>
</ac:structured-macro>
"""
# Add recommendations
content += f"""
<h2>π― Recommendations</h2>
<h3>Performance Optimization</h3>
<ul>
"""
if throughput < 500:
content += " <li>Consider increasing parallel transfer streams</li>\n"
content += " <li>Optimize network configuration for higher throughput</li>\n"
if duration_seconds > 7200: # 2 hours
content += " <li>For large transfers, consider using compression</li>\n"
content += " <li>Schedule transfers during off-peak hours</li>\n"
content += """</ul>
<h3>Cost Optimization</h3>
<ul>
<li>Monitor bandwidth usage for cost optimization</li>
<li>Consider data deduplication for recurring transfers</li>
</ul>
<h2>π Artifacts and Logs</h2>
<p>The following artifacts were generated during this transfer:</p>
<ul>
"""
artifacts = metrics.get('artifacts', [])
for artifact in artifacts:
filename = Path(artifact).name
content += f" <li><code>{filename}</code> - {artifact}</li>\n"
content += f"""</ul>
<h2>π Historical Context</h2>
<p>This report was automatically generated on {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')} by the DTO automated reporting system.</p>
<ac:structured-macro ac:name="info">
<ac:parameter ac:name="title">Next Steps</ac:parameter>
<ac:rich-text-body>
<ul>
<li>Review performance metrics against SLOs</li>
<li>Analyze any validation failures</li>
<li>Implement recommended optimizations</li>
<li>Update data lineage documentation</li>
</ul>
</ac:rich-text-body>
</ac:structured-macro>
"""
return content
def create_run_report(self, run_data: Dict[str, Any],
metrics: Dict[str, Any]) -> Optional[str]:
"""Create Confluence page for run report"""
if not self.auth:
print("β Confluence not configured")
return None
run_id = run_data.get('run_id', 'unknown')
data_class = run_data.get('data_class', 'unknown')
environment = run_data.get('environment', 'unknown')
# Get parent page
parent_page_id = self.get_or_create_parent_page()
if not parent_page_id:
print("β Could not get parent page")
return None
try:
# Generate report content
report_content = self.generate_run_report_content(run_data, metrics)
# Create page
page_data = {
"type": "page",
"title": f"Transfer Report: {run_id} ({data_class} - {environment})",
"space": {"key": self.space_key},
"ancestors": [{"id": parent_page_id}],
"body": {
"storage": {
"value": report_content,
"representation": "storage"
}
}
}
response = requests.post(
f"{self.server_url}/rest/api/content",
auth=self.auth,
headers=self.headers,
data=json.dumps(page_data)
)
if response.status_code == 200:
page_info = response.json()
page_id = page_info['id']
page_url = f"{self.server_url}/wiki{page_info['_links']['webui']}"
print(f"β
Created Confluence report: {page_url}")
return page_url
else:
print(f"β Failed to create report: {response.status_code} - {response.text}")
return None
except Exception as e:
print(f"β Error creating report: {e}")
return None
def attach_file_to_page(self, page_id: str, file_path: str,
comment: str = "DTO transfer artifact") -> bool:
"""Attach file to Confluence page"""
if not self.auth or not Path(file_path).exists():
return False
try:
with open(file_path, 'rb') as f:
files = {
'file': (Path(file_path).name, f, 'application/octet-stream'),
'comment': (None, comment)
}
response = requests.post(
f"{self.server_url}/rest/api/content/{page_id}/child/attachment",
auth=self.auth,
files=files
)
if response.status_code == 200:
print(f"β
Attached file to page: {Path(file_path).name}")
return True
else:
print(f"β Failed to attach file: {response.status_code}")
return False
except Exception as e:
print(f"β Error attaching file: {e}")
return False
def update_run_report(self, page_id: str, updated_metrics: Dict[str, Any]) -> bool:
"""Update existing run report with new metrics"""
if not self.auth:
return False
try:
# Get current page content
response = requests.get(
f"{self.server_url}/rest/api/content/{page_id}",
auth=self.auth,
headers=self.headers,
params={'expand': 'body.storage,version'}
)
if response.status_code != 200:
print(f"β Failed to get page: {response.status_code}")
return False
page_data = response.json()
current_version = page_data['version']['number']
# Update content (simplified - append metrics)
current_content = page_data['body']['storage']['value']
metrics_update = f"""
<h2>π Updated Metrics</h2>
<p><em>Updated: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}</em></p>
<ul>
"""
for key, value in updated_metrics.items():
metrics_update += f" <li><strong>{key}:</strong> {value}</li>\n"
metrics_update += "</ul>\n"
updated_content = current_content + metrics_update
# Update page
update_data = {
"version": {"number": current_version + 1},
"title": page_data['title'],
"type": "page",
"body": {
"storage": {
"value": updated_content,
"representation": "storage"
}
}
}
update_response = requests.put(
f"{self.server_url}/rest/api/content/{page_id}",
auth=self.auth,
headers=self.headers,
data=json.dumps(update_data)
)
if update_response.status_code == 200:
print(f"β
Updated Confluence report: {page_id}")
return True
else:
print(f"β Failed to update report: {update_response.status_code}")
return False
except Exception as e:
print(f"β Error updating report: {e}")
return False
# Test function
def test_confluence_integration():
"""Test Confluence integration with mock report"""
client = DTOConfluenceClient()
if not client.test_connection():
print("β Confluence integration test failed (expected without proper credentials)")
return False
# Test report creation
test_run_data = {
'run_id': 'test-confluence-001',
'job_id': 'test-job-001',
'data_class': 'CLASS_A',
'environment': 'production',
'final_status': 'SUCCESS',
'manifest_path': '/manifests/class_a/test.yaml'
}
test_metrics = {
'average_throughput_mbps': 734.2,
'total_duration_seconds': 5400, # 1.5 hours
'data_size_gb': 150.0,
'transfer_method': 'ssh+dd',
'source_host': 'vast2.adapt.ai',
'target_host': 'vast1.adapt.ai',
'validation_results': {
'files_validated': 145,
'files_passed': 145,
'checksum_algorithm': 'SHA-256'
},
'artifacts': [
'/logs/test-confluence-001.log',
'/reports/test-confluence-001.pdf',
'/checksums/test-confluence-001.sha256'
]
}
report_url = client.create_run_report(test_run_data, test_metrics)
if report_url:
print(f"β
Confluence integration test completed: {report_url}")
return True
else:
print("β Failed to create test report")
return False
if __name__ == "__main__":
print("Testing DTO Confluence Integration...")
print("=" * 50)
test_confluence_integration()
print("\nTo use Confluence integration, set these environment variables:")
print("export CONFLUENCE_SERVER_URL=https://your-domain.atlassian.net/wiki")
print("export CONFLUENCE_USERNAME=your-email@company.com")
print("export CONFLUENCE_API_TOKEN=your-api-token") |