File size: 30,262 Bytes
36459c4
6eaa3dc
e2b7de3
83ce7a6
e2b7de3
36459c4
e2b7de3
 
 
c528749
e2b7de3
221e826
de27e81
 
221e826
7037721
1f2a07b
eadd170
 
 
1f2a07b
eadd170
9a6ae7c
 
eadd170
 
1f2a07b
 
 
9a6ae7c
1f2a07b
eadd170
 
1f2a07b
 
 
7037721
9a6ae7c
1f2a07b
eadd170
 
 
 
1f2a07b
 
 
7037721
1f2a07b
 
eadd170
 
 
 
 
 
 
 
 
9a6ae7c
 
1f2a07b
9a6ae7c
1f2a07b
eadd170
 
 
 
 
1f2a07b
 
eadd170
9a6ae7c
eadd170
1f2a07b
eadd170
 
 
 
1f2a07b
 
eadd170
9a6ae7c
1f2a07b
 
eadd170
9a6ae7c
1f2a07b
 
eadd170
 
1f2a07b
 
eadd170
 
 
 
 
 
 
 
1f2a07b
 
 
 
 
 
 
 
 
9a6ae7c
 
eadd170
1f2a07b
 
 
 
 
 
 
 
 
 
 
9a6ae7c
1f2a07b
 
eadd170
9a6ae7c
eadd170
 
 
 
9a6ae7c
eadd170
 
 
 
 
9a6ae7c
eadd170
 
 
 
 
 
 
 
 
 
 
 
 
9a6ae7c
eadd170
 
 
 
 
 
 
 
 
 
 
9a6ae7c
 
eadd170
 
 
 
 
 
 
 
9a6ae7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eadd170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f2a07b
 
 
231d664
 
7052c88
231d664
 
bfc7a88
 
7052c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231d664
 
7037721
bfc7a88
 
7037721
 
 
231d664
 
7052c88
 
 
 
 
 
 
 
231d664
 
eadd170
 
7037721
eadd170
 
 
 
 
 
 
 
7037721
 
eadd170
7037721
 
 
eadd170
 
7037721
eadd170
7037721
 
eadd170
7037721
 
eadd170
 
 
 
 
83ce7a6
 
7037721
83ce7a6
 
c528749
83ce7a6
c528749
 
83ce7a6
 
7037721
c528749
7037721
eadd170
83ce7a6
eadd170
 
83ce7a6
 
 
eadd170
7037721
83ce7a6
7037721
 
83ce7a6
eadd170
83ce7a6
eadd170
83ce7a6
 
c528749
 
83ce7a6
 
c528749
83ce7a6
c528749
 
1f2a07b
 
 
83ce7a6
c528749
83ce7a6
 
c528749
 
1f2a07b
eadd170
83ce7a6
 
 
1f2a07b
 
 
 
 
 
 
7037721
 
eadd170
1f2a07b
 
 
 
 
 
 
 
 
13ed916
e2b7de3
 
eadd170
e2b7de3
de27e81
 
 
13ed916
e2b7de3
eadd170
13ed916
e2b7de3
de27e81
 
952210b
e2b7de3
eadd170
6eaa3dc
de27e81
e2b7de3
684911e
e2b7de3
 
0d752e6
bfc7a88
 
 
 
7052c88
231d664
 
 
eadd170
e2b7de3
0d752e6
e2b7de3
eadd170
8846627
e2b7de3
 
eadd170
e2b7de3
 
 
de27e81
221e826
e2b7de3
de27e81
e2b7de3
de27e81
 
 
 
eadd170
de27e81
83ce7a6
 
e2b7de3
de27e81
 
 
83ce7a6
de27e81
e2b7de3
de27e81
e2b7de3
de27e81
e2b7de3
c528749
e2b7de3
c528749
 
 
 
de27e81
83ce7a6
 
 
 
 
eadd170
 
 
 
 
1f2a07b
 
 
 
 
e2b7de3
 
 
c528749
de27e81
eadd170
e2b7de3
 
 
 
 
 
 
231d664
9a6ae7c
7037721
e2b7de3
 
231d664
 
 
 
eadd170
231d664
9a6ae7c
7037721
9a6ae7c
231d664
9a6ae7c
 
 
 
e2b7de3
de27e81
e2b7de3
 
231d664
e2b7de3
 
 
08fef74
e2b7de3
 
221e826
e2b7de3
 
83ce7a6
e2b7de3
 
36459c4
83ce7a6
e2b7de3
 
 
 
221e826
e2b7de3
 
36459c4
e2b7de3
 
 
 
 
 
 
 
 
 
 
83ce7a6
 
 
231d664
e2b7de3
 
 
 
 
 
 
1f2a07b
e2b7de3
 
 
231d664
de27e81
 
 
e2b7de3
 
c528749
 
 
 
 
eadd170
c528749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83ce7a6
e2b7de3
c528749
 
 
231d664
c528749
7037721
 
 
e2b7de3
de27e81
eadd170
c528749
 
7037721
 
c528749
 
 
231d664
c528749
 
 
 
 
 
 
7037721
 
 
 
 
 
 
c528749
 
 
 
 
 
 
eadd170
 
c528749
e2b7de3
 
eadd170
231d664
e2b7de3
7037721
c528749
231d664
c528749
231d664
7037721
 
c528749
 
231d664
 
 
c528749
 
 
e2b7de3
eadd170
e2b7de3
231d664
de27e81
e2b7de3
c528749
 
de27e81
c528749
231d664
1f2a07b
 
 
 
 
 
 
 
 
 
c528749
e2b7de3
c528749
 
1f2a07b
 
 
 
 
 
e2b7de3
231d664
 
c528749
 
 
 
1f2a07b
c528749
e2b7de3
c528749
e2b7de3
c528749
 
 
 
 
 
1f2a07b
7037721
 
eadd170
1f2a07b
eadd170
c528749
1f2a07b
c528749
1f2a07b
 
 
 
 
c528749
 
 
 
 
de27e81
e2b7de3
 
de27e81
e2b7de3
 
 
 
 
1f2a07b
eadd170
36459c4
eadd170
 
36459c4
eadd170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2b7de3
36459c4
 
eadd170
36459c4
221e826
 
de27e81
e2b7de3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neighbors import LocalOutlierFactor
from datetime import datetime, timedelta
import os
import logging
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib import colors
import tempfile

# Configure logging to match the log format
logging.basicConfig(level=logging.INFO, format='%(asctime)s,%(msecs)03d - %(levelname)s - %(message)s')

# CSS styling for the Gradio interface with a dark theme and blue button
css = """
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');

body {
    font-family: 'Roboto', sans-serif;
    background-color: #1F2937;
    color: #D1D5DB;
    margin: 0;
    padding: 20px;
}

h1 {
    color: #FFFFFF;
    text-align: center;
    font-size: 2rem;
    margin-bottom: 30px;
}

.gr-button {
    background-color: #3B82F6;
    color: #1F2937;
    border: none;
    border-radius: 8px;
    padding: 12px 24px;
    font-weight: 500;
    transition: background-color 0.3s;
}

.gr-button:hover {
    background-color: #2563EB;
}

.dashboard-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

.card {
    background-color: #374151;
    border: 1px solid #4B5563;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    padding: 20px;
    transition: transform 0.2s;
}

.card:hover {
    transform: translateY(-5px);
}

.card h2 {
    color: #FFFFFF;
    font-size: 1.2rem;
    margin-top: 0;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.device-card {
    border-left: 5px solid #2DD4BF;
}

.alert-card {
    border-left: 5px solid #F87171;
}

.chart-container {
    overflow-x: auto;
}

.dataframe-container {
    max-height: 400px;
    overflow-y: auto;
}

.flowchart-container {
    max-height: 400px;
    overflow-y: auto;
}

.flowchart {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.flowchart-step {
    background-color: #4B5563;
    border-left: 5px solid #2DD4BF;
    padding: 15px;
    border-radius: 5px;
    position: relative;
}

.flowchart-step:not(:last-child):after {
    content: '↓';
    position: absolute;
    bottom: -20px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 20px;
    color: #2DD4BF;
}

.alert-urgent {
    color: #F87171;
    font-weight: bold;
}

.alert-upcoming {
    color: #FBBF24;
    font-weight: bold;
}

.recommendation {
    font-style: italic;
    color: #9CA3AF;
    margin-top: 10px;
}

.anomaly-badge {
    display: inline-block;
    padding: 5px 10px;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 500;
}

.anomaly-unusual {
    background-color: #FEE2E2;
    color: #F87171;
}

.anomaly-normal {
    background-color: #D1FAE5;
    color: #10B981;
}

.download-button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background-color: #2DD4BF;
    color: #1F2937;
    padding: 10px 20px;
    border-radius: 8px;
    text-decoration: none;
    font-weight: 500;
    transition: background-color 0.3s;
}

.download-button:hover {
    background-color: #26A69A;
}

/* Ensure text in dataframe is readable */
.dataframe-container table {
    color: #D1D5DB;
    background-color: #374151;
}

.dataframe-container thead th {
    background-color: #4B5563;
    color: #FFFFFF;
    font-weight: 500;
}

.dataframe-container tbody tr:nth-child(even) {
    background-color: #4B5563;
}

.dataframe-container tbody tr:hover {
    background-color: #6B7280;
}

/* Responsive Design */
@media (max-width: 768px) {
    .dashboard-container {
        grid-template-columns: 1fr;
    }

    h1 {
        font-size: 1.5rem;
    }

    .card {
        padding: 15px;
    }

    .gr-button {
        width: 100%;
        padding: 10px;
    }

    .download-button {
        width: 100%;
        justify-content: center;
    }
}
"""

def validate_csv(df):
    """
    Validate that the CSV has the required columns, handling both original and renamed columns.
    Returns True if valid, False otherwise with an error message.
    """
    # Strip whitespace from column names
    df.columns = df.columns.str.strip()

    # Define expected original and renamed columns
    original_columns = ['device_id', 'usage_hours', 'amc_date', 'status']
    renamed_columns = ['equipment', 'usage_count', 'amc_expiry', 'status']
    
    # Check for original columns
    missing_original = [col for col in original_columns if col not in df.columns]
    # Check for renamed columns
    missing_renamed = [col for col in renamed_columns if col not in df.columns]

    # If original columns are present, proceed as is
    if not missing_original:
        logging.info("Found original columns in CSV. Proceeding with validation.")
    # If renamed columns are present, map them back to original for validation
    elif not missing_renamed:
        logging.info("Found renamed columns in CSV. Mapping back to original names for validation.")
        df.rename(columns={
            'equipment': 'device_id',
            'usage_count': 'usage_hours',
            'amc_expiry': 'amc_date'
        }, inplace=True)
    else:
        # If neither set is fully present, report missing columns
        found_columns = ', '.join(df.columns)
        return False, f"Missing required columns. Expected either {', '.join(original_columns)} or {', '.join(renamed_columns)}. Found columns: {found_columns}"

    # Validate data types
    try:
        df['usage_hours'] = pd.to_numeric(df['usage_hours'], errors='raise')
        # Parse amc_date with specified format
        df['amc_date'] = pd.to_datetime(df['amc_date'], format='%d-%m-%Y', errors='raise')
        # Handle 'downtime' if present
        if 'downtime' in df.columns:
            df['downtime'] = pd.to_numeric(df['downtime'], errors='raise')
    except Exception as e:
        return False, f"Invalid data types: {str(e)}"
    
    # Rename columns to internal names after validation
    df.rename(columns={
        'device_id': 'equipment',
        'usage_hours': 'usage_count',
        'amc_date': 'amc_expiry'
    }, inplace=True)
    
    return True, ""

def generate_device_cards(df, anomaly_df):
    """
    Generate HTML for device cards showing health, usage hours, downtime, status, log type, and timestamp.
    Returns an HTML string.
    """
    if anomaly_df is not None:
        df['anomaly'] = anomaly_df['anomaly'].map({1: "Normal", -1: "Unusual"})
    else:
        df['anomaly'] = "Unknown"

    html = []
    for device_id in df['equipment'].unique():
        device_data = df[df['equipment'] == device_id].iloc[-1]  # Latest record
        anomaly_class = "anomaly-unusual" if device_data['anomaly'] == "Unusual" else "anomaly-normal"
        downtime = device_data.get('downtime', 'N/A')
        log_type = device_data.get('log_type', 'N/A')
        timestamp = device_data.get('timestamp', 'N/A')
        html.append(f"""
        <div class="card device-card">
            <h2><i class="fas fa-microchip"></i> {device_id}</h2>
            <p><strong>Status:</strong> {device_data['status']}</p>
            <p><strong>Usage Hours:</strong> {device_data['usage_count']}</p>
            <p><strong>Downtime (hrs):</strong> {downtime}</p>
            <p><strong>Activity:</strong> <span class="anomaly-badge {anomaly_class}">{device_data['anomaly']}</span></p>
            <p><strong>Log Type:</strong> {log_type}</p>
            <p><strong>Last Log:</strong> {timestamp}</p>
            <p><strong>AMC Expiry:</strong> {device_data['amc_expiry'].strftime('%Y-%m-%d')}</p>
        </div>
        """)
    return "\n".join(html)

def generate_summary(combined_df, anomaly_df, amc_df, plot_path, pdf_path):
    """
    Generate a detailed and easy-to-understand summary of the processing results, including downtime.
    Returns a markdown string for display in the Gradio interface.
    """
    summary = []

    # Overview
    summary.append("## Overview")
    total_records = len(combined_df)
    unique_devices = combined_df['equipment'].unique()
    total_downtime = combined_df['downtime'].sum() if 'downtime' in combined_df.columns else 0
    summary.append(f"We processed **{total_records} log entries** for **{len(unique_devices)} devices** ({', '.join(unique_devices)}).")
    summary.append(f"Total downtime recorded: **{total_downtime} hours**.")
    summary.append("This dashboard provides real-time insights into device health, usage patterns, and maintenance needs.\n")

    # Downtime Insights (Anomalies)
    summary.append("## Downtime Insights")
    if anomaly_df is not None:
        num_anomalies = sum(anomaly_df['anomaly'] == -1)
        if num_anomalies > 0:
            summary.append(f"**{num_anomalies} potential downtime risks** detected:")
            anomaly_records = anomaly_df[anomaly_df['anomaly'] == -1][['equipment', 'usage_count', 'status', 'downtime']]
            for _, row in anomaly_records.iterrows():
                downtime = row['downtime'] if 'downtime' in row else 'N/A'
                summary.append(f"- **{row['equipment']}** (Usage: {row['usage_count']}, Status: {row['status']}, Downtime: {downtime} hrs) - Indicates possible overuse or underuse.")
        else:
            summary.append("No potential downtime risks detected. All devices are operating within expected patterns.")
    else:
        summary.append("Unable to detect downtime risks due to an error.")
    summary.append("\n")

    # Maintenance Alerts (AMC Expiries)
    summary.append("## Maintenance Alerts")
    if amc_df is not None and not amc_df.empty:
        unique_devices_amc = amc_df['equipment'].unique()
        summary.append(f"**{len(unique_devices_amc)} devices** need maintenance soon (within 7 days from 2025-06-05):")
        for _, row in amc_df.iterrows():
            days_until_expiry = (row['amc_expiry'] - datetime(2025, 6, 5)).days
            urgency = "Urgent" if days_until_expiry <= 3 else "Upcoming"
            urgency_class = "alert-urgent" if urgency == "Urgent" else "alert-upcoming"
            summary.append(f"- <span class='{urgency_class}'>⚠️ {urgency}</span>: **{row['equipment']}** - Due on {row['amc_expiry'].strftime('%Y-%m-%d')} ({days_until_expiry} days left)")
        summary.append("\n<div class='recommendation'>Recommendation: Contact the maintenance team within 24 hours for urgent alerts at support@company.com.</div>")
    else:
        summary.append("No devices need maintenance within the next 7 days.")
    summary.append("\n")

    # Generated Reports
    summary.append("## Generated Reports")
    summary.append("- **Usage Chart**: Visualizes usage patterns across devices, helping identify overworked or underused equipment. See below for the chart.")
    summary.append("- **PDF Report**: A comprehensive report including device logs, downtime insights, maintenance alerts, and a processing flowchart. Download it below.")

    return "\n".join(summary)

def generate_flowchart_html():
    """
    Generate an HTML representation of the flowchart for the Gradio interface.
    Returns an HTML string.
    """
    steps = [
        ("Upload CSV File(s)", "User uploads log files in CSV format."),
        ("Validate Data", "Checks for required columns (device_id, usage_hours, amc_date, status) and correct data types."),
        ("Generate Usage Chart", "Creates a bar chart showing usage hours by device and status (e.g., ok, warning)."),
        ("Detect Downtime Risks", "Uses Local Outlier Factor to identify devices with unusual usage patterns (e.g., too high or too low)."),
        ("Check Maintenance Dates", "Identifies devices with AMC expiries within 7 days from 2025-06-05."),
        ("Create PDF Report", "Generates a detailed PDF with data tables, insights, and this flowchart.")
    ]
    html = ["<div class='flowchart'>"]
    for step, description in steps:
        html.append(f"<div class='flowchart-step'><strong>{step}</strong><br>{description}</div>")
    html.append("</div>")
    return "\n".join(html)

def process_files(uploaded_files):
    """
    Process uploaded CSV files, generate usage plots, detect anomalies, and process AMC expiries.
    Returns a dataframe, plot path, PDF path, AMC expiry message, summary, device cards HTML, and flowchart HTML.
    """
    # Log received files
    logging.info(f"Received uploaded files: {uploaded_files}")

    if not uploaded_files:
        logging.warning("No files uploaded.")
        return None, None, None, "Please upload at least one valid CSV file.", "## Summary\nNo files uploaded.", "", ""

    valid_files = [f for f in uploaded_files if f.name.endswith('.csv')]
    logging.info(f"Processing {len(valid_files)} valid files: {valid_files}")

    if not valid_files:
        logging.warning("No valid CSV files uploaded.")
        return None, None, None, "Please upload at least one valid CSV file.", "## Summary\nNo valid CSV files uploaded.", "", ""

    logging.info("Loading logs from uploaded files...")
    all_data = []

    # Load and combine CSV files
    for file in valid_files:
        try:
            # Read CSV with explicit delimiter and strip whitespace
            df = pd.read_csv(file.name, delimiter=',', skipinitialspace=True)
            # Log the columns for debugging
            logging.info(f"Columns in {file.name}: {', '.join(df.columns)}")
            # Validate CSV structure (renaming happens inside validate_csv now)
            is_valid, error_msg = validate_csv(df)
            if not is_valid:
                logging.error(f"Failed to load {file.name}: {error_msg}")
                return None, None, None, f"Error loading {file.name}: {error_msg}", f"## Summary\nError: {error_msg}", "", ""
            all_data.append(df)
        except Exception as e:
            logging.error(f"Failed to load {file.name}: {str(e)}")
            return None, None, None, f"Error loading {file.name}: {str(e)}", f"## Summary\nError: {str(e)}", "", ""

    if not all_data:
        logging.warning("No data loaded from uploaded files.")
        return None, None, None, "No valid data found in uploaded files.", "## Summary\nNo data loaded.", "", ""

    combined_df = pd.concat(all_data, ignore_index=True)
    logging.info(f"Combined {len(combined_df)} total records.")
    logging.info(f"Loaded {len(combined_df)} log records from uploaded files.")

    # Generate usage plot
    logging.info("Generating usage plot...")
    plot_path = generate_usage_plot(combined_df)
    if plot_path:
        logging.info("Usage plot generated successfully.")
    else:
        logging.error("Failed to generate usage plot.")
        return combined_df, None, None, "Failed to generate usage plot.", "## Summary\nUsage plot generation failed.", "", ""

    # Detect anomalies using Local Outlier Factor
    logging.info("Detecting anomalies using Local Outlier Factor...")
    anomaly_df = detect_anomalies(combined_df)
    if anomaly_df is None:
        logging.error("Failed to detect anomalies.")
    else:
        logging.info(f"Detected {sum(anomaly_df['anomaly'] == -1)} anomalies using Local Outlier Factor.")

    # Process AMC expiries
    logging.info("Processing AMC expiries...")
    amc_message, amc_df = process_amc_expiries(combined_df)

    # Generate PDF report
    logging.info("Generating PDF report...")
    pdf_path = generate_pdf_report(combined_df, anomaly_df, amc_df)
    if pdf_path:
        logging.info("PDF report generated successfully.")
    else:
        logging.error("Failed to generate PDF report.")

    # Generate summary
    logging.info("Generating summary of results...")
    summary = generate_summary(combined_df, anomaly_df, amc_df, plot_path, pdf_path)
    logging.info("Summary generated successfully.")

    # Generate device cards
    logging.info("Generating device cards HTML...")
    device_cards_html = generate_device_cards(combined_df, anomaly_df)
    logging.info("Device cards HTML generated successfully.")

    # Generate flowchart HTML
    logging.info("Generating flowchart HTML...")
    flowchart_html = generate_flowchart_html()
    logging.info("Flowchart HTML generated successfully.")

    # Prepare output dataframe (combine original data with anomalies)
    output_df = combined_df.copy()
    if anomaly_df is not None:
        output_df['anomaly'] = anomaly_df['anomaly'].map({1: "Normal", -1: "Unusual"})

    return output_df, plot_path, pdf_path, amc_message, summary, device_cards_html, flowchart_html

def generate_usage_plot(df):
    """
    Generate a bar plot of usage_count by equipment and status.
    Returns the path to the saved plot.
    """
    try:
        plt.figure(figsize=(12, 6))
        # Define colors for statuses (adjusted for dark theme visibility)
        status_colors = {'ok': '#2DD4BF', 'warning': '#F87171', 'normal': '#10B981', 'down': '#FBBF24'}
        for status in df['status'].unique():
            subset = df[df['status'] == status]
            plt.bar(
                subset['equipment'] + f" ({status})",
                subset['usage_count'],
                label=status,
                color=status_colors.get(status, '#6B7280')
            )
        plt.xlabel("Equipment (Status)", fontsize=12, color='#D1D5DB')
        plt.ylabel("Usage Hours", fontsize=12, color='#D1D5DB')
        plt.title("Device Usage Overview", fontsize=14, color='#FFFFFF')
        plt.legend(title="Status")
        plt.xticks(rotation=45, ha='right', color='#D1D5DB')
        plt.yticks(color='#D1D5DB')
        plt.gca().set_facecolor('#374151')
        plt.gcf().set_facecolor('#1F2937')
        plt.tight_layout()

        # Save plot to temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
            plt.savefig(tmp.name, format='png', dpi=100)
            plot_path = tmp.name
        plt.close()
        return plot_path
    except Exception as e:
        logging.error(f"Failed to generate usage plot: {str(e)}")
        return None

def detect_anomalies(df):
    """
    Detect anomalies in usage_count using Local Outlier Factor.
    Returns a dataframe with an 'anomaly' column (-1 for anomalies, 1 for normal).
    """
    try:
        model = LocalOutlierFactor(n_neighbors=5, contamination=0.1)
        anomalies = model.fit_predict(df[['usage_count']].values)
        anomaly_df = df.copy()
        anomaly_df['anomaly'] = anomalies
        return anomaly_df
    except Exception as e:
        logging.error(f"Failed to detect anomalies: {str(e)}")
        return None

def process_amc_expiries(df):
    """
    Identify devices with AMC expiries within 7 days from 2025-06-05.
    Returns a message and a dataframe of devices with upcoming expiries.
    """
    try:
        current_date = datetime(2025, 6, 5)
        threshold = current_date + timedelta(days=7)
        df['amc_expiry'] = pd.to_datetime(df['amc_expiry'])
        upcoming_expiries = df[df['amc_expiry'] <= threshold]
        unique_devices = upcoming_expiries['equipment'].unique()
        message = f"Found {len(unique_devices)} devices with upcoming AMC expiries: {', '.join(unique_devices)}. Details: " + "; ".join(
            [f"{row['equipment']}: {row['amc_expiry'].strftime('%Y-%m-%d')}" for _, row in upcoming_expiries.iterrows()]
        )
        logging.info(f"Found {len(unique_devices)} devices with upcoming AMC expiries.")
        return message, upcoming_expiries
    except Exception as e:
        logging.error(f"Failed to process AMC expiries: {str(e)}")
        return f"Error processing AMC expiries: {str(e)}", None

def generate_pdf_report(original_df, anomaly_df, amc_df):
    """
    Generate a professionally formatted PDF report with necessary fields and a detailed flowchart.
    Returns the path to the saved PDF.
    """
    try:
        if original_df is None or original_df.empty:
            logging.warning("No data available for PDF generation.")
            return None

        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp:
            c = canvas.Canvas(tmp.name, pagesize=letter)
            width, height = letter

            def draw_header():
                c.setFont("Helvetica-Bold", 16)
                c.setFillColor(colors.darkblue)
                c.drawString(50, height - 50, "Multi-Device LabOps Dashboard Report")
                c.setFont("Helvetica", 10)
                c.setFillColor(colors.black)
                c.drawString(50, height - 70, f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
                c.line(50, height - 80, width - 50, height - 80)

            def draw_section_title(title, y):
                c.setFont("Helvetica-Bold", 14)
                c.setFillColor(colors.darkblue)
                c.drawString(50, y, title)
                c.setFillColor(colors.black)
                c.line(50, y - 5, width - 50, y - 5)
                return y - 30

            y = height - 100
            draw_header()

            # Summary
            y = draw_section_title("Summary", y)
            c.setFont("Helvetica", 12)
            c.drawString(50, y, f"Total Records: {len(original_df)}")
            y -= 20
            c.drawString(50, y, f"Unique Devices: {', '.join(original_df['equipment'].unique())}")
            y -= 20
            total_downtime = original_df['downtime'].sum() if 'downtime' in original_df.columns else 0
            c.drawString(50, y, f"Total Downtime: {total_downtime} hours")
            y -= 40

            # Device Log Details
            y = draw_section_title("Device Log Details", y)
            c.setFont("Helvetica-Bold", 10)
            headers = ["Equipment", "Timestamp", "Usage Hours", "Downtime (hrs)", "Status", "Log Type", "AMC Expiry", "Activity"]
            x_positions = [50, 110, 190, 260, 320, 370, 430, 490]
            for i, header in enumerate(headers):
                c.drawString(x_positions[i], y, header)
            c.line(50, y - 5, width - 50, y - 5)
            y -= 20

            c.setFont("Helvetica", 10)
            output_df = original_df.copy()
            if anomaly_df is not None:
                output_df['anomaly'] = anomaly_df['anomaly'].map({1: "Normal", -1: "Unusual"})
            for _, row in output_df.iterrows():
                c.drawString(50, y, str(row['equipment']))
                c.drawString(110, y, str(row.get('timestamp', 'N/A')))
                c.drawString(190, y, str(row['usage_count']))
                c.drawString(260, y, str(row.get('downtime', 'N/A')))
                c.drawString(320, y, str(row['status']))
                c.drawString(370, y, str(row.get('log_type', 'N/A')))
                c.drawString(430, y, str(row['amc_expiry'].strftime('%Y-%m-%d')))
                c.drawString(490, y, str(row['anomaly']))
                y -= 20
                if y < 50:
                    c.showPage()
                    y = height - 100
                    draw_header()
                    c.setFont("Helvetica", 10)

            # Downtime Insights
            y = draw_section_title("Downtime Insights (Using Local Outlier Factor)", y)
            c.setFont("Helvetica", 12)
            if anomaly_df is not None:
                num_anomalies = sum(anomaly_df['anomaly'] == -1)
                c.drawString(50, y, f"Potential Downtime Risks Detected: {num_anomalies}")
                y -= 20
                if num_anomalies > 0:
                    anomaly_records = anomaly_df[anomaly_df['anomaly'] == -1][['equipment', 'usage_count', 'status', 'downtime']]
                    c.drawString(50, y, "Details:")
                    y -= 20
                    c.setFont("Helvetica-Oblique", 10)
                    for _, row in anomaly_records.iterrows():
                        downtime = row['downtime'] if 'downtime' in row else 'N/A'
                        c.drawString(50, y, f"{row['equipment']}: Usage Hours = {row['usage_count']}, Status = {row['status']}, Downtime = {downtime} hrs")
                        y -= 20
                        c.drawString(70, y, "Note: This device’s usage is significantly higher or lower than others, which may indicate overuse or underuse.")
                        y -= 20
                        if y < 50:
                            c.showPage()
                            y = height - 100
                            draw_header()
                            c.setFont("Helvetica-Oblique", 10)
            else:
                c.drawString(50, y, "Unable to detect downtime risks due to an error.")
                y -= 20
            y -= 20

            # AMC Expiries
            y = draw_section_title("Maintenance Alerts (as of 2025-06-05)", y)
            c.setFont("Helvetica", 12)
            if amc_df is not None and not amc_df.empty:
                c.drawString(50, y, f"Devices Needing Maintenance Soon: {len(amc_df['equipment'].unique())}")
                y -= 20
                # Table headers
                c.setFont("Helvetica-Bold", 10)
                headers = ["Device", "Expiry Date", "Urgency", "Days Left", "Action"]
                x_positions = [50, 150, 250, 350, 450]
                for i, header in enumerate(headers):
                    c.drawString(x_positions[i], y, header)
                c.line(50, y - 5, width - 50, y - 5)
                y -= 20

                # Table rows
                c.setFont("Helvetica", 10)
                for _, row in amc_df.iterrows():
                    days_until_expiry = (row['amc_expiry'] - datetime(2025, 6, 5)).days
                    urgency = "Urgent" if days_until_expiry <= 3 else "Upcoming"
                    action = "Contact maintenance team within 24 hours" if urgency == "Urgent" else "Schedule maintenance this week"
                    c.drawString(50, y, str(row['equipment']))
                    c.drawString(150, y, str(row['amc_expiry'].strftime('%Y-%m-%d')))
                    c.drawString(250, y, urgency)
                    c.drawString(350, y, str(days_until_expiry))
                    c.drawString(450, y, action)
                    y -= 20
                    if y < 50:
                        c.showPage()
                        y = height - 100
                        draw_header()
                        c.setFont("Helvetica", 10)
                c.setFont("Helvetica-Oblique", 10)
                c.drawString(50, y, "Contact: Email the maintenance team at support@company.com for scheduling.")
                y -= 20
            else:
                c.drawString(50, y, "No devices need maintenance within the next 7 days.")
                y -= 20
            y -= 20

            # Flowchart
            y = draw_section_title("Processing Pipeline Flowchart", y)
            c.setFont("Helvetica", 10)
            flowchart = [
                ("1. Upload CSV File(s)", "User uploads log files in CSV format containing device usage data."),
                ("2. Validate Data", "Ensures all required columns (device_id, usage_hours, amc_date, status) are present and data types are correct (e.g., usage_hours as numeric, amc_date as date)."),
                ("3. Generate Usage Chart", "Creates a bar chart showing usage hours by device and status (e.g., ok, warning) to visualize usage patterns."),
                ("4. Detect Downtime Risks", "Uses Local Outlier Factor (LOF) algorithm to identify devices with unusual usage patterns by comparing local density of usage counts (contamination=0.1, n_neighbors=5)."),
                ("5. Check Maintenance Dates", "Identifies devices with AMC expiries within 7 days from 2025-06-05, calculating days left and urgency (urgent if ≤3 days)."),
                ("6. Create PDF Report", "Generates this PDF with a data table, downtime insights, maintenance alerts, and this detailed flowchart.")
            ]
            for step, description in flowchart:
                c.drawString(50, y, step)
                y -= 15
                c.setFont("Helvetica-Oblique", 9)
                c.drawString(70, y, description)
                c.setFont("Helvetica", 10)
                y -= 25
                if y < 50:
                    c.showPage()
                    y = height - 100
                    draw_header()
                    c.setFont("Helvetica", 10)

            c.showPage()
            c.save()
            return tmp.name
    except Exception as e:
        logging.error(f"Failed to generate PDF report: {str(e)}")
        return None

# Gradio interface
with gr.Blocks(css=css) as demo:
    gr.Markdown("# Multi-Device LabOps Dashboard")
    with gr.Row():
        file_input = gr.File(file_count="multiple", label="Upload Device Logs (CSV)")
        process_button = gr.Button("Process Logs")
    with gr.Row():
        output_summary = gr.Markdown(label="Dashboard Summary", elem_classes=["card"])
    with gr.Row(elem_classes=["dashboard-container"]):
        output_device_cards = gr.HTML(label="Device Overview")
    with gr.Row(elem_classes=["dashboard-container"]):
        with gr.Column():
            output_plot = gr.Image(label="Usage Chart", elem_classes=["card", "chart-container"])
        with gr.Column():
            output_message = gr.Textbox(label="Maintenance Alerts", elem_classes=["card", "alert-card"])
    with gr.Row(elem_classes=["dashboard-container"]):
        output_df = gr.Dataframe(label="Device Logs", elem_classes=["card", "dataframe-container"])
    with gr.Row(elem_classes=["dashboard-container"]):
        output_flowchart = gr.HTML(label="Processing Flowchart", elem_classes=["card", "flowchart-container"])
    with gr.Row(elem_classes=["dashboard-container"]):
        with gr.Column():
            output_pdf = gr.File(label="Download Detailed Report", elem_classes=["card"])
    process_button.click(
        fn=process_files,
        inputs=[file_input],
        outputs=[output_df, output_plot, output_pdf, output_message, output_summary, output_device_cards, output_flowchart]
    )

if __name__ == "__main__":
    logging.info("Application starting...")
    demo.launch(server_name="0.0.0.0", server_port=7860)