|
| 1 | +import asyncio |
| 2 | +import inspect |
1 | 3 | import logging |
2 | 4 | import sys |
3 | 5 | from datetime import datetime |
4 | 6 |
|
5 | 7 | import pytz |
6 | | -from connect.client import ClientError, ConnectClient |
| 8 | +from connect.client import AsyncConnectClient, ClientError, ConnectClient |
7 | 9 | from connect.reports.constants import REPORTS_ENV |
8 | 10 | from connect.reports.datamodels import Account, Report |
9 | 11 | from connect.reports.renderers import get_renderer |
@@ -72,20 +74,30 @@ def normalize_parameters(connect_parameters): |
72 | 74 | return parameters |
73 | 75 |
|
74 | 76 |
|
| 77 | +async def execute_report_async(entrypoint, args, renderer, output_file): |
| 78 | + if inspect.iscoroutinefunction(entrypoint): |
| 79 | + data = await entrypoint(*args) |
| 80 | + else: |
| 81 | + data = entrypoint(*args) |
| 82 | + return await renderer.render_async( |
| 83 | + data, |
| 84 | + output_file, |
| 85 | + start_time=datetime.now(tz=pytz.utc), |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def _run_render(is_async, entrypoint, args, renderer, output_file): |
| 90 | + if is_async: |
| 91 | + return asyncio.run(execute_report_async(entrypoint, args, renderer, output_file)) |
| 92 | + else: |
| 93 | + data = entrypoint(*args) |
| 94 | + return renderer.render(data, output_file, start_time=datetime.now(tz=pytz.utc)) |
| 95 | + |
| 96 | + |
75 | 97 | def execute_report(control_client, report_definition, connect_report): # noqa: CCR001 |
76 | 98 | report_env = get_report_env() |
77 | 99 | reports_dir = get_default_reports_dir() |
78 | 100 |
|
79 | | - report_client = ConnectClient( |
80 | | - endpoint=report_env["api_endpoint"], |
81 | | - use_specs=False, |
82 | | - api_key=report_env["client_token"], |
83 | | - max_retries=5, |
84 | | - default_limit=500, |
85 | | - default_headers=get_user_agent(), |
86 | | - timeout=(360, 360), |
87 | | - resourceset_append=False, |
88 | | - ) |
89 | 101 | connect_parameters = connect_report.get('parameters', []) |
90 | 102 | parameters = normalize_parameters(connect_parameters) |
91 | 103 |
|
@@ -120,6 +132,23 @@ def progress(current_value, max_value): |
120 | 132 | logger.exception('An error ocurred while importing report entrypoint.') |
121 | 133 | handle_preparation_exception(e, control_client) |
122 | 134 |
|
| 135 | + is_async = ( |
| 136 | + inspect.isasyncgenfunction(report_entry_point) |
| 137 | + or inspect.iscoroutinefunction(report_entry_point) |
| 138 | + ) |
| 139 | + |
| 140 | + client_class = AsyncConnectClient if is_async else ConnectClient |
| 141 | + report_client = client_class( |
| 142 | + endpoint=report_env["api_endpoint"], |
| 143 | + use_specs=False, |
| 144 | + api_key=report_env["client_token"], |
| 145 | + max_retries=5, |
| 146 | + default_limit=500, |
| 147 | + default_headers=get_user_agent(), |
| 148 | + timeout=(360, 360), |
| 149 | + resourceset_append=False, |
| 150 | + ) |
| 151 | + |
123 | 152 | renderer_id = connect_report['renderer'] |
124 | 153 | renderer_definition = next( |
125 | 154 | filter( |
@@ -151,8 +180,7 @@ def progress(current_value, max_value): |
151 | 180 | renderer.set_extra_context, |
152 | 181 | ], |
153 | 182 | ) |
154 | | - data = report_entry_point(*args) |
155 | | - return renderer.render(data, '/report', start_time=datetime.now(tz=pytz.utc)) |
| 183 | + return _run_render(is_async, report_entry_point, args, renderer, '/report') |
156 | 184 | except Exception as e: |
157 | 185 | handle_exception(e, control_client, connect_report) |
158 | 186 |
|
|
0 commit comments