|
| 1 | +``` |
| 2 | +import json |
| 3 | +import pandas as pd |
| 4 | +from datetime import datetime |
| 5 | +
|
| 6 | +now = datetime.now() # current date and time |
| 7 | +date_time = now.strftime("%m-%d-%Y-%H-%M") |
| 8 | +print("date and time:",date_time) |
| 9 | +
|
| 10 | +
|
| 11 | +# Get the file name as input from the user |
| 12 | +file_name = input("Please provide post validation json file: ") |
| 13 | +# Load the JSON data from the file |
| 14 | +with open(file_name, 'r') as f: |
| 15 | + data = json.load(f) |
| 16 | +
|
| 17 | +# Prepare a list to store the data |
| 18 | +data_list = [] |
| 19 | +
|
| 20 | +# Loop through each rack in the racks list |
| 21 | +for rack in data['racks']: |
| 22 | + # Loop through each device in the networkDevices list |
| 23 | + for device in rack['rackInfo']['networkConfiguration']['networkDevices']: |
| 24 | + # Loop through each interface map for the device |
| 25 | + for interface_map in device['fixedInterfaceMaps']: |
| 26 | + # Loop through each validation result for the interface map |
| 27 | + for validation_result in interface_map['validationResult']: |
| 28 | + # Append the data to the list |
| 29 | + temp_item = [device['name'], interface_map['name'], validation_result['status'], interface_map['destinationHostname'], interface_map['destinationPort'],validation_result['validationDetails']['deviceConfiguration'], validation_result['validationDetails']['error'] , validation_result['validationDetails']['reason'],'FixedInterface'] |
| 30 | + # print(temp_item) |
| 31 | + data_list.append(temp_item) |
| 32 | +
|
| 33 | + # Check if scaleSpecificInterfaceMaps is not None |
| 34 | + if device['scaleSpecificInterfaceMaps'] is not None: |
| 35 | + # Loop through each scaleSpecificInterface_Map for the interface map |
| 36 | + for scale_map in device['scaleSpecificInterfaceMaps']: |
| 37 | + # Loop through each interface map for the device. |
| 38 | + for interfacemaps in scale_map['InterfaceMaps']: |
| 39 | + # Loop through each validation result for the scaleSpecificInterface_Map |
| 40 | + for validation_result in interfacemaps['validationResult']: |
| 41 | + # Append the data to the list |
| 42 | + temp_item = [device['name'], interfacemaps['name'], validation_result['status'], interfacemaps['destinationHostname'], interfacemaps['destinationPort'], validation_result['validationDetails']['deviceConfiguration'], validation_result['validationDetails']['error'] , validation_result['validationDetails']['reason'], 'ScaleSpecificInterface'] |
| 43 | + # print(temp_item) |
| 44 | + data_list.append(temp_item) |
| 45 | +
|
| 46 | +# # Check if scaleSpecificInterfaceMaps is not None |
| 47 | +# if device['topologySpecificInterfaceMaps'] is not None: |
| 48 | +# # Loop through each scaleSpecificInterface_Map for the interface map |
| 49 | +# for topo_map in device['topologySpecificInterfaceMaps']: |
| 50 | +# # Loop through each interface map for the device. |
| 51 | +# for interfacemaps in topo_map['InterfaceMaps']: |
| 52 | +# # Loop through each validation result for the scaleSpecificInterface_Map |
| 53 | +# for validation_result in interfacemaps['validationResult']: |
| 54 | +# # Append the data to the list |
| 55 | +# data_list.append([device['name'], interfacemaps['name'], validation_result['status'], interface_map['destinationHostname'], interface_map['destinationPort'], validation_result['validationDetails']['deviceConfiguration'], validation_result['validationDetails']['error'] , validation_result['validationDetails']['reason'], 'TopologySpecificInterface']) |
| 56 | +
|
| 57 | +# Convert the list to a DataFrame |
| 58 | +df = pd.DataFrame(data_list, columns=['Device Name', 'Interface Map Name', 'Validation Result Status', 'Destination Hostname ', 'Destination Port ','Device Configuration', 'Error', 'Reason','Map Type']) |
| 59 | +
|
| 60 | +# Function to apply color based on validation result |
| 61 | +def color_status(val): |
| 62 | + color = 'red' if val == 'NonCompliant' else 'green' if val == 'Compliant' else 'black' |
| 63 | + return 'color: %s' % color |
| 64 | +
|
| 65 | +# Apply the color to the DataFrame |
| 66 | +styled_df = df.style.applymap(color_status, subset=['Validation Result Status']) |
| 67 | +
|
| 68 | +# Set CSS properties for th elements in dataframe |
| 69 | +th_props = [ |
| 70 | + ('font-size', '18px'), |
| 71 | + ('text-align', 'center'), |
| 72 | + ('font-weight', 'bold'), |
| 73 | + ('color', '#6d6d6d'), |
| 74 | + ('background-color', '#f7f7f9') |
| 75 | + ] |
| 76 | +
|
| 77 | +# Set CSS properties for td elements in dataframe |
| 78 | +td_props = [ |
| 79 | + ('font-size', '16px') |
| 80 | + ] |
| 81 | +
|
| 82 | +# Set table styles |
| 83 | +styles = [ |
| 84 | + dict(selector="th", props=th_props), |
| 85 | + dict(selector="td", props=td_props) |
| 86 | + ] |
| 87 | +
|
| 88 | +# Filter the DataFrame based on the 'Validation Result Status' column |
| 89 | +df_compliant = df[df['Validation Result Status'] == 'Compliant'] |
| 90 | +df_noncompliant = df[df['Validation Result Status'] == 'NonCompliant'] |
| 91 | +df_unknown = df[df['Validation Result Status'] == 'Unknown'] |
| 92 | +
|
| 93 | +# Apply the color to the DataFrames |
| 94 | +styled_df_compliant = df_compliant.style.applymap(color_status, subset=['Validation Result Status']) |
| 95 | +styled_df_noncompliant = df_noncompliant.style.applymap(color_status, subset=['Validation Result Status']) |
| 96 | +styled_df_unknown = df_unknown.style.applymap(color_status, subset=['Validation Result Status']) |
| 97 | +
|
| 98 | +# Generate the DataFrames' HTML strings |
| 99 | +df_html_compliant = styled_df_compliant.set_table_styles(styles).to_html() |
| 100 | +df_html_noncompliant = styled_df_noncompliant.set_table_styles(styles).to_html() |
| 101 | +df_html_unknown = styled_df_unknown.set_table_styles(styles).to_html() |
| 102 | +
|
| 103 | +# Combine the HTML strings |
| 104 | +html = f""" |
| 105 | +<html> |
| 106 | +<head> |
| 107 | + <style> |
| 108 | + body {{ |
| 109 | + background-color: #f0f0f5; |
| 110 | + }} |
| 111 | + .box {{ |
| 112 | + border: 1px solid black; |
| 113 | + margin: 10px; |
| 114 | + padding: 10px; |
| 115 | + }} |
| 116 | + </style> |
| 117 | +</head> |
| 118 | +<body> |
| 119 | + <div class="box"> |
| 120 | + <h2>Compliant</h2> |
| 121 | + {df_html_compliant} |
| 122 | + </div> |
| 123 | + <div class="box"> |
| 124 | + <h2>NonCompliant</h2> |
| 125 | + {df_html_noncompliant} |
| 126 | + </div> |
| 127 | + <div class="box"> |
| 128 | + <h2>Unknown</h2> |
| 129 | + {df_html_unknown} |
| 130 | + </div> |
| 131 | +</body> |
| 132 | +</html> |
| 133 | +""" |
| 134 | +
|
| 135 | +# Save the HTML string to a file |
| 136 | +with open("reports/report-{filename}.html".format(filename = date_time), 'w') as f: |
| 137 | + f.write(html) |
| 138 | +``` |
0 commit comments