in src/lambdas/r8s_report_generator/handler.py [0:0]
def get_priority(self, formatted_recommendations: list,
saving_threshold: int):
resource_mapping = {}
for recommendation in formatted_recommendations:
resource_id = recommendation.get('resource_id')
recommendation_type = recommendation.get('recommendation_type')
if resource_id not in resource_mapping:
resource_mapping[resource_id] = {}
if recommendation_type not in resource_mapping[resource_id]:
resource_mapping[resource_id][
recommendation_type] = recommendation
for recommendation in resource_mapping.values():
recommendation['estimated_savings'] = \
self._get_resource_saving(recommendation)
if saving_threshold and saving_threshold > 0:
_LOG.debug(f'Filtering priority resources with saving gt '
f'{saving_threshold}')
resource_mapping = {k: v for k, v in resource_mapping.items()
if max(v.get('estimated_savings')) >
saving_threshold}
_LOG.debug(f'Sorting priority resources by savings')
priority_resources = sorted(resource_mapping.values(),
key=lambda m: max(
m.get('estimated_savings')),
reverse=True)
result = []
for resource in priority_resources:
recommendation_keys = [key for key in resource.keys() if
key.isupper()]
recommendation = resource[recommendation_keys[0]]
current_price = recommendation.get('current_price')
item = {
'resource_id': recommendation.get('resource_id'),
'current_price': current_price,
'current_instance_type': recommendation.get(
'current_instance_type'),
"region": recommendation.get('region'),
'estimated_saving': resource.get('estimated_savings'),
'recommendations': {}
}
for key in recommendation_keys:
recommendation = resource[key]
item['recommendations'][key] = {
"recommendation": recommendation.get('recommendation'),
"estimated_saving": recommendation.get('estimated_saving')
}
result.append(item)
return result