Refactor report generation and logging handlers.
Introduced `CustomLogsHandler` to streamline log management and updated `ReportGenerator` to simplify report generation output. Removed unused asynchronous iterator methods, improving code clarity and reducing complexity.
This commit is contained in:
parent
1dbf774d55
commit
936eb6f394
2 changed files with 5 additions and 21 deletions
|
@ -37,7 +37,8 @@ async def get_report_endpoint(request: ReportRequest):
|
|||
try:
|
||||
# Call the asynchronous get_report function
|
||||
generator = ReportGenerator(request.query, request.report_type)
|
||||
await generator.generate_report()
|
||||
custom_logs_handler = await generator.generate_report()
|
||||
yield "Report generation completed successfully!\n"
|
||||
except Exception as e:
|
||||
yield f"Error: {str(e)}"
|
||||
|
||||
|
|
|
@ -4,15 +4,13 @@ from typing import Dict, Any, AsyncGenerator, Coroutine
|
|||
|
||||
class CustomLogsHandler:
|
||||
"""A custom Logs handler class to handle JSON data."""
|
||||
def __init__(self, logs=None):
|
||||
if logs is None:
|
||||
logs = []
|
||||
self.logs = logs # Initialize logs to store data
|
||||
def __init__(self):
|
||||
self.logs = [] # Initialize logs to store data
|
||||
|
||||
async def send_json(self, data: Dict[str, Any]) -> None:
|
||||
"""Send JSON data and log it."""
|
||||
self.logs.append(data) # Append data to logs
|
||||
print(f"My custom Log: {data}")
|
||||
print(f"My custom Log: {data}") # For demonstration, print the log
|
||||
|
||||
class ReportGenerator:
|
||||
def __init__(self, query: str, report_type: str):
|
||||
|
@ -51,18 +49,3 @@ class ReportGenerator:
|
|||
"query": self.query,
|
||||
"report_type": self.report_type
|
||||
}
|
||||
|
||||
# Implementing the asynchronous iterator protocol
|
||||
def __aiter__(self):
|
||||
"""Initialize and return the asynchronous iterator."""
|
||||
self._log_index = 0 # Iterator index
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
"""Return the next log asynchronously."""
|
||||
if self._log_index < len(self.logs):
|
||||
log = self.logs[self._log_index]
|
||||
self._log_index += 1
|
||||
yield log # Return the next log
|
||||
else:
|
||||
raise StopAsyncIteration # Stop when logs are exhausted
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue