1616from azure .mgmt .costmanagement import CostManagementClient
1717from azure .mgmt .costmanagement .models import QueryDefinition , TimeframeType
1818
19- from .utils import copy_assets , get_cost_summary , get_risk_summary , prepare_alternative_technologies
19+ from .utils import copy_assets
2020from .utils_aws import build_aws_resource_inventory , build_aws_cost_inventory
2121from .utils_azure import build_azure_resource_inventory , build_azure_cost_inventory
2222from .utils_db import connect , load_data
23+ from .utils_report import generate_html_report , generate_pdf_report , generate_json_report
2324
2425# Configure the logger
2526logger = logging .getLogger ("core.engine" )
@@ -271,7 +272,7 @@ def perform_risk_assessment(exit_strategy, report_path):
271272 return {"success" : False , "logs" : str (e )}
272273
273274# Stage 6
274- def generate_report (cloud_service_provider , exit_strategy , assessment_type , report_path ):
275+ def generate_report (provider_details , cloud_service_provider , exit_strategy , assessment_type , report_path , raw_data_path ):
275276 try :
276277 db_path = os .path .join (report_path , "data" , "assessment.db" )
277278
@@ -286,81 +287,38 @@ def generate_report(cloud_service_provider, exit_strategy, assessment_type, repo
286287 cost_data = load_data ("cost_inventory" , db_path = db_path )
287288 risk_data = load_data ("risk_inventory" , db_path = db_path )
288289
289- # Create resource_inventory_dict with names and icons
290- resource_inventory_dict = {
291- str ( item [ "resource_type" ]): {
292- ** item ,
293- "name " : resource_type_mapping . get ( str ( item [ "resource_type" ]), {}). get ( "name" , "Unknown Resource" ) ,
294- "icon " : resource_type_mapping . get ( str ( item [ "resource_type" ]), {}). get ( "icon" , "assets/icons/default.png" )
295- }
296- for item in resource_inventory
290+ # Timestamp
291+ timestamp = datetime . utcnow (). strftime ( "%Y-%m-%d %H:%M:%S UTC" )
292+
293+ metadata = {
294+ "cloud_service_provider " : cloud_service_provider ,
295+ "exit_strategy " : exit_strategy ,
296+ "assessment_type" : assessment_type ,
297+ "timestamp" : timestamp ,
297298 }
298299
299- # Prepare risk data
300- risks , severity_counts = get_risk_summary (risk_data , risk_definitions , resource_inventory_dict )
301-
302- # Prepare cost data
303- months , cost_values , total_cost , currency_symbol = get_cost_summary (cost_data )
304-
305- # Prepare resource data with names and icons
306- resource_counts = []
307- for resource_type , resource in resource_inventory_dict .items ():
308- count = resource .get ("count" , 0 )
309- resource_info = resource_type_mapping .get (str (resource_type ), {})
310- name = resource_info .get ("name" , "Unknown Resource" )
311- icon = resource_info .get ("icon" , "assets/icons/default.png" ).lstrip ('/' )
312-
313- resource_counts .append ({
314- "resource_type" : resource_type ,
315- "name" : name ,
316- "icon" : icon ,
317- "count" : count
318- })
319-
320- # Get the total count of all resources
321- total_resources = sum (item ["count" ] for item in resource_counts )
322-
323- # Prepare alternative technologies using the helper function
324- alternative_technologies_data = prepare_alternative_technologies (
325- resource_inventory ,
326- alternatives ,
327- alternative_technologies ,
328- exit_strategy
300+ # Generate Outputs
301+ reports = {}
302+
303+ # Generate HTML report
304+ reports ["HTML" ] = generate_html_report (
305+ report_path , metadata , resource_type_mapping , resource_inventory ,
306+ cost_data , risk_data , risk_definitions , alternatives , alternative_technologies , exit_strategy
329307 )
330308
331- # Render the HTML template
332- assessment_ts = datetime .utcnow ().strftime ("%Y-%m-%d %H:%M:%S UTC" )
333- assessment_type = assessment_type or "Not Specified"
334-
335- template_path = os .path .join ("assets" , "template" , "index.html" )
336- with open (template_path , 'r' ) as file :
337- template_content = file .read ()
338-
339- template = Template (template_content )
340- html_content = template .render (
341- cloud_service_provider = cloud_service_provider ,
342- exit_strategy = exit_strategy ,
343- assessment_type = assessment_type ,
344- assessment_ts = assessment_ts ,
345- risks = risks ,
346- high_risk_count = severity_counts ['high' ],
347- medium_risk_count = severity_counts ['medium' ],
348- low_risk_count = severity_counts ['low' ],
349- total_cost = total_cost ,
350- months_json = json .dumps (months ),
351- costs_json = json .dumps (cost_values ),
352- currency_symbol = currency_symbol ,
353- total_resources = total_resources ,
354- resource_inventory = resource_counts ,
355- alternative_technologies = alternative_technologies_data ,
309+ # Generate PDF report
310+ reports ["PDF" ] = generate_pdf_report (
311+ provider_details , report_path , metadata , resource_type_mapping , resource_inventory ,
312+ cost_data , risk_data , risk_definitions , alternatives , alternative_technologies , exit_strategy
356313 )
357314
358- # Save the generated report to a file
359- report_file_path = os .path .join (report_path , "index.html" )
360- with open (report_file_path , 'w' ) as report_file :
361- report_file .write (html_content )
315+ # Generate JSON report
316+ reports ["JSON" ] = generate_json_report (
317+ raw_data_path , metadata , resource_type_mapping , resource_inventory ,
318+ cost_data , risk_data , risk_definitions , alternatives , alternative_technologies , exit_strategy
319+ )
362320
363- return {"success" : True , "reports" : { "HTML" : report_file_path } }
321+ return {"success" : True , "reports" : reports }
364322
365323 except Exception as e :
366324 return {"success" : False , "logs" : f"Error generating report: { str (e )} " }
0 commit comments