Here's a design for a notification framework that can aggregate test results from systems without direct network access. This solution uses a combination of message queues and a centralized aggregator service.
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
import json
import uuid
from typing import List, Dict, Any
# Core Data Structures
class TestStatus(Enum):
PASSED = "PASSED"
FAILED = "FAILED"
ERROR = "ERROR"
SKIPPED = "SKIPPED"
@dataclass
class TestResult:
test_id: str
system_id: str
status: TestStatus
timestamp: datetime
details: Dict[str, Any]
# Message Transport Interface
class MessageTransport(ABC):
@abstractmethod
def send_message(self, message: Dict) -> bool:
pass
@abstractmethod
def receive_messages(self) -> List[Dict]:
pass
# Example File-based Transport
class FileBasedTransport(MessageTransport):
def __init__(self, file_path: str):
self.file_path = file_path
def send_message(self, message: Dict) -> bool:
try:
with open(self.file_path, 'a') as f:
json.dump(message, f)
f.write('\n')
return True
except Exception:
return False
def receive_messages(self) -> List[Dict]:
messages = []
try:
with open(self.file_path, 'r') as f:
for line in f:
messages.append(json.loads(line))
except Exception:
pass
return messages
# Test Result Reporter (runs on test systems)
class TestReporter:
def __init__(self, system_id: str, transport: MessageTransport):
self.system_id = system_id
self.transport = transport
def report_result(self, test_result: TestResult) -> bool:
message = {
"test_id": test_result.test_id,
"system_id": self.system_id,
"status": test_result.status.value,
"timestamp": test_result.timestamp.isoformat(),
"details": test_result.details
}
return self.transport.send_message(message)
# Result Aggregator (runs on central system)
class ResultAggregator:
def __init__(self, transport: MessageTransport):
self.transport = transport
self.results: Dict[str, List[TestResult]] = {}
def collect_results(self) -> None:
messages = self.transport.receive_messages()
for message in messages:
result = TestResult(
test_id=message["test_id"],
system_id=message["system_id"],
status=TestStatus(message["status"]),
timestamp=datetime.fromisoformat(message["timestamp"]),
details=message["details"]
)
if result.test_id not in self.results:
self.results[result.test_id] = []
self.results[result.test_id].append(result)
def get_results_for_test(self, test_id: str) -> List[TestResult]:
return self.results.get(test_id, [])
# Example Usage
def example_usage():
# Setup transport mechanism (could be file, database, message queue, etc.)
transport = FileBasedTransport("test_results.json")
# On Test System 1
reporter1 = TestReporter("system1", transport)
test_result = TestResult(
test_id=str(uuid.uuid4()),
system_id="system1",
status=TestStatus.PASSED,
timestamp=datetime.now(),
details={"duration": 1.5, "memory_usage": "100MB"}
)
reporter1.report_result(test_result)
# On Central System
aggregator = ResultAggregator(transport)
aggregator.collect_results()
# Get results for a specific test
results = aggregator.get_results_for_test(test_result.test_id)
for result in results:
print(f"Test {result.test_id} on {result.system_id}: {result.status}")
# Additional features that could be added:
class NotificationManager:
def __init__(self):
self.subscribers = []
def subscribe(self, callback):
self.subscribers.append(callback)
def notify(self, test_result: TestResult):
for subscriber in self.subscribers:
subscriber(test_result)
class ResultAnalyzer:
def analyze_results(self, results: List[TestResult]) -> Dict[str, Any]:
# Implement analysis logic
pass
class ReportGenerator:
def generate_report(self, results: List[TestResult]) -> str:
# Implement report generation logic
pass
This framework provides several key features:
-
Flexible Transport: The MessageTransport interface allows for different implementation of message passing (file-based, database, message queue, etc.)
-
Decoupled Components:
TestReporter: Runs on test systems and reports results
ResultAggregator: Runs on central system and collects results
NotificationManager: Handles notifications to subscribers
ResultAnalyzer: Analyzes collected results
ReportGenerator: Generates reports from results
-
Extensible Design:
- New transport mechanisms can be added by implementing
MessageTransport
- Additional analysis and reporting features can be added
- Notification system can be extended with different types of notifications
To use this framework:
-
Implement appropriate MessageTransport for your environment (e.g., using Redis, RabbitMQ, or even simple file-based transport)
-
On each test system:
reporter = TestReporter("system_id", transport)
reporter.report_result(test_result)
-
On the central system:
aggregator = ResultAggregator(transport)
aggregator.collect_results()
results = aggregator.get_results_for_test(test_id)
Possible enhancements:
- Add error handling and retry mechanisms
- Implement different transport mechanisms (Redis, RabbitMQ, etc.)
- Add result persistence
- Implement real-time notifications
- Add authentication and security measures
- Add result filtering and search capabilities
- Implement result aggregation and statistics
This framework provides a foundation that can be extended based on specific needs while maintaining separation of concerns and flexibility in implementation.