Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -319,19 +319,29 @@ async def process_logs(file_obj, lab_site_filter, equipment_type_filter, date_ra
|
|
| 319 |
start_time = time.time()
|
| 320 |
try:
|
| 321 |
if not file_obj and not current_file_path:
|
|
|
|
| 322 |
return "No file uploaded.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None, last_modified_state, cached_df_state, cached_filtered_df_state, current_file_path
|
| 323 |
|
| 324 |
file_path = file_obj.name if file_obj else current_file_path
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
# Check if we can use cached filtered data
|
| 328 |
if (last_modified_state == current_modified_time and
|
| 329 |
cached_filtered_df_state is not None and
|
| 330 |
file_path == current_file_path):
|
|
|
|
| 331 |
filtered_df = cached_filtered_df_state
|
| 332 |
else:
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
| 335 |
if not file_path.endswith(".csv"):
|
| 336 |
return "Please upload a CSV file.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, "", "", "", None, last_modified_state, cached_df_state, cached_filtered_df_state, file_path
|
| 337 |
|
|
@@ -347,7 +357,7 @@ async def process_logs(file_obj, lab_site_filter, equipment_type_filter, date_ra
|
|
| 347 |
df = pd.read_csv(file_path, dtype=dtypes)
|
| 348 |
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 349 |
if missing_columns:
|
| 350 |
-
return f"Missing columns: {missing_columns}", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state,
|
| 351 |
|
| 352 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
|
| 353 |
df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
|
|
@@ -356,6 +366,7 @@ async def process_logs(file_obj, lab_site_filter, equipment_type_filter, date_ra
|
|
| 356 |
if df.empty:
|
| 357 |
return "No data available.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state, df, cached_filtered_df_state, file_path
|
| 358 |
else:
|
|
|
|
| 359 |
df = cached_df_state
|
| 360 |
|
| 361 |
# Apply filters
|
|
@@ -427,8 +438,9 @@ async def generate_pdf(summary, preview_html, usage_chart, device_cards, daily_l
|
|
| 427 |
return None
|
| 428 |
|
| 429 |
# Update filters
|
| 430 |
-
def update_filters(
|
| 431 |
-
if not
|
|
|
|
| 432 |
return gr.update(choices=['All'], value='All'), gr.update(choices=['All'], value='All')
|
| 433 |
try:
|
| 434 |
df = cached_df_state
|
|
@@ -510,9 +522,9 @@ try:
|
|
| 510 |
gr.Markdown("### Export Report")
|
| 511 |
pdf_output = gr.File(label="Download Status Report as PDF")
|
| 512 |
|
| 513 |
-
# Update filters when CSV is uploaded
|
| 514 |
file_input.change(
|
| 515 |
-
fn=lambda file_obj, cached_df, file_path: (file_obj.name if file_obj else file_path, cached_df, file_path),
|
| 516 |
inputs=[file_input, cached_df_state, current_file_path],
|
| 517 |
outputs=[current_file_path, cached_df_state, current_file_path],
|
| 518 |
queue=False
|
|
@@ -521,6 +533,10 @@ try:
|
|
| 521 |
inputs=[current_file_path, cached_df_state],
|
| 522 |
outputs=[lab_site_filter, equipment_type_filter],
|
| 523 |
queue=False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
)
|
| 525 |
|
| 526 |
# Process logs on submit or filter change
|
|
|
|
| 319 |
start_time = time.time()
|
| 320 |
try:
|
| 321 |
if not file_obj and not current_file_path:
|
| 322 |
+
logging.warning("No file provided or cached.")
|
| 323 |
return "No file uploaded.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None, last_modified_state, cached_df_state, cached_filtered_df_state, current_file_path
|
| 324 |
|
| 325 |
file_path = file_obj.name if file_obj else current_file_path
|
| 326 |
+
if not os.path.exists(file_path):
|
| 327 |
+
logging.error(f"File path does not exist: {file_path}")
|
| 328 |
+
return "File not found.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, "No anomalies detected.", "No AMC reminders.", "No insights generated.", None, last_modified_state, cached_df_state, cached_filtered_df_state, current_file_path
|
| 329 |
+
|
| 330 |
+
current_modified_time = os.path.getmtime(file_path)
|
| 331 |
+
df = None # Initialize df to avoid undefined reference
|
| 332 |
|
| 333 |
# Check if we can use cached filtered data
|
| 334 |
if (last_modified_state == current_modified_time and
|
| 335 |
cached_filtered_df_state is not None and
|
| 336 |
file_path == current_file_path):
|
| 337 |
+
logging.info("Using cached filtered DataFrame")
|
| 338 |
filtered_df = cached_filtered_df_state
|
| 339 |
else:
|
| 340 |
+
# Load or use cached raw DataFrame
|
| 341 |
+
if (cached_df_state is None or
|
| 342 |
+
current_modified_time != last_modified_state or
|
| 343 |
+
file_path != current_file_path):
|
| 344 |
+
logging.info(f"Reading new CSV file: {file_path}")
|
| 345 |
if not file_path.endswith(".csv"):
|
| 346 |
return "Please upload a CSV file.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, "", "", "", None, last_modified_state, cached_df_state, cached_filtered_df_state, file_path
|
| 347 |
|
|
|
|
| 357 |
df = pd.read_csv(file_path, dtype=dtypes)
|
| 358 |
missing_columns = [col for col in required_columns if col not in df.columns]
|
| 359 |
if missing_columns:
|
| 360 |
+
return f"Missing columns: {missing_columns}", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state, df, cached_filtered_df_state, file_path
|
| 361 |
|
| 362 |
df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
|
| 363 |
df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
|
|
|
|
| 366 |
if df.empty:
|
| 367 |
return "No data available.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state, df, cached_filtered_df_state, file_path
|
| 368 |
else:
|
| 369 |
+
logging.info("Using cached raw DataFrame")
|
| 370 |
df = cached_df_state
|
| 371 |
|
| 372 |
# Apply filters
|
|
|
|
| 438 |
return None
|
| 439 |
|
| 440 |
# Update filters
|
| 441 |
+
def update_filters(current_file_path, cached_df_state):
|
| 442 |
+
if not current_file_path or not cached_df_state:
|
| 443 |
+
logging.warning("No file or cached DataFrame available for filter update")
|
| 444 |
return gr.update(choices=['All'], value='All'), gr.update(choices=['All'], value='All')
|
| 445 |
try:
|
| 446 |
df = cached_df_state
|
|
|
|
| 522 |
gr.Markdown("### Export Report")
|
| 523 |
pdf_output = gr.File(label="Download Status Report as PDF")
|
| 524 |
|
| 525 |
+
# Update file path and filters when CSV is uploaded
|
| 526 |
file_input.change(
|
| 527 |
+
fn=lambda file_obj, cached_df, file_path: (file_obj.name if file_obj else file_path, cached_df, file_obj.name if file_obj else file_path),
|
| 528 |
inputs=[file_input, cached_df_state, current_file_path],
|
| 529 |
outputs=[current_file_path, cached_df_state, current_file_path],
|
| 530 |
queue=False
|
|
|
|
| 533 |
inputs=[current_file_path, cached_df_state],
|
| 534 |
outputs=[lab_site_filter, equipment_type_filter],
|
| 535 |
queue=False
|
| 536 |
+
).then(
|
| 537 |
+
fn=process_logs,
|
| 538 |
+
inputs=[file_input, lab_site_filter, equipment_type_filter, date_range_filter, last_modified_state, cached_df_state, cached_filtered_df_state, current_file_path],
|
| 539 |
+
outputs=[summary_output, preview_output, usage_chart_output, device_cards_output, daily_log_trends_output, weekly_uptime_output, anomaly_alerts_output, downtime_chart_output, anomaly_output, amc_output, insights_output, pdf_output, last_modified_state, cached_df_state, cached_filtered_df_state, current_file_path]
|
| 540 |
)
|
| 541 |
|
| 542 |
# Process logs on submit or filter change
|