Skip to content

Commit 68407dc

Browse files
authored
Add cable-html.py cable validation html converter
1 parent 09b120a commit 68407dc

File tree

1 file changed

+137
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)