Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,18 +35,38 @@ def load_logs(file_obj):
|
|
| 35 |
except Exception as e:
|
| 36 |
raise ValueError(f"Failed to load CSV: {e}")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# ----------------------------
|
| 39 |
# Summarize Log Data
|
| 40 |
# ----------------------------
|
| 41 |
def summarize_logs(df):
|
|
|
|
|
|
|
|
|
|
| 42 |
summary = df.groupby(['Lab', 'Type'])['Status'].value_counts().unstack().fillna(0)
|
| 43 |
return summary
|
| 44 |
|
| 45 |
# ----------------------------
|
| 46 |
# Generate Chart
|
| 47 |
# ----------------------------
|
| 48 |
-
def generate_chart(df):
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
fig, ax = plt.subplots(figsize=(8, 4))
|
| 51 |
summary.plot(kind='bar', stacked=True, ax=ax)
|
| 52 |
ax.set_title("Device Uptime/Downtime Summary")
|
|
@@ -59,8 +79,9 @@ def generate_chart(df):
|
|
| 59 |
# ----------------------------
|
| 60 |
# Export PDF using ReportLab
|
| 61 |
# ----------------------------
|
| 62 |
-
def export_pdf(df):
|
| 63 |
-
|
|
|
|
| 64 |
buffer = io.BytesIO()
|
| 65 |
pdf = canvas.Canvas(buffer, pagesize=letter)
|
| 66 |
width, height = letter
|
|
@@ -94,23 +115,39 @@ def export_pdf(df):
|
|
| 94 |
return ("LabOps_Summary.pdf", buffer.read())
|
| 95 |
|
| 96 |
# ----------------------------
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# ----------------------------
|
| 99 |
-
def dashboard(file_obj):
|
| 100 |
try:
|
| 101 |
df = load_logs(file_obj)
|
| 102 |
-
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
-
return
|
| 105 |
|
| 106 |
# ----------------------------
|
| 107 |
-
# PDF Generator
|
| 108 |
# ----------------------------
|
| 109 |
-
def generate_pdf_button(file_obj):
|
| 110 |
try:
|
| 111 |
df = load_logs(file_obj)
|
| 112 |
-
filename, pdf_bytes = export_pdf(df)
|
| 113 |
-
return gr.File.update(value=(filename, pdf_bytes), visible=True)
|
| 114 |
except Exception as e:
|
| 115 |
return gr.File.update(visible=False), gr.Textbox.update(value=f"❌ Error: {e}", visible=True)
|
| 116 |
|
|
@@ -123,13 +160,35 @@ with gr.Blocks() as demo:
|
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
file_input = gr.File(label="Upload Log CSV", file_types=[".csv"])
|
|
|
|
|
|
|
| 126 |
download_button = gr.Button("Download PDF Summary")
|
| 127 |
download_file = gr.File(label="Download PDF", visible=False)
|
| 128 |
|
| 129 |
plot_output = gr.Plot()
|
| 130 |
error_output = gr.Textbox(visible=False, label="Errors")
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
demo.launch()
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
raise ValueError(f"Failed to load CSV: {e}")
|
| 37 |
|
| 38 |
+
# ----------------------------
|
| 39 |
+
# Filter DataFrame by Lab and Type
|
| 40 |
+
# ----------------------------
|
| 41 |
+
def filter_df(df, selected_lab, selected_type):
|
| 42 |
+
filtered = df.copy()
|
| 43 |
+
if selected_lab and selected_lab != "All":
|
| 44 |
+
filtered = filtered[filtered['Lab'] == selected_lab]
|
| 45 |
+
if selected_type and selected_type != "All":
|
| 46 |
+
filtered = filtered[filtered['Type'] == selected_type]
|
| 47 |
+
return filtered
|
| 48 |
+
|
| 49 |
# ----------------------------
|
| 50 |
# Summarize Log Data
|
| 51 |
# ----------------------------
|
| 52 |
def summarize_logs(df):
|
| 53 |
+
if df.empty:
|
| 54 |
+
# Return empty DataFrame with expected columns for plotting
|
| 55 |
+
return pd.DataFrame(columns=['OK', 'DOWN'])
|
| 56 |
summary = df.groupby(['Lab', 'Type'])['Status'].value_counts().unstack().fillna(0)
|
| 57 |
return summary
|
| 58 |
|
| 59 |
# ----------------------------
|
| 60 |
# Generate Chart
|
| 61 |
# ----------------------------
|
| 62 |
+
def generate_chart(df, selected_lab, selected_type):
|
| 63 |
+
filtered = filter_df(df, selected_lab, selected_type)
|
| 64 |
+
summary = summarize_logs(filtered)
|
| 65 |
+
if summary.empty:
|
| 66 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 67 |
+
ax.text(0.5, 0.5, 'No data for selected filters', ha='center', va='center', fontsize=14)
|
| 68 |
+
ax.axis('off')
|
| 69 |
+
return fig
|
| 70 |
fig, ax = plt.subplots(figsize=(8, 4))
|
| 71 |
summary.plot(kind='bar', stacked=True, ax=ax)
|
| 72 |
ax.set_title("Device Uptime/Downtime Summary")
|
|
|
|
| 79 |
# ----------------------------
|
| 80 |
# Export PDF using ReportLab
|
| 81 |
# ----------------------------
|
| 82 |
+
def export_pdf(df, selected_lab, selected_type):
|
| 83 |
+
filtered = filter_df(df, selected_lab, selected_type)
|
| 84 |
+
summary = summarize_logs(filtered)
|
| 85 |
buffer = io.BytesIO()
|
| 86 |
pdf = canvas.Canvas(buffer, pagesize=letter)
|
| 87 |
width, height = letter
|
|
|
|
| 115 |
return ("LabOps_Summary.pdf", buffer.read())
|
| 116 |
|
| 117 |
# ----------------------------
|
| 118 |
+
# Update dropdown options based on uploaded file
|
| 119 |
+
# ----------------------------
|
| 120 |
+
def update_dropdown_options(file_obj):
|
| 121 |
+
try:
|
| 122 |
+
df = load_logs(file_obj)
|
| 123 |
+
labs = sorted(df['Lab'].dropna().unique().tolist())
|
| 124 |
+
types = sorted(df['Type'].dropna().unique().tolist())
|
| 125 |
+
# Add "All" option for no filtering
|
| 126 |
+
labs = ["All"] + labs
|
| 127 |
+
types = ["All"] + types
|
| 128 |
+
return labs, types, gr.update(visible=True)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
return ["All"], ["All"], gr.update(visible=False)
|
| 131 |
+
|
| 132 |
+
# ----------------------------
|
| 133 |
+
# Dashboard Display with filters
|
| 134 |
# ----------------------------
|
| 135 |
+
def dashboard(file_obj, selected_lab, selected_type):
|
| 136 |
try:
|
| 137 |
df = load_logs(file_obj)
|
| 138 |
+
fig = generate_chart(df, selected_lab, selected_type)
|
| 139 |
+
return fig, gr.update(visible=False) # Hide error
|
| 140 |
except Exception as e:
|
| 141 |
+
return None, gr.update(value=f"❌ Error: {e}", visible=True)
|
| 142 |
|
| 143 |
# ----------------------------
|
| 144 |
+
# PDF Generator with filters
|
| 145 |
# ----------------------------
|
| 146 |
+
def generate_pdf_button(file_obj, selected_lab, selected_type):
|
| 147 |
try:
|
| 148 |
df = load_logs(file_obj)
|
| 149 |
+
filename, pdf_bytes = export_pdf(df, selected_lab, selected_type)
|
| 150 |
+
return gr.File.update(value=(filename, pdf_bytes), visible=True), gr.Textbox.update(visible=False)
|
| 151 |
except Exception as e:
|
| 152 |
return gr.File.update(visible=False), gr.Textbox.update(value=f"❌ Error: {e}", visible=True)
|
| 153 |
|
|
|
|
| 160 |
|
| 161 |
with gr.Row():
|
| 162 |
file_input = gr.File(label="Upload Log CSV", file_types=[".csv"])
|
| 163 |
+
lab_dropdown = gr.Dropdown(label="Select Lab", choices=["All"], value="All", interactive=True)
|
| 164 |
+
type_dropdown = gr.Dropdown(label="Select Equipment Type", choices=["All"], value="All", interactive=True)
|
| 165 |
download_button = gr.Button("Download PDF Summary")
|
| 166 |
download_file = gr.File(label="Download PDF", visible=False)
|
| 167 |
|
| 168 |
plot_output = gr.Plot()
|
| 169 |
error_output = gr.Textbox(visible=False, label="Errors")
|
| 170 |
|
| 171 |
+
# Update dropdowns on file upload
|
| 172 |
+
file_input.change(
|
| 173 |
+
fn=update_dropdown_options,
|
| 174 |
+
inputs=file_input,
|
| 175 |
+
outputs=[lab_dropdown, type_dropdown, error_output]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Update plot when file or dropdowns change
|
| 179 |
+
inputs_for_dashboard = [file_input, lab_dropdown, type_dropdown]
|
| 180 |
+
for input_component in inputs_for_dashboard:
|
| 181 |
+
input_component.change(
|
| 182 |
+
fn=dashboard,
|
| 183 |
+
inputs=inputs_for_dashboard,
|
| 184 |
+
outputs=[plot_output, error_output]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Generate PDF on button click
|
| 188 |
+
download_button.click(
|
| 189 |
+
fn=generate_pdf_button,
|
| 190 |
+
inputs=[file_input, lab_dropdown, type_dropdown],
|
| 191 |
+
outputs=[download_file, error_output]
|
| 192 |
+
)
|
| 193 |
|
| 194 |
demo.launch()
|