RathodHarish commited on
Commit
20b0648
·
verified ·
1 Parent(s): ddc52ac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import plotly.express as px
4
+ from weasyprint import HTML
5
+ import datetime
6
+ import os
7
+
8
+ # Cache data loading for performance
9
+ def load_data():
10
+ logs = pd.concat([
11
+ pd.read_csv("data/smartlog.csv"),
12
+ pd.read_csv("data/cell_analysis.csv"),
13
+ pd.read_csv("data/weight_log.csv"),
14
+ pd.read_csv("data/uv_verification.csv")
15
+ ])
16
+ logs["timestamp"] = pd.to_datetime(logs["timestamp"])
17
+ equipment = pd.read_csv("data/equipment.csv")
18
+ equipment["amc_expiry"] = pd.to_datetime(equipment["amc_expiry"])
19
+ return logs, equipment
20
+
21
+ # Generate PDF report
22
+ def generate_pdf_report(data):
23
+ os.makedirs("reports", exist_ok=True)
24
+ html = f"""
25
+ <h1>LabOps Dashboard Report</h1>
26
+ <h2>Filtered Data</h2>
27
+ {data.to_html()}
28
+ """
29
+ filename = f"reports/report_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
30
+ HTML(string=html).write_pdf(filename)
31
+ return filename
32
+
33
+ # Main dashboard function
34
+ def render_dashboard(lab, device_type, date_start, date_end, refresh=False):
35
+ logs, equipment = load_data()
36
+
37
+ # Apply filters
38
+ filtered_logs = logs
39
+ if lab != "All":
40
+ device_ids = equipment[equipment["lab_id"] == lab]["device_id"]
41
+ filtered_logs = filtered_logs[filtered_logs["device_id"].isin(device_ids)]
42
+ if device_type != "All":
43
+ device_ids = equipment[equipment["type"] == device_type]["device_id"]
44
+ filtered_logs = filtered_logs[filtered_logs["device_id"].isin(device_ids)]
45
+ filtered_logs = filtered_logs[
46
+ (filtered_logs["timestamp"].dt.date >= pd.to_datetime(date_start).date()) &
47
+ (filtered_logs["timestamp"].dt.date <= pd.to_datetime(date_end).date())
48
+ ]
49
+
50
+ # Device Cards (FR-002)
51
+ device_cards = ""
52
+ for device_id in filtered_logs["device_id"].unique():
53
+ device_data = filtered_logs[filtered_logs["device_id"] == device_id]
54
+ if not device_data.empty:
55
+ status = device_data["status"].iloc[-1]
56
+ color = "green" if status == "UP" else "red"
57
+ device_cards += f"""
58
+ <div style='border: 1px solid {color}; padding: 10px; margin: 10px; border-radius: 5px;'>
59
+ <h3>Device: {device_id}</h3>
60
+ <p>Status: <span style='color: {color};'>{status}</span></p>
61
+ <p>Last Log: {device_data['timestamp'].iloc[-1]}</p>
62
+ <p>Usage Count: {len(device_data)}</p>
63
+ </div>
64
+ """
65
+
66
+ # Charts (FR-003)
67
+ downtime = filtered_logs[filtered_logs["status"] == "DOWN"].groupby("device_id").size().reset_index(name="downtime_count")
68
+ downtime_fig = px.bar(downtime, x="device_id", y="downtime_count", title="Downtime Events by Device")
69
+
70
+ usage = filtered_logs.groupby([filtered_logs["timestamp"].dt.date, "device_id"])["metrics"].mean().reset_index()
71
+ usage_fig = px.line(usage, x="timestamp", y="metrics", color="device_id", title="Daily Usage Metrics")
72
+
73
+ # AMC Reminders (FR-004)
74
+ today = datetime.datetime.now().date()
75
+ two_weeks = today + datetime.timedelta(days=14)
76
+ amc_alerts = equipment[equipment["amc_expiry"].dt.date <= two_weeks]
77
+ amc_text = amc_alerts[["device_id", "type", "lab_id", "amc_expiry"]] if not amc_alerts.empty else "No AMC expirations within 2 weeks."
78
+
79
+ # PDF Export (FR-004)
80
+ report_file = generate_pdf_report(filtered_logs)
81
+
82
+ return device_cards, downtime_fig, usage_fig, amc_text, report_file
83
+
84
+ # Gradio interface
85
+ with gr.Blocks(css=".gradio-container {max-width: 100%;}") as demo:
86
+ gr.Markdown("# LabOps Dashboard")
87
+
88
+ # Filters
89
+ with gr.Row():
90
+ lab = gr.Dropdown(choices=["All"] + list(pd.read_csv("data/equipment.csv")["lab_id"].unique()), label="Select Lab", value="All")
91
+ device_type = gr.Dropdown(choices=["All"] + list(pd.read_csv("data/equipment.csv")["type"].unique()), label="Device Type", value="All")
92
+ with gr.Row():
93
+ date_start = gr.DateTime(label="Start Date", value=pd.read_csv("data/smartlog.csv")["timestamp"].min())
94
+ date_end = gr.DateTime(label="End Date", value=pd.read_csv("data/smartlog.csv")["timestamp"].max())
95
+
96
+ refresh_btn = gr.Button("Refresh")
97
+
98
+ # Outputs
99
+ device_cards = gr.HTML(label="Device Status")
100
+ downtime_plot = gr.Plot(label="Downtime Trends")
101
+ usage_plot = gr.Plot(label="Usage Trends")
102
+ amc_reminders = gr.Textbox(label="AMC Expiry Reminders")
103
+ report_download = gr.File(label="Download Report")
104
+
105
+ # Bind inputs to outputs
106
+ inputs = [lab, device_type, date_start, date_end, refresh_btn]
107
+ outputs = [device_cards, downtime_plot, usage_plot, amc_reminders, report_download]
108
+ for input_component in inputs:
109
+ input_component.change(fn=render_dashboard, inputs=[lab, device_type, date_start, date_end], outputs=outputs)
110
+
111
+ demo.launch()