in src/lambdas/r8s_report_generator/handler.py [0:0]
def generate_report(self, customer, tenant, processing_days,
priority_saving_threshold):
processing_from_date, processing_to_date = \
self.get_processing_date_range(processing_days)
recommendations = self.recommendation_service.list(
customer=customer.name,
tenant=tenant.name,
from_dt=processing_from_date
)
if not recommendations:
_LOG.error(f'No recommendations found '
f'for tenant \'{tenant.name}\'')
return build_response(
code=RESPONSE_BAD_REQUEST_CODE,
content=f'No recommendations found '
f'for tenant \'{tenant.name}\''
)
_LOG.debug(f'Filtering recommendation to include only one '
f'recommendations from the last job with the resource')
recommendations = self.filter_latest_job_resource(
recommendations=recommendations
)
_LOG.debug(f'Formatting recommendations')
formatted = []
for recommendation in recommendations:
formatted_recommendation = self.format_recommendation(
recommendation=recommendation)
formatted.append(formatted_recommendation)
priority_resources = self.get_priority(
formatted_recommendations=formatted,
saving_threshold=priority_saving_threshold)
_LOG.debug(f'Calculating total summary')
total_summary = self._resources_summary(resources=formatted)
_LOG.debug(f'Total summary: {total_summary}')
if len(priority_resources) > MAX_PRIORITY_RESOURCES:
priority_resources = priority_resources[0:MAX_PRIORITY_RESOURCES]
_LOG.debug(f'Dividing resources by recommendation type')
by_type = self.divide_by_recommendation_type(
formatted,
max_per_type=MAX_RESOURCES_PER_TYPE)
_LOG.debug(f'Calculating displayed items summary')
displayed_resources = list(itertools.chain.from_iterable(
by_type.values()))
report_summary = self._resources_summary(
resources=list(displayed_resources))
_LOG.debug(f'Displayed resources summary: {report_summary}')
job_id = self.get_most_frequent_job_id(recommendations=recommendations)
report_item = {
'summary': {
"total": total_summary,
"displayed": report_summary
},
'high_priority': priority_resources,
'detailed': by_type,
"from": self.to_milliseconds(processing_from_date),
"to": self.to_milliseconds(processing_to_date),
CUSTOMER_ATTR: customer.name,
TENANT_ATTR: tenant.name,
"timezone": self.resolve_timezone(job_id=job_id)
}
_LOG.debug(f'Report: {report_item}')
return report_item