MaheshP98 commited on
Commit
ca1a7bb
·
verified ·
1 Parent(s): b204664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +480 -480
app.py CHANGED
@@ -16,531 +16,531 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
16
 
17
  # Try to import reportlab
18
  try:
19
- from reportlab.lib.pagesizes import letter
20
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
21
- from reportlab.lib.styles import getSampleStyleSheet
22
- from reportlab.lib import colors
23
- reportlab_available = True
24
- logging.info("reportlab module successfully imported")
25
  except ImportError:
26
- logging.warning("reportlab module not found. PDF generation disabled.")
27
- reportlab_available = False
28
 
29
  # Summarize logs
30
  def summarize_logs(df):
31
- try:
32
- total_devices = df["device_id"].nunique()
33
- total_usage = df["usage_hours"].sum() if "usage_hours" in df.columns else 0
34
- return f"{total_devices} devices processed with {total_usage:.2f} total usage hours."
35
- except Exception as e:
36
- logging.error(f"Summary generation failed: {str(e)}")
37
- return "Failed to generate summary."
38
 
39
  # Anomaly detection
40
  def detect_anomalies(df):
41
- try:
42
- if "usage_hours" not in df.columns or "downtime" not in df.columns:
43
- return "Anomaly detection requires 'usage_hours' and 'downtime' columns.", pd.DataFrame()
44
- features = df[["usage_hours", "downtime"]].fillna(0)
45
- if len(features) > 50:
46
- features = features.sample(n=50, random_state=42)
47
- iso_forest = IsolationForest(contamination=0.1, random_state=42)
48
- df["anomaly"] = iso_forest.fit_predict(features)
49
- anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
50
- if anomalies.empty:
51
- return "No anomalies detected.", anomalies
52
- return "\n".join([f"- Device ID: {row['device_id']}, Usage: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}" for _, row in anomalies.head(5).iterrows()]), anomalies
53
- except Exception as e:
54
- logging.error(f"Anomaly detection failed: {str(e)}")
55
- return f"Anomaly detection failed: {str(e)}", pd.DataFrame()
56
-
57
- # AMC reminders
58
  def check_amc_reminders(df, current_date):
59
- try:
60
- if "device_id" not in df.columns or "amc_date" not in df.columns:
61
- return "AMC reminders require 'device_id' and 'amc_date' columns.", pd.DataFrame()
62
- df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
63
- current_date = pd.to_datetime(current_date)
64
- df["days_to_amc"] = (df["amc_date"] - current_date).dt.days
65
- reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]]
66
- if reminders.empty:
67
- return "No AMC reminders due within the next 30 days.", reminders
68
- return "\n".join([f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}" for _, row in reminders.head(5).iterrows()]), reminders
69
- except Exception as e:
70
- logging.error(f"AMC reminder generation failed: {str(e)}")
71
- return f"AMC reminder generation failed: {str(e)}", pd.DataFrame()
72
 
73
  # Dashboard insights
74
  def generate_dashboard_insights(df):
75
- try:
76
- total_devices = df["device_id"].nunique()
77
- avg_usage = df["usage_hours"].mean() if "usage_hours" in df.columns else 0
78
- return f"{total_devices} devices with average usage of {avg_usage:.2f} hours."
79
- except Exception as e:
80
- logging.error(f"Dashboard insights generation failed: {str(e)}")
81
- return "Failed to generate insights."
82
 
83
  # Placeholder chart for empty data
84
  def create_placeholder_chart(title):
85
- fig = go.Figure()
86
- fig.add_annotation(
87
- text="No data available for this chart",
88
- xref="paper", yref="paper",
89
- x=0.5, y=0.5, showarrow=False,
90
- font=dict(size=16)
91
- )
92
- fig.update_layout(title=title, margin=dict(l=20, r=20, t=40, b=20))
93
- return fig
94
 
95
  # Create usage chart
96
  def create_usage_chart(df):
97
- try:
98
- if df.empty or "usage_hours" not in df.columns or "device_id" not in df.columns:
99
- logging.warning("Insufficient data for usage chart")
100
- return create_placeholder_chart("Usage Hours per Device")
101
- usage_data = df.groupby("device_id")["usage_hours"].sum().reset_index()
102
- if len(usage_data) > 5:
103
- usage_data = usage_data.nlargest(5, "usage_hours")
104
- fig = px.bar(
105
- usage_data,
106
- x="device_id",
107
- y="usage_hours",
108
- title="Usage Hours per Device",
109
- labels={"device_id": "Device ID", "usage_hours": "Usage Hours"}
110
- )
111
- fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
112
- return fig
113
- except Exception as e:
114
- logging.error(f"Failed to create usage chart: {str(e)}")
115
- return create_placeholder_chart("Usage Hours per Device")
116
 
117
  # Create downtime chart
118
  def create_downtime_chart(df):
119
- try:
120
- if df.empty or "downtime" not in df.columns or "device_id" not in df.columns:
121
- logging.warning("Insufficient data for downtime chart")
122
- return create_placeholder_chart("Downtime per Device")
123
- downtime_data = df.groupby("device_id")["downtime"].sum().reset_index()
124
- if len(downtime_data) > 5:
125
- downtime_data = downtime_data.nlargest(5, "downtime")
126
- fig = px.bar(
127
- downtime_data,
128
- x="device_id",
129
- y="downtime",
130
- title="Downtime per Device",
131
- labels={"device_id": "Device ID", "downtime": "Downtime (Hours)"}
132
- )
133
- fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
134
- return fig
135
- except Exception as e:
136
- logging.error(f"Failed to create downtime chart: {str(e)}")
137
- return create_placeholder_chart("Downtime per Device")
138
 
139
  # Create daily log trends chart
140
  def create_daily_log_trends_chart(df):
141
- try:
142
- if df.empty or "timestamp" not in df.columns:
143
- logging.warning("Insufficient data for daily log trends chart")
144
- return create_placeholder_chart("Daily Log Trends")
145
- df['date'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.date
146
- daily_logs = df.groupby('date').size().reset_index(name='log_count')
147
- if daily_logs.empty:
148
- return create_placeholder_chart("Daily Log Trends")
149
- fig = px.line(
150
- daily_logs,
151
- x='date',
152
- y='log_count',
153
- title="Daily Log Trends",
154
- labels={"date": "Date", "log_count": "Number of Logs"}
155
- )
156
- fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
157
- return fig
158
- except Exception as e:
159
- logging.error(f"Failed to create daily log trends chart: {str(e)}")
160
- return create_placeholder_chart("Daily Log Trends")
161
 
162
  # Create weekly uptime chart
163
  def create_weekly_uptime_chart(df):
164
- try:
165
- if df.empty or "timestamp" not in df.columns or "usage_hours" not in df.columns or "downtime" not in df.columns:
166
- logging.warning("Insufficient data for weekly uptime chart")
167
- return create_placeholder_chart("Weekly Uptime Percentage")
168
- df['week'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.isocalendar().week
169
- df['year'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.year
170
- weekly_data = df.groupby(['year', 'week']).agg({
171
- 'usage_hours': 'sum',
172
- 'downtime': 'sum'
173
- }).reset_index()
174
- weekly_data['uptime_percent'] = (weekly_data['usage_hours'] / (weekly_data['usage_hours'] + weekly_data['downtime'])) * 100
175
- weekly_data['year_week'] = weekly_data['year'].astype(str) + '-W' + weekly_data['week'].astype(str)
176
- if weekly_data.empty:
177
- return create_placeholder_chart("Weekly Uptime Percentage")
178
- fig = px.bar(
179
- weekly_data,
180
- x='year_week',
181
- y='uptime_percent',
182
- title="Weekly Uptime Percentage",
183
- labels={"year_week": "Year-Week", "uptime_percent": "Uptime %"}
184
- )
185
- fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
186
- return fig
187
- except Exception as e:
188
- logging.error(f"Failed to create weekly uptime chart: {str(e)}")
189
- return create_placeholder_chart("Weekly Uptime Percentage")
190
 
191
  # Create anomaly alerts chart
192
  def create_anomaly_alerts_chart(anomalies_df):
193
- try:
194
- if anomalies_df is None or anomalies_df.empty or "timestamp" not in anomalies_df.columns:
195
- logging.warning("Insufficient data for anomaly alerts chart")
196
- return create_placeholder_chart("Anomaly Alerts Over Time")
197
- anomalies_df['date'] = pd.to_datetime(anomalies_df['timestamp'], errors='coerce').dt.date
198
- anomaly_counts = anomalies_df.groupby('date').size().reset_index(name='anomaly_count')
199
- if anomaly_counts.empty:
200
- return create_placeholder_chart("Anomaly Alerts Over Time")
201
- fig = px.scatter(
202
- anomaly_counts,
203
- x='date',
204
- y='anomaly_count',
205
- title="Anomaly Alerts Over Time",
206
- labels={"date": "Date", "anomaly_count": "Number of Anomalies"}
207
- )
208
- fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
209
- return fig
210
- except Exception as e:
211
- logging.error(f"Failed to create anomaly alerts chart: {str(e)}")
212
- return create_placeholder_chart("Anomaly Alerts Over Time")
213
 
214
  # Generate device cards
215
  def generate_device_cards(df):
216
- try:
217
- if df.empty:
218
- return '<p>No devices available to display.</p>'
219
- device_stats = df.groupby('device_id').agg({
220
- 'status': 'last',
221
- 'timestamp': 'max',
222
- }).reset_index()
223
- device_stats['count'] = df.groupby('device_id').size().reindex(device_stats['device_id']).values
224
- device_stats['health'] = device_stats['status'].map({
225
- 'Active': 'Healthy',
226
- 'Inactive': 'Unhealthy',
227
- 'Pending': 'Warning'
228
- }).fillna('Unknown')
229
- cards_html = '<div style="display: flex; flex-wrap: wrap; gap: 20px;">'
230
- for _, row in device_stats.iterrows():
231
- health_color = {'Healthy': 'green', 'Unhealthy': 'red', 'Warning': 'orange', 'Unknown': 'gray'}.get(row['health'], 'gray')
232
- timestamp_str = str(row['timestamp']) if pd.notna(row['timestamp']) else 'Unknown'
233
- cards_html += f"""
234
- <div style="border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px; width: 200px;">
235
- <h4>Device: {row['device_id']}</h4>
236
- <p><b>Health:</b> <span style="color: {health_color}">{row['health']}</span></p>
237
- <p><b>Usage Count:</b> {row['count']}</p>
238
- <p><b>Last Log:</b> {timestamp_str}</p>
239
- </div>
240
- """
241
- cards_html += '</div>'
242
- return cards_html
243
- except Exception as e:
244
- logging.error(f"Failed to generate device cards: {str(e)}")
245
- return f'<p>Error generating device cards: {str(e)}</p>'
246
 
247
  # Generate PDF content
248
  def generate_pdf_content(summary, preview_df, anomalies, amc_reminders, insights, device_cards_html, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart):
249
- if not reportlab_available:
250
- return None
251
- try:
252
- pdf_path = f"status_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
253
- doc = SimpleDocTemplate(pdf_path, pagesize=letter)
254
- styles = getSampleStyleSheet()
255
- story = []
256
-
257
- def safe_paragraph(text, style):
258
- return Paragraph(str(text).replace('\n', '<br/>'), style) if text else Paragraph("", style)
259
-
260
- story.append(Paragraph("LabOps Status Report", styles['Title']))
261
- story.append(Paragraph(f"Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", styles['Normal']))
262
- story.append(Spacer(1, 12))
263
-
264
- story.append(Paragraph("Summary Report", styles['Heading2']))
265
- story.append(safe_paragraph(summary, styles['Normal']))
266
- story.append(Spacer(1, 12))
267
-
268
- story.append(Paragraph("Log Preview", styles['Heading2']))
269
- if not preview_df.empty:
270
- data = [preview_df.columns.tolist()] + preview_df.head(5).values.tolist()
271
- table = Table(data)
272
- table.setStyle(TableStyle([
273
- ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
274
- ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
275
- ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
276
- ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
277
- ('FONTSIZE', (0, 0), (-1, 0), 12),
278
- ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
279
- ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
280
- ('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
281
- ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
282
- ('FONTSIZE', (0, 1), (-1, -1), 10),
283
- ('GRID', (0, 0), (-1, -1), 1, colors.black)
284
- ]))
285
- story.append(table)
286
- else:
287
- story.append(safe_paragraph("No preview available.", styles['Normal']))
288
- story.append(Spacer(1, 12))
289
-
290
- story.append(Paragraph("Device Cards", styles['Heading2']))
291
- device_cards_text = device_cards_html.replace('<div>', '').replace('</div>', '\n').replace('<h4>', '').replace('</h4>', '\n').replace('<p>', '').replace('</p>', '\n').replace('<b>', '').replace('</b>', '').replace('<span style="color: green">', '').replace('<span style="color: red">', '').replace('<span style="color: orange">', '').replace('<span style="color: gray">', '').replace('</span>', '')
292
- story.append(safe_paragraph(device_cards_text, styles['Normal']))
293
- story.append(Spacer(1, 12))
294
-
295
- story.append(Paragraph("Anomaly Detection", styles['Heading2']))
296
- story.append(safe_paragraph(anomalies, styles['Normal']))
297
- story.append(Spacer(1, 12))
298
-
299
- story.append(Paragraph("AMC Reminders", styles['Heading2']))
300
- story.append(safe_paragraph(amc_reminders, styles['Normal']))
301
- story.append(Spacer(1, 12))
302
-
303
- story.append(Paragraph("Dashboard Insights", styles['Heading2']))
304
- story.append(safe_paragraph(insights, styles['Normal']))
305
- story.append(Spacer(1, 12))
306
-
307
- story.append(Paragraph("Charts", styles['Heading2']))
308
- story.append(Paragraph("[Chart placeholders - see dashboard for visuals]", styles['Normal']))
309
-
310
- doc.build(story)
311
- logging.info(f"PDF generated at {pdf_path}")
312
- return pdf_path
313
- except Exception as e:
314
- logging.error(f"Failed to generate PDF: {str(e)}")
315
- return None
316
-
317
- # Main processing function (Updated)
318
  async def process_logs(file_obj, lab_site_filter, equipment_type_filter, date_range, last_modified_state, cached_df_state, cached_filtered_df_state):
319
- start_time = time.time()
320
- try:
321
- if not file_obj:
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, None
323
-
324
- file_path = file_obj.name
325
- current_modified_time = os.path.getmtime(file_path)
326
-
327
- # Load or use cached original dataframe
328
- if cached_df_state is None or current_modified_time != last_modified_state:
329
- logging.info(f"Processing file: {file_path}")
330
- if not file_path.endswith(".csv"):
331
- 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, None
332
-
333
- required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
334
- dtypes = {
335
- "device_id": "string",
336
- "log_type": "string",
337
- "status": "string",
338
- "usage_hours": "float32",
339
- "downtime": "float32",
340
- "amc_date": "string"
341
- }
342
- df = pd.read_csv(file_path, dtype=dtypes)
343
- missing_columns = [col for col in required_columns if col not in df.columns]
344
- if missing_columns:
345
- 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, cached_df_state, None
346
-
347
- df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
348
- df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
349
- if df["timestamp"].dt.tz is None:
350
- df["timestamp"] = df["timestamp"].dt.tz_localize('UTC').dt.tz_convert('Asia/Kolkata')
351
- if df.empty:
352
- 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, None
353
- cached_df_state = df
354
- else:
355
- df = cached_df_state
356
-
357
- # Always apply current filters to the original dataframe
358
- filtered_df = df.copy()
359
- if lab_site_filter and lab_site_filter != 'All' and 'lab_site' in filtered_df.columns:
360
- filtered_df = filtered_df[filtered_df['lab_site'] == lab_site_filter]
361
- if equipment_type_filter and equipment_type_filter != 'All' and 'equipment_type' in filtered_df.columns:
362
- filtered_df = filtered_df[filtered_df['equipment_type'] == equipment_type_filter]
363
- if date_range and len(date_range) == 2:
364
- days_start, days_end = date_range
365
- today = pd.to_datetime(datetime.now().date()).tz_localize('Asia/Kolkata')
366
- start_date = today + pd.Timedelta(days=days_start)
367
- end_date = today + pd.Timedelta(days=days_end) + pd.Timedelta(days=1) - pd.Timedelta(seconds=1)
368
- filtered_df = filtered_df[(filtered_df['timestamp'] >= start_date) & (filtered_df['timestamp'] <= end_date)]
369
-
370
- if filtered_df.empty:
371
- return "No data after applying filters.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state, cached_df_state, None
372
-
373
- # Generate table for preview
374
- preview_df = filtered_df[['device_id', 'log_type', 'status', 'timestamp', 'usage_hours', 'downtime', 'amc_date']].head(5)
375
- preview_html = preview_df.to_html(index=False, classes='table table-striped', border=0)
376
-
377
- # Run critical tasks concurrently
378
- try:
379
- with ThreadPoolExecutor(max_workers=2) as executor:
380
- future_anomalies = executor.submit(detect_anomalies, filtered_df)
381
- future_amc = executor.submit(check_amc_reminders, filtered_df, datetime.now())
382
-
383
- summary = f"Step 1: Summary Report\n{summarize_logs(filtered_df)}"
384
- anomalies, anomalies_df = future_anomalies.result()
385
- anomalies = f"Anomaly Detection\n{anomalies}"
386
- amc_reminders, reminders_df = future_amc.result()
387
- amc_reminders = f"AMC Reminders\n{amc_reminders}"
388
- insights = f"Dashboard Insights\n{generate_dashboard_insights(filtered_df)}"
389
- except Exception as e:
390
- logging.error(f"Concurrent task execution failed: {str(e)}")
391
- summary = "Failed to generate summary due to processing error."
392
- anomalies = "Anomaly detection failed due to processing error."
393
- amc_reminders = "AMC reminders failed due to processing error."
394
- insights = "Insights generation failed due to processing error."
395
- anomalies_df = pd.DataFrame()
396
-
397
- # Generate charts sequentially
398
- usage_chart = create_usage_chart(filtered_df)
399
- downtime_chart = create_downtime_chart(filtered_df)
400
- daily_log_chart = create_daily_log_trends_chart(filtered_df)
401
- weekly_uptime_chart = create_weekly_uptime_chart(filtered_df)
402
- anomaly_alerts_chart = create_anomaly_alerts_chart(anomalies_df)
403
- device_cards = generate_device_cards(filtered_df)
404
-
405
- elapsed_time = time.time() - start_time
406
- logging.info(f"Processing completed in {elapsed_time:.2f} seconds")
407
- if elapsed_time > 3:
408
- logging.warning(f"Processing time exceeded 3 seconds: {elapsed_time:.2f} seconds")
409
-
410
- return (summary, preview_html, usage_chart, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart, anomalies, amc_reminders, insights, None, current_modified_time, cached_df_state, filtered_df)
411
- except Exception as e:
412
- logging.error(f"Failed to process file: {str(e)}")
413
- return f"Error: {str(e)}", pd.DataFrame(), None, '<p>Error processing data.</p>', None, None, None, None, None, None, None, None, last_modified_state, cached_df_state, None
414
 
415
  # Generate PDF separately
416
  async def generate_pdf(summary, preview_html, usage_chart, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart, anomalies, amc_reminders, insights):
417
- try:
418
- preview_df = pd.read_html(preview_html)[0]
419
- pdf_file = generate_pdf_content(summary, preview_df, anomalies, amc_reminders, insights, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart)
420
- return pdf_file
421
- except Exception as e:
422
- logging.error(f"Failed to generate PDF: {str(e)}")
423
- return None
424
 
425
  # Update filters
426
  def update_filters(file_obj, current_file_state):
427
- if not file_obj or file_obj.name == current_file_state:
428
- return gr.update(), gr.update(), current_file_state
429
- try:
430
- with open(file_obj.name, 'rb') as f:
431
- csv_content = f.read().decode('utf-8')
432
- df = pd.read_csv(io.StringIO(csv_content))
433
- df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
434
-
435
- lab_site_options = ['All'] + [site for site in df['lab_site'].dropna().astype(str).unique().tolist() if site.strip()] if 'lab_site' in df.columns else ['All']
436
- equipment_type_options = ['All'] + [equip for equip in df['equipment_type'].dropna().astype(str).unique().tolist() if equip.strip()] if 'equipment_type' in df.columns else ['All']
437
-
438
- return gr.update(choices=lab_site_options, value='All'), gr.update(choices=equipment_type_options, value='All'), file_obj.name
439
- except Exception as e:
440
- logging.error(f"Failed to update filters: {str(e)}")
441
- return gr.update(choices=['All'], value='All'), gr.update(choices=['All'], value='All'), current_file_state
442
 
443
  # Gradio Interface
444
  try:
445
- logging.info("Initializing Gradio interface...")
446
- with gr.Blocks(css="""
447
- .dashboard-container {border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px;}
448
- .dashboard-title {font-size: 24px; font-weight: bold; margin-bottom: 5px;}
449
- .dashboard-section {margin-bottom: 20px;}
450
- .dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
451
- .dashboard-section p {margin: 1px 0; line-height: 1.2;}
452
- .dashboard-section ul {margin: 2px 0; padding-left: 20px;}
453
- .table {width: 100%; border-collapse: collapse;}
454
- .table th, .table td {border: 1px solid #ddd; padding: 8px; text-align: left;}
455
- .table th {background-color: #f2f2f2;}
456
- .table tr:nth-child(even) {background-color: #f9f9f9;}
457
- """) as iface:
458
- gr.Markdown("<h1>LabOps Log Analyzer Dashboard</h1>")
459
- gr.Markdown("Upload a CSV file to analyze. Click 'Analyze' to refresh the dashboard. Use 'Export PDF' for report download.")
460
-
461
- last_modified_state = gr.State(value=None)
462
- current_file_state = gr.State(value=None)
463
- cached_df_state = gr.State(value=None)
464
- cached_filtered_df_state = gr.State(value=None)
465
-
466
- with gr.Row():
467
- with gr.Column(scale=1):
468
- file_input = gr.File(label="Upload Logs (CSV)", file_types=[".csv"])
469
- with gr.Group():
470
- gr.Markdown("### Filters")
471
- lab_site_filter = gr.Dropdown(label="Lab Site", choices=['All'], value='All', interactive=True)
472
- equipment_type_filter = gr.Dropdown(label="Equipment Type", choices=['All'], value='All', interactive=True)
473
- date_range_filter = gr.Slider(label="Date Range (Days from Today)", minimum=-365, maximum=0, step=1, value=[-30, 0])
474
- submit_button = gr.Button("Analyze", variant="primary")
475
- pdf_button = gr.Button("Export PDF", variant="secondary")
476
-
477
- with gr.Column(scale=2):
478
- with gr.Group(elem_classes="dashboard-container"):
479
- gr.Markdown("<div class='dashboard-title'>Analysis Results</div>")
480
- with gr.Group(elem_classes="dashboard-section"):
481
- gr.Markdown("### Step 1: Summary Report")
482
- summary_output = gr.Markdown()
483
- with gr.Group(elem_classes="dashboard-section"):
484
- gr.Markdown("### Step 2: Log Preview")
485
- preview_output = gr.HTML()
486
- with gr.Group(elem_classes="dashboard-section"):
487
- gr.Markdown("### Device Cards")
488
- device_cards_output = gr.HTML()
489
- with gr.Group(elem_classes="dashboard-section"):
490
- gr.Markdown("### Charts")
491
- with gr.Tab("Usage Hours per Device"):
492
- usage_chart_output = gr.Plot()
493
- with gr.Tab("Downtime per Device"):
494
- downtime_chart_output = gr.Plot()
495
- with gr.Tab("Daily Log Trends"):
496
- daily_log_trends_output = gr.Plot()
497
- with gr.Tab("Weekly Uptime Percentage"):
498
- weekly_uptime_output = gr.Plot()
499
- with gr.Tab("Anomaly Alerts"):
500
- anomaly_alerts_output = gr.Plot()
501
- with gr.Group(elem_classes="dashboard-section"):
502
- gr.Markdown("### Step 4: Anomaly Detection")
503
- anomaly_output = gr.Markdown()
504
- with gr.Group(elem_classes="dashboard-section"):
505
- gr.Markdown("### Step 5: AMC Reminders")
506
- amc_output = gr.Markdown()
507
- with gr.Group(elem_classes="dashboard-section"):
508
- gr.Markdown("### Step 6: Insights")
509
- insights_output = gr.Markdown()
510
- with gr.Group(elem_classes="dashboard-section"):
511
- gr.Markdown("### Export Report")
512
- pdf_output = gr.File(label="Download Status Report as PDF")
513
-
514
- file_input.change(
515
- fn=update_filters,
516
- inputs=[file_input, current_file_state],
517
- outputs=[lab_site_filter, equipment_type_filter, current_file_state],
518
- queue=False
519
- )
520
-
521
- submit_button.click(
522
- fn=process_logs,
523
- inputs=[file_input, lab_site_filter, equipment_type_filter, date_range_filter, last_modified_state, cached_df_state, cached_filtered_df_state],
524
- 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]
525
- )
526
-
527
- pdf_button.click(
528
- fn=generate_pdf,
529
- inputs=[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],
530
- outputs=[pdf_output]
531
- )
532
-
533
- logging.info("Gradio interface initialized successfully")
534
  except Exception as e:
535
- logging.error(f"Failed to initialize Gradio interface: {str(e)}")
536
- raise e
537
 
538
  if __name__ == "__main__":
539
- try:
540
- logging.info("Launching Gradio interface...")
541
- iface.launch(server_name="0.0.0.0", server_port=7860, debug=True, share=False)
542
- logging.info("Gradio interface launched successfully")
543
- except Exception as e:
544
- logging.error(f"Failed to launch Gradio interface: {str(e)}")
545
- print(f"Error launching app: {str(e)}")
546
- raise e
 
16
 
17
  # Try to import reportlab
18
  try:
19
+ from reportlab.lib.pagesizes import letter
20
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
21
+ from reportlab.lib.styles import getSampleStyleSheet
22
+ from reportlab.lib import colors
23
+ reportlab_available = True
24
+ logging.info("reportlab module successfully imported")
25
  except ImportError:
26
+ logging.warning("reportlab module not found. PDF generation disabled.")
27
+ reportlab_available = False
28
 
29
  # Summarize logs
30
  def summarize_logs(df):
31
+ try:
32
+ total_devices = df["device_id"].nunique()
33
+ total_usage = df["usage_hours"].sum() if "usage_hours" in df.columns else 0
34
+ return f"{total_devices} devices processed with {total_usage:.2f} total usage hours."
35
+ except Exception as e:
36
+ logging.error(f"Summary generation failed: {str(e)}")
37
+ return "Failed to generate summary."
38
 
39
  # Anomaly detection
40
  def detect_anomalies(df):
41
+ try:
42
+ if "usage_hours" not in df.columns or "downtime" not in df.columns:
43
+ return "Anomaly detection requires 'usage_hours' and 'downtime' columns.", pd.DataFrame()
44
+ features = df[["usage_hours", "downtime"]].fillna(0)
45
+ if len(features) > 50:
46
+ features = features.sample(n=50, random_state=42)
47
+ iso_forest = IsolationForest(contamination=0.1, random_state=42)
48
+ df["anomaly"] = iso_forest.fit_predict(features)
49
+ anomalies = df[df["anomaly"] == -1][["device_id", "usage_hours", "downtime", "timestamp"]]
50
+ if anomalies.empty:
51
+ return "No anomalies detected.", anomalies
52
+ return "\n".join([f"- Device ID: {row['device_id']}, Usage: {row['usage_hours']}, Downtime: {row['downtime']}, Timestamp: {row['timestamp']}" for _, row in anomalies.head(5).iterrows()]), anomalies
53
+ except Exception as e:
54
+ logging.error(f"Anomaly detection failed: {str(e)}")
55
+ return f"Anomaly detection failed: {str(e)}", pd.DataFrame()
56
+
57
+ # AMC reminders (Updated to ensure up to 5 reminders)
58
  def check_amc_reminders(df, current_date):
59
+ try:
60
+ if "device_id" not in df.columns or "amc_date" not in df.columns:
61
+ return "AMC reminders require 'device_id' and 'amc_date' columns.", pd.DataFrame()
62
+ df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
63
+ current_date = pd.to_datetime(current_date)
64
+ df["days_to_amc"] = (df["amc_date"] - current_date).dt.days
65
+ reminders = df[(df["days_to_amc"] >= 0) & (df["days_to_amc"] <= 30)][["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]]
66
+ if reminders.empty:
67
+ return "No AMC reminders due within the next 30 days.", reminders
68
+ return "\n".join([f"- Device ID: {row['device_id']}, AMC Date: {row['amc_date']}" for _, row in reminders.head(5).iterrows()]), reminders
69
+ except Exception as e:
70
+ logging.error(f"AMC reminder generation failed: {str(e)}")
71
+ return f"AMC reminder generation failed: {str(e)}", pd.DataFrame()
72
 
73
  # Dashboard insights
74
  def generate_dashboard_insights(df):
75
+ try:
76
+ total_devices = df["device_id"].nunique()
77
+ avg_usage = df["usage_hours"].mean() if "usage_hours" in df.columns else 0
78
+ return f"{total_devices} devices with average usage of {avg_usage:.2f} hours."
79
+ except Exception as e:
80
+ logging.error(f"Dashboard insights generation failed: {str(e)}")
81
+ return "Failed to generate insights."
82
 
83
  # Placeholder chart for empty data
84
  def create_placeholder_chart(title):
85
+ fig = go.Figure()
86
+ fig.add_annotation(
87
+ text="No data available for this chart",
88
+ xref="paper", yref="paper",
89
+ x=0.5, y=0.5, showarrow=False,
90
+ font=dict(size=16)
91
+ )
92
+ fig.update_layout(title=title, margin=dict(l=20, r=20, t=40, b=20))
93
+ return fig
94
 
95
  # Create usage chart
96
  def create_usage_chart(df):
97
+ try:
98
+ if df.empty or "usage_hours" not in df.columns or "device_id" not in df.columns:
99
+ logging.warning("Insufficient data for usage chart")
100
+ return create_placeholder_chart("Usage Hours per Device")
101
+ usage_data = df.groupby("device_id")["usage_hours"].sum().reset_index()
102
+ if len(usage_data) > 5:
103
+ usage_data = usage_data.nlargest(5, "usage_hours")
104
+ fig = px.bar(
105
+ usage_data,
106
+ x="device_id",
107
+ y="usage_hours",
108
+ title="Usage Hours per Device",
109
+ labels={"device_id": "Device ID", "usage_hours": "Usage Hours"}
110
+ )
111
+ fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
112
+ return fig
113
+ except Exception as e:
114
+ logging.error(f"Failed to create usage chart: {str(e)}")
115
+ return create_placeholder_chart("Usage Hours per Device")
116
 
117
  # Create downtime chart
118
  def create_downtime_chart(df):
119
+ try:
120
+ if df.empty or "downtime" not in df.columns or "device_id" not in df.columns:
121
+ logging.warning("Insufficient data for downtime chart")
122
+ return create_placeholder_chart("Downtime per Device")
123
+ downtime_data = df.groupby("device_id")["downtime"].sum().reset_index()
124
+ if len(downtime_data) > 5:
125
+ downtime_data = downtime_data.nlargest(5, "downtime")
126
+ fig = px.bar(
127
+ downtime_data,
128
+ x="device_id",
129
+ y="downtime",
130
+ title="Downtime per Device",
131
+ labels={"device_id": "Device ID", "downtime": "Downtime (Hours)"}
132
+ )
133
+ fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
134
+ return fig
135
+ except Exception as e:
136
+ logging.error(f"Failed to create downtime chart: {str(e)}")
137
+ return create_placeholder_chart("Downtime per Device")
138
 
139
  # Create daily log trends chart
140
  def create_daily_log_trends_chart(df):
141
+ try:
142
+ if df.empty or "timestamp" not in df.columns:
143
+ logging.warning("Insufficient data for daily log trends chart")
144
+ return create_placeholder_chart("Daily Log Trends")
145
+ df['date'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.date
146
+ daily_logs = df.groupby('date').size().reset_index(name='log_count')
147
+ if daily_logs.empty:
148
+ return create_placeholder_chart("Daily Log Trends")
149
+ fig = px.line(
150
+ daily_logs,
151
+ x='date',
152
+ y='log_count',
153
+ title="Daily Log Trends",
154
+ labels={"date": "Date", "log_count": "Number of Logs"}
155
+ )
156
+ fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
157
+ return fig
158
+ except Exception as e:
159
+ logging.error(f"Failed to create daily log trends chart: {str(e)}")
160
+ return create_placeholder_chart("Daily Log Trends")
161
 
162
  # Create weekly uptime chart
163
  def create_weekly_uptime_chart(df):
164
+ try:
165
+ if df.empty or "timestamp" not in df.columns or "usage_hours" not in df.columns or "downtime" not in df.columns:
166
+ logging.warning("Insufficient data for weekly uptime chart")
167
+ return create_placeholder_chart("Weekly Uptime Percentage")
168
+ df['week'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.isocalendar().week
169
+ df['year'] = pd.to_datetime(df['timestamp'], errors='coerce').dt.year
170
+ weekly_data = df.groupby(['year', 'week']).agg({
171
+ 'usage_hours': 'sum',
172
+ 'downtime': 'sum'
173
+ }).reset_index()
174
+ weekly_data['uptime_percent'] = (weekly_data['usage_hours'] / (weekly_data['usage_hours'] + weekly_data['downtime'])) * 100
175
+ weekly_data['year_week'] = weekly_data['year'].astype(str) + '-W' + weekly_data['week'].astype(str)
176
+ if weekly_data.empty:
177
+ return create_placeholder_chart("Weekly Uptime Percentage")
178
+ fig = px.bar(
179
+ weekly_data,
180
+ x='year_week',
181
+ y='uptime_percent',
182
+ title="Weekly Uptime Percentage",
183
+ labels={"year_week": "Year-Week", "uptime_percent": "Uptime %"}
184
+ )
185
+ fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
186
+ return fig
187
+ except Exception as e:
188
+ logging.error(f"Failed to create weekly uptime chart: {str(e)}")
189
+ return create_placeholder_chart("Weekly Uptime Percentage")
190
 
191
  # Create anomaly alerts chart
192
  def create_anomaly_alerts_chart(anomalies_df):
193
+ try:
194
+ if anomalies_df is None or anomalies_df.empty or "timestamp" not in anomalies_df.columns:
195
+ logging.warning("Insufficient data for anomaly alerts chart")
196
+ return create_placeholder_chart("Anomaly Alerts Over Time")
197
+ anomalies_df['date'] = pd.to_datetime(anomalies_df['timestamp'], errors='coerce').dt.date
198
+ anomaly_counts = anomalies_df.groupby('date').size().reset_index(name='anomaly_count')
199
+ if anomaly_counts.empty:
200
+ return create_placeholder_chart("Anomaly Alerts Over Time")
201
+ fig = px.scatter(
202
+ anomaly_counts,
203
+ x='date',
204
+ y='anomaly_count',
205
+ title="Anomaly Alerts Over Time",
206
+ labels={"date": "Date", "anomaly_count": "Number of Anomalies"}
207
+ )
208
+ fig.update_layout(title_font_size=16, margin=dict(l=20, r=20, t=40, b=20))
209
+ return fig
210
+ except Exception as e:
211
+ logging.error(f"Failed to create anomaly alerts chart: {str(e)}")
212
+ return create_placeholder_chart("Anomaly Alerts Over Time")
213
 
214
  # Generate device cards
215
  def generate_device_cards(df):
216
+ try:
217
+ if df.empty:
218
+ return '<p>No devices available to display.</p>'
219
+ device_stats = df.groupby('device_id').agg({
220
+ 'status': 'last',
221
+ 'timestamp': 'max',
222
+ }).reset_index()
223
+ device_stats['count'] = df.groupby('device_id').size().reindex(device_stats['device_id']).values
224
+ device_stats['health'] = device_stats['status'].map({
225
+ 'Active': 'Healthy',
226
+ 'Inactive': 'Unhealthy',
227
+ 'Pending': 'Warning'
228
+ }).fillna('Unknown')
229
+ cards_html = '<div style="display: flex; flex-wrap: wrap; gap: 20px;">'
230
+ for _, row in device_stats.iterrows():
231
+ health_color = {'Healthy': 'green', 'Unhealthy': 'red', 'Warning': 'orange', 'Unknown': 'gray'}.get(row['health'], 'gray')
232
+ timestamp_str = str(row['timestamp']) if pd.notna(row['timestamp']) else 'Unknown'
233
+ cards_html += f"""
234
+ <div style="border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px; width: 200px;">
235
+ <h4>Device: {row['device_id']}</h4>
236
+ <p><b>Health:</b> <span style="color: {health_color}">{row['health']}</span></p>
237
+ <p><b>Usage Count:</b> {row['count']}</p>
238
+ <p><b>Last Log:</b> {timestamp_str}</p>
239
+ </div>
240
+ """
241
+ cards_html += '</div>'
242
+ return cards_html
243
+ except Exception as e:
244
+ logging.error(f"Failed to generate device cards: {str(e)}")
245
+ return f'<p>Error generating device cards: {str(e)}</p>'
246
 
247
  # Generate PDF content
248
  def generate_pdf_content(summary, preview_df, anomalies, amc_reminders, insights, device_cards_html, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart):
249
+ if not reportlab_available:
250
+ return None
251
+ try:
252
+ pdf_path = f"status_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
253
+ doc = SimpleDocTemplate(pdf_path, pagesize=letter)
254
+ styles = getSampleStyleSheet()
255
+ story = []
256
+
257
+ def safe_paragraph(text, style):
258
+ return Paragraph(str(text).replace('\n', '<br/>'), style) if text else Paragraph("", style)
259
+
260
+ story.append(Paragraph("LabOps Status Report", styles['Title']))
261
+ story.append(Paragraph(f"Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", styles['Normal']))
262
+ story.append(Spacer(1, 12))
263
+
264
+ story.append(Paragraph("Summary Report", styles['Heading2']))
265
+ story.append(safe_paragraph(summary, styles['Normal']))
266
+ story.append(Spacer(1, 12))
267
+
268
+ story.append(Paragraph("Log Preview", styles['Heading2']))
269
+ if not preview_df.empty:
270
+ data = [preview_df.columns.tolist()] + preview_df.head(5).values.tolist()
271
+ table = Table(data)
272
+ table.setStyle(TableStyle([
273
+ ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
274
+ ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
275
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
276
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
277
+ ('FONTSIZE', (0, 0), (-1, 0), 12),
278
+ ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
279
+ ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
280
+ ('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
281
+ ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
282
+ ('FONTSIZE', (0, 1), (-1, -1), 10),
283
+ ('GRID', (0, 0), (-1, -1), 1, colors.black)
284
+ ]))
285
+ story.append(table)
286
+ else:
287
+ story.append(safe_paragraph("No preview available.", styles['Normal']))
288
+ story.append(Spacer(1, 12))
289
+
290
+ story.append(Paragraph("Device Cards", styles['Heading2']))
291
+ device_cards_text = device_cards_html.replace('<div>', '').replace('</div>', '\n').replace('<h4>', '').replace('</h4>', '\n').replace('<p>', '').replace('</p>', '\n').replace('<b>', '').replace('</b>', '').replace('<span style="color: green">', '').replace('<span style="color: red">', '').replace('<span style="color: orange">', '').replace('<span style="color: gray">', '').replace('</span>', '')
292
+ story.append(safe_paragraph(device_cards_text, styles['Normal']))
293
+ story.append(Spacer(1, 12))
294
+
295
+ story.append(Paragraph("Anomaly Detection", styles['Heading2']))
296
+ story.append(safe_paragraph(anomalies, styles['Normal']))
297
+ story.append(Spacer(1, 12))
298
+
299
+ story.append(Paragraph("AMC Reminders", styles['Heading2']))
300
+ story.append(safe_paragraph(amc_reminders, styles['Normal']))
301
+ story.append(Spacer(1, 12))
302
+
303
+ story.append(Paragraph("Dashboard Insights", styles['Heading2']))
304
+ story.append(safe_paragraph(insights, styles['Normal']))
305
+ story.append(Spacer(1, 12))
306
+
307
+ story.append(Paragraph("Charts", styles['Heading2']))
308
+ story.append(Paragraph("[Chart placeholders - see dashboard for visuals]", styles['Normal']))
309
+
310
+ doc.build(story)
311
+ logging.info(f"PDF generated at {pdf_path}")
312
+ return pdf_path
313
+ except Exception as e:
314
+ logging.error(f"Failed to generate PDF: {str(e)}")
315
+ return None
316
+
317
+ # Main processing function
318
  async def process_logs(file_obj, lab_site_filter, equipment_type_filter, date_range, last_modified_state, cached_df_state, cached_filtered_df_state):
319
+ start_time = time.time()
320
+ try:
321
+ if not file_obj:
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, None
323
+
324
+ file_path = file_obj.name
325
+ current_modified_time = os.path.getmtime(file_path)
326
+
327
+ # Load or use cached original dataframe
328
+ if cached_df_state is None or current_modified_time != last_modified_state:
329
+ logging.info(f"Processing file: {file_path}")
330
+ if not file_path.endswith(".csv"):
331
+ 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, None
332
+
333
+ required_columns = ["device_id", "log_type", "status", "timestamp", "usage_hours", "downtime", "amc_date"]
334
+ dtypes = {
335
+ "device_id": "string",
336
+ "log_type": "string",
337
+ "status": "string",
338
+ "usage_hours": "float32",
339
+ "downtime": "float32",
340
+ "amc_date": "string"
341
+ }
342
+ df = pd.read_csv(file_path, dtype=dtypes)
343
+ missing_columns = [col for col in required_columns if col not in df.columns]
344
+ if missing_columns:
345
+ 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, cached_df_state, None
346
+
347
+ df["timestamp"] = pd.to_datetime(df["timestamp"], errors='coerce')
348
+ df["amc_date"] = pd.to_datetime(df["amc_date"], errors='coerce')
349
+ if df["timestamp"].dt.tz is None:
350
+ df["timestamp"] = df["timestamp"].dt.tz_localize('UTC').dt.tz_convert('Asia/Kolkata')
351
+ if df.empty:
352
+ 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, None
353
+ cached_df_state = df
354
+ else:
355
+ df = cached_df_state
356
+
357
+ # Always apply current filters to the original dataframe
358
+ filtered_df = df.copy()
359
+ if lab_site_filter and lab_site_filter != 'All' and 'lab_site' in filtered_df.columns:
360
+ filtered_df = filtered_df[filtered_df['lab_site'] == lab_site_filter]
361
+ if equipment_type_filter and equipment_type_filter != 'All' and 'equipment_type' in filtered_df.columns:
362
+ filtered_df = filtered_df[filtered_df['equipment_type'] == equipment_type_filter]
363
+ if date_range and len(date_range) == 2:
364
+ days_start, days_end = date_range
365
+ today = pd.to_datetime(datetime.now().date()).tz_localize('Asia/Kolkata')
366
+ start_date = today + pd.Timedelta(days=days_start)
367
+ end_date = today + pd.Timedelta(days=days_end) + pd.Timedelta(days=1) - pd.Timedelta(seconds=1)
368
+ filtered_df = filtered_df[(filtered_df['timestamp'] >= start_date) & (filtered_df['timestamp'] <= end_date)]
369
+
370
+ if filtered_df.empty:
371
+ return "No data after applying filters.", pd.DataFrame(), None, '<p>No device cards available.</p>', None, None, None, None, None, None, None, None, last_modified_state, cached_df_state, None
372
+
373
+ # Generate table for preview
374
+ preview_df = filtered_df[['device_id', 'log_type', 'status', 'timestamp', 'usage_hours', 'downtime', 'amc_date']].head(5)
375
+ preview_html = preview_df.to_html(index=False, classes='table table-striped', border=0)
376
+
377
+ # Run critical tasks concurrently
378
+ try:
379
+ with ThreadPoolExecutor(max_workers=2) as executor:
380
+ future_anomalies = executor.submit(detect_anomalies, filtered_df)
381
+ future_amc = executor.submit(check_amc_reminders, filtered_df, datetime.now())
382
+
383
+ summary = f"Step 1: Summary Report\n{summarize_logs(filtered_df)}"
384
+ anomalies, anomalies_df = future_anomalies.result()
385
+ anomalies = f"Anomaly Detection\n{anomalies}"
386
+ amc_reminders, reminders_df = future_amc.result()
387
+ amc_reminders = f"AMC Reminders\n{amc_reminders}"
388
+ insights = f"Dashboard Insights\n{generate_dashboard_insights(filtered_df)}"
389
+ except Exception as e:
390
+ logging.error(f"Concurrent task execution failed: {str(e)}")
391
+ summary = "Failed to generate summary due to processing error."
392
+ anomalies = "Anomaly detection failed due to processing error."
393
+ amc_reminders = "AMC reminders failed due to processing error."
394
+ insights = "Insights generation failed due to processing error."
395
+ anomalies_df = pd.DataFrame()
396
+
397
+ # Generate charts sequentially
398
+ usage_chart = create_usage_chart(filtered_df)
399
+ downtime_chart = create_downtime_chart(filtered_df)
400
+ daily_log_chart = create_daily_log_trends_chart(filtered_df)
401
+ weekly_uptime_chart = create_weekly_uptime_chart(filtered_df)
402
+ anomaly_alerts_chart = create_anomaly_alerts_chart(anomalies_df)
403
+ device_cards = generate_device_cards(filtered_df)
404
+
405
+ elapsed_time = time.time() - start_time
406
+ logging.info(f"Processing completed in {elapsed_time:.2f} seconds")
407
+ if elapsed_time > 3:
408
+ logging.warning(f"Processing time exceeded 3 seconds: {elapsed_time:.2f} seconds")
409
+
410
+ return (summary, preview_html, usage_chart, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart, anomalies, amc_reminders, insights, None, current_modified_time, cached_df_state, filtered_df)
411
+ except Exception as e:
412
+ logging.error(f"Failed to process file: {str(e)}")
413
+ return f"Error: {str(e)}", pd.DataFrame(), None, '<p>Error processing data.</p>', None, None, None, None, None, None, None, None, last_modified_state, cached_df_state, None
414
 
415
  # Generate PDF separately
416
  async def generate_pdf(summary, preview_html, usage_chart, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart, anomalies, amc_reminders, insights):
417
+ try:
418
+ preview_df = pd.read_html(preview_html)[0]
419
+ pdf_file = generate_pdf_content(summary, preview_df, anomalies, amc_reminders, insights, device_cards, daily_log_chart, weekly_uptime_chart, anomaly_alerts_chart, downtime_chart)
420
+ return pdf_file
421
+ except Exception as e:
422
+ logging.error(f"Failed to generate PDF: {str(e)}")
423
+ return None
424
 
425
  # Update filters
426
  def update_filters(file_obj, current_file_state):
427
+ if not file_obj or file_obj.name == current_file_state:
428
+ return gr.update(), gr.update(), current_file_state
429
+ try:
430
+ with open(file_obj.name, 'rb') as f:
431
+ csv_content = f.read().decode('utf-8')
432
+ df = pd.read_csv(io.StringIO(csv_content))
433
+ df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
434
+
435
+ lab_site_options = ['All'] + [site for site in df['lab_site'].dropna().astype(str).unique().tolist() if site.strip()] if 'lab_site' in df.columns else ['All']
436
+ equipment_type_options = ['All'] + [equip for equip in df['equipment_type'].dropna().astype(str).unique().tolist() if equip.strip()] if 'equipment_type' in df.columns else ['All']
437
+
438
+ return gr.update(choices=lab_site_options, value='All'), gr.update(choices=equipment_type_options, value='All'), file_obj.name
439
+ except Exception as e:
440
+ logging.error(f"Failed to update filters: {str(e)}")
441
+ return gr.update(choices=['All'], value='All'), gr.update(choices=['All'], value='All'), current_file_state
442
 
443
  # Gradio Interface
444
  try:
445
+ logging.info("Initializing Gradio interface...")
446
+ with gr.Blocks(css="""
447
+ .dashboard-container {border: 1px solid #e0e0e0; padding: 10px; border-radius: 5px;}
448
+ .dashboard-title {font-size: 24px; font-weight: bold; margin-bottom: 5px;}
449
+ .dashboard-section {margin-bottom: 20px;}
450
+ .dashboard-section h3 {font-size: 18px; margin-bottom: 2px;}
451
+ .dashboard-section p {margin: 1px 0; line-height: 1.2;}
452
+ .dashboard-section ul {margin: 2px 0; padding-left: 20px;}
453
+ .table {width: 100%; border-collapse: collapse;}
454
+ .table th, .table td {border: 1px solid #ddd; padding: 8px; text-align: left;}
455
+ .table th {background-color: #f2f2f2;}
456
+ .table tr:nth-child(even) {background-color: #f9f9f9;}
457
+ """) as iface:
458
+ gr.Markdown("<h1>LabOps Log Analyzer Dashboard</h1>")
459
+ gr.Markdown("Upload a CSV file to analyze. Click 'Analyze' to refresh the dashboard. Use 'Export PDF' for report download.")
460
+
461
+ last_modified_state = gr.State(value=None)
462
+ current_file_state = gr.State(value=None)
463
+ cached_df_state = gr.State(value=None)
464
+ cached_filtered_df_state = gr.State(value=None)
465
+
466
+ with gr.Row():
467
+ with gr.Column(scale=1):
468
+ file_input = gr.File(label="Upload Logs (CSV)", file_types=[".csv"])
469
+ with gr.Group():
470
+ gr.Markdown("### Filters")
471
+ lab_site_filter = gr.Dropdown(label="Lab Site", choices=['All'], value='All', interactive=True)
472
+ equipment_type_filter = gr.Dropdown(label="Equipment Type", choices=['All'], value='All', interactive=True)
473
+ date_range_filter = gr.Slider(label="Date Range (Days from Today)", minimum=-365, maximum=0, step=1, value=[-30, 0])
474
+ submit_button = gr.Button("Analyze", variant="primary")
475
+ pdf_button = gr.Button("Export PDF", variant="secondary")
476
+
477
+ with gr.Column(scale=2):
478
+ with gr.Group(elem_classes="dashboard-container"):
479
+ gr.Markdown("<div class='dashboard-title'>Analysis Results</div>")
480
+ with gr.Group(elem_classes="dashboard-section"):
481
+ gr.Markdown("### Step 1: Summary Report")
482
+ summary_output = gr.Markdown()
483
+ with gr.Group(elem_classes="dashboard-section"):
484
+ gr.Markdown("### Step 2: Log Preview")
485
+ preview_output = gr.HTML()
486
+ with gr.Group(elem_classes="dashboard-section"):
487
+ gr.Markdown("### Device Cards")
488
+ device_cards_output = gr.HTML()
489
+ with gr.Group(elem_classes="dashboard-section"):
490
+ gr.Markdown("### Charts")
491
+ with gr.Tab("Usage Hours per Device"):
492
+ usage_chart_output = gr.Plot()
493
+ with gr.Tab("Downtime per Device"):
494
+ downtime_chart_output = gr.Plot()
495
+ with gr.Tab("Daily Log Trends"):
496
+ daily_log_trends_output = gr.Plot()
497
+ with gr.Tab("Weekly Uptime Percentage"):
498
+ weekly_uptime_output = gr.Plot()
499
+ with gr.Tab("Anomaly Alerts"):
500
+ anomaly_alerts_output = gr.Plot()
501
+ with gr.Group(elem_classes="dashboard-section"):
502
+ gr.Markdown("### Step 4: Anomaly Detection")
503
+ anomaly_output = gr.Markdown()
504
+ with gr.Group(elem_classes="dashboard-section"):
505
+ gr.Markdown("### Step 5: AMC Reminders")
506
+ amc_output = gr.Markdown()
507
+ with gr.Group(elem_classes="dashboard-section"):
508
+ gr.Markdown("### Step 6: Insights")
509
+ insights_output = gr.Markdown()
510
+ with gr.Group(elem_classes="dashboard-section"):
511
+ gr.Markdown("### Export Report")
512
+ pdf_output = gr.File(label="Download Status Report as PDF")
513
+
514
+ file_input.change(
515
+ fn=update_filters,
516
+ inputs=[file_input, current_file_state],
517
+ outputs=[lab_site_filter, equipment_type_filter, current_file_state],
518
+ queue=False
519
+ )
520
+
521
+ submit_button.click(
522
+ fn=process_logs,
523
+ inputs=[file_input, lab_site_filter, equipment_type_filter, date_range_filter, last_modified_state, cached_df_state, cached_filtered_df_state],
524
+ 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]
525
+ )
526
+
527
+ pdf_button.click(
528
+ fn=generate_pdf,
529
+ inputs=[summary_output, preview_html, 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],
530
+ outputs=[pdf_output]
531
+ )
532
+
533
+ logging.info("Gradio interface initialized successfully")
534
  except Exception as e:
535
+ logging.error(f"Failed to initialize Gradio interface: {str(e)}")
536
+ raise e
537
 
538
  if __name__ == "__main__":
539
+ try:
540
+ logging.info("Launching Gradio interface...")
541
+ iface.launch(server_name="0.0.0.0", server_port=7860, debug=True, share=False)
542
+ logging.info("Gradio interface launched successfully")
543
+ except Exception as e:
544
+ logging.error(f"Failed to launch Gradio interface: {str(e)}")
545
+ print(f"Error launching app: {str(e)}")
546
+ raise e