-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_builder.py
More file actions
337 lines (286 loc) · 15.2 KB
/
Copy pathreport_builder.py
File metadata and controls
337 lines (286 loc) · 15.2 KB
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
"""
report_builder.py — Excel report generation for LCY3 AFM Dashboard
"""
import io
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
from openpyxl.utils import get_column_letter
# ── Styles ────────────────────────────────────────────────────────────────────
HDR_FILL = PatternFill("solid", fgColor="1A237E")
HDR_FONT = Font(color="FFFFFF", bold=True, size=10)
GRAND_FILL = PatternFill("solid", fgColor="E8EAF6")
GRAND_FONT = Font(color="1A237E", bold=True, size=10)
RED_FILL = PatternFill("solid", fgColor="D22828")
ORG_FILL = PatternFill("solid", fgColor="FF8C00")
GRN_FILL = PatternFill("solid", fgColor="3CB44B")
WHT_FONT = Font(color="FFFFFF", bold=True, size=10)
BLK_FONT = Font(color="000000", bold=True, size=10)
THIN = Side(style="thin", color="C5CAE9")
BORDER = Border(left=THIN, right=THIN, top=THIN, bottom=THIN)
CENTER = Alignment(horizontal="center", vertical="center", wrap_text=True)
DEFAULT_THRESHOLD = 5
THRESHOLDS = {"Amnesty": 10, "Drive Lacking Capability": 10}
NO_THRESHOLD = ["Unreachable Charger"]
def _get_threshold(andon_type):
if andon_type in NO_THRESHOLD:
return None
return THRESHOLDS.get(andon_type, DEFAULT_THRESHOLD)
def _auto_width(ws):
for col in ws.columns:
max_len = 0
col_letter = get_column_letter(col[0].column)
for cell in col:
try:
max_len = max(max_len, len(str(cell.value or "")))
except Exception:
pass
ws.column_dimensions[col_letter].width = min(max_len + 4, 40)
def _header_row(ws, values, row=1):
for c, v in enumerate(values, 1):
cell = ws.cell(row=row, column=c, value=v)
cell.fill = HDR_FILL
cell.font = HDR_FONT
cell.alignment = CENTER
cell.border = BORDER
def _style_cell_border(ws, row, col):
ws.cell(row=row, column=col).border = BORDER
def _colour_avg(cell, val, threshold):
if val is None or (isinstance(val, float) and pd.isna(val)) or threshold is None:
return
if val > threshold * 1.5:
cell.fill = RED_FILL; cell.font = WHT_FONT
elif val > threshold:
cell.fill = ORG_FILL; cell.font = BLK_FONT
else:
cell.fill = GRN_FILL; cell.font = WHT_FONT
# ── Sheet: Summary KPIs ───────────────────────────────────────────────────────
def _sheet_kpis(wb, fdf, uploaded_files, within_threshold_fn):
ws = wb.create_sheet("Summary KPIs")
within_pct = fdf.apply(within_threshold_fn, axis=1).mean() * 100
eff = (fdf.groupby("Resolver").agg(n=("Resolve_Min", "count"), avg=("Resolve_Min", "mean"))
.apply(lambda r: r["n"] / r["avg"] if r["avg"] > 0 else 0, axis=1).mean())
_header_row(ws, ["Metric", "Value"], row=1)
rows = [
("Total Andons", len(fdf)),
("Avg Resolve Time (min)", round(fdf["Resolve_Min"].mean(), 2)),
("Median Resolve Time (min)",round(fdf["Resolve_Min"].median(), 2)),
("% Within Threshold", round(within_pct, 1)),
("Avg Efficiency Score", round(eff, 1)),
("Files Uploaded", len(uploaded_files)),
("Date Range", f"{fdf['Date'].min()} → {fdf['Date'].max()}"),
]
for r, (m, v) in enumerate(rows, 2):
ws.cell(row=r, column=1, value=m).border = BORDER
ws.cell(row=r, column=2, value=v).border = BORDER
_auto_width(ws)
# ── Sheet: Leaderboard ────────────────────────────────────────────────────────
def _sheet_leaderboard(wb, fdf, within_threshold_fn):
ws = wb.create_sheet("Leaderboard")
lb = (fdf.groupby("Resolver")
.agg(Total_Andons=("Resolve_Min", "count"), Avg_Time=("Resolve_Min", "mean"))
.reset_index())
lb["Avg_Time"] = lb["Avg_Time"].round(2)
lb["Efficiency"] = (lb["Total_Andons"] / lb["Avg_Time"]).round(2)
lb["Within_Threshold"] = fdf.groupby("Resolver").apply(
lambda g: g.apply(within_threshold_fn, axis=1).mean() * 100
).round(1).values
lb = lb.sort_values("Avg_Time").reset_index(drop=True)
lb.index += 1
headers = ["Rank", "Resolver", "Total Andons", "Avg Time (min)", "Efficiency Score", "% Within Threshold"]
_header_row(ws, headers, row=1)
for rank, row in lb.iterrows():
r = rank + 1
ws.cell(row=r, column=1, value=rank).border = BORDER
ws.cell(row=r, column=2, value=row["Resolver"]).border = BORDER
ws.cell(row=r, column=3, value=int(row["Total_Andons"])).border = BORDER
avg_cell = ws.cell(row=r, column=4, value=row["Avg_Time"])
avg_cell.border = BORDER
_colour_avg(avg_cell, row["Avg_Time"], DEFAULT_THRESHOLD)
ws.cell(row=r, column=5, value=row["Efficiency"]).border = BORDER
ws.cell(row=r, column=6, value=round(row["Within_Threshold"], 1)).border = BORDER
_auto_width(ws)
# ── Sheet: AFM Performance ────────────────────────────────────────────────────
def _sheet_afm_performance(wb, fdf):
ws = wb.create_sheet("AFM Performance")
andon_types = sorted(fdf["Andon Type"].dropna().unique())
all_resolvers = sorted(fdf["Resolver"].unique())
# Build header
headers = ["Resolver"]
for at in andon_types:
headers += [f"{at} — Count", f"{at} — Avg (min)"]
headers += ["Total Count", "Total Avg (min)"]
_header_row(ws, headers, row=1)
cp = fdf.pivot_table(index="Resolver", columns="Andon Type",
values="Resolve_Min", aggfunc="count", fill_value=0).reindex(all_resolvers, fill_value=0)
ap = fdf.pivot_table(index="Resolver", columns="Andon Type",
values="Resolve_Min", aggfunc="mean").round(2).reindex(all_resolvers)
for r, res in enumerate(all_resolvers, 2):
col = 1
ws.cell(row=r, column=col, value=res).border = BORDER; col += 1
for at in andon_types:
cnt_val = int(cp.loc[res, at]) if at in cp.columns else 0
avg_val = round(ap.loc[res, at], 2) if at in ap.columns and not pd.isna(ap.loc[res, at]) else None
ws.cell(row=r, column=col, value=cnt_val).border = BORDER; col += 1
avg_cell = ws.cell(row=r, column=col, value=avg_val)
avg_cell.border = BORDER
if avg_val:
_colour_avg(avg_cell, avg_val, DEFAULT_THRESHOLD)
col += 1
total_cnt = int(cp.loc[res, andon_types].sum()) if len(andon_types) > 0 else 0
total_avg = round(fdf[fdf["Resolver"] == res]["Resolve_Min"].mean(), 2)
ws.cell(row=r, column=col, value=total_cnt).border = BORDER; col += 1
avg_cell = ws.cell(row=r, column=col, value=total_avg)
avg_cell.border = BORDER
_colour_avg(avg_cell, total_avg, DEFAULT_THRESHOLD)
# Grand total row
gr = len(all_resolvers) + 2
ws.cell(row=gr, column=1, value="Grand Total").fill = GRAND_FILL
ws.cell(row=gr, column=1).font = GRAND_FONT
ws.cell(row=gr, column=1).border = BORDER
col = 2
for at in andon_types:
sub = fdf[fdf["Andon Type"] == at]
ws.cell(row=gr, column=col, value=int(sub["Resolve_Min"].count())).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=round(sub["Resolve_Min"].mean(), 2)).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=int(fdf["Resolve_Min"].count())).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=round(fdf["Resolve_Min"].mean(), 2)).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER
_auto_width(ws)
# ── Sheet: Andons by Type ─────────────────────────────────────────────────────
def _sheet_by_type(wb, fdf):
ws = wb.create_sheet("Andons by Type")
tc = fdf["Andon Type"].value_counts().reset_index()
tc.columns = ["Andon Type", "Count"]
tc["% of Total"] = (tc["Count"] / tc["Count"].sum() * 100).round(1)
tc["Avg Time (min)"] = tc["Andon Type"].map(fdf.groupby("Andon Type")["Resolve_Min"].mean().round(2))
_header_row(ws, ["Andon Type", "Count", "% of Total", "Avg Time (min)"], row=1)
for r, row in tc.iterrows():
ws.cell(row=r+2, column=1, value=row["Andon Type"]).border = BORDER
ws.cell(row=r+2, column=2, value=int(row["Count"])).border = BORDER
ws.cell(row=r+2, column=3, value=row["% of Total"]).border = BORDER
avg_cell = ws.cell(row=r+2, column=4, value=row["Avg Time (min)"])
avg_cell.border = BORDER
t = _get_threshold(row["Andon Type"])
_colour_avg(avg_cell, row["Avg Time (min)"], t)
_auto_width(ws)
# ── Sheet: Weekly Breakdown ───────────────────────────────────────────────────
def _sheet_weekly(wb, fdf):
ws = wb.create_sheet("Weekly Breakdown")
weeks_avail = sorted(fdf["Week"].dropna().unique(), reverse=True)
headers = ["Andon Type"]
for w in weeks_avail:
headers += [f"Wk {w} — Andons", f"Wk {w} — Avg (min)"]
headers += ["Total Andons", "Total Avg (min)"]
_header_row(ws, headers, row=1)
andon_types = sorted(fdf["Andon Type"].dropna().unique())
for r, at in enumerate(andon_types, 2):
col = 1
ws.cell(row=r, column=col, value=at).border = BORDER; col += 1
for w in weeks_avail:
sub_w = fdf[(fdf["Week"] == w) & (fdf["Andon Type"] == at)]
cnt = int(sub_w["Resolve_Min"].count())
avg = round(sub_w["Resolve_Min"].mean(), 2) if cnt > 0 else None
ws.cell(row=r, column=col, value=cnt).border = BORDER; col += 1
avg_cell = ws.cell(row=r, column=col, value=avg)
avg_cell.border = BORDER
if avg:
_colour_avg(avg_cell, avg, DEFAULT_THRESHOLD)
col += 1
sub_at = fdf[fdf["Andon Type"] == at]
ws.cell(row=r, column=col, value=int(sub_at["Resolve_Min"].count())).border = BORDER; col += 1
t_avg = round(sub_at["Resolve_Min"].mean(), 2)
avg_cell = ws.cell(row=r, column=col, value=t_avg)
avg_cell.border = BORDER
_colour_avg(avg_cell, t_avg, _get_threshold(at))
# Grand total
gr = len(andon_types) + 2
ws.cell(row=gr, column=1, value="Grand Total").fill = GRAND_FILL
ws.cell(row=gr, column=1).font = GRAND_FONT
ws.cell(row=gr, column=1).border = BORDER
col = 2
for w in weeks_avail:
sub_w = fdf[fdf["Week"] == w]
ws.cell(row=gr, column=col, value=int(sub_w["Resolve_Min"].count())).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=round(sub_w["Resolve_Min"].mean(), 2)).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=int(fdf["Resolve_Min"].count())).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER; col += 1
ws.cell(row=gr, column=col, value=round(fdf["Resolve_Min"].mean(), 2)).fill = GRAND_FILL
ws.cell(row=gr, column=col).font = GRAND_FONT
ws.cell(row=gr, column=col).border = BORDER
_auto_width(ws)
# ── Sheet: Raw Data ───────────────────────────────────────────────────────────
def _sheet_raw(wb, fdf):
ws = wb.create_sheet("Raw Data")
cols = [c for c in fdf.columns if not c.startswith("_")]
_header_row(ws, cols, row=1)
for r, (_, row) in enumerate(fdf[cols].iterrows(), 2):
for c, val in enumerate(row, 1):
cell = ws.cell(row=r, column=c, value=str(val) if not isinstance(val, (int, float)) else val)
cell.border = BORDER
_auto_width(ws)
# ── Sheet: System vs Non-System ───────────────────────────────────────────────
def _sheet_sys_nonsys(wb, fdf):
ws = wb.create_sheet("System vs Non-System")
fdf2 = fdf.copy()
fdf2["_rtype"] = fdf2["Resolver"].apply(lambda r: "System" if r == "System" else "Non-System")
weeks_avail = sorted(fdf2["Week"].dropna().unique(), reverse=True)
headers = ["Resolver Type"]
for w in weeks_avail:
headers += [f"Wk {w} — Andons", f"Wk {w} — Avg (min)"]
headers += ["Total Andons", "Total Avg (min)", "% of Total"]
_header_row(ws, headers, row=1)
for r, rtype in enumerate(["System", "Non-System"], 2):
sub = fdf2[fdf2["_rtype"] == rtype]
col = 1
ws.cell(row=r, column=col, value=rtype).border = BORDER; col += 1
for w in weeks_avail:
sub_w = sub[sub["Week"] == w]
cnt = int(sub_w["Resolve_Min"].count())
avg = round(sub_w["Resolve_Min"].mean(), 2) if cnt > 0 else None
ws.cell(row=r, column=col, value=cnt).border = BORDER; col += 1
ws.cell(row=r, column=col, value=avg).border = BORDER; col += 1
total = int(sub["Resolve_Min"].count())
total_avg = round(sub["Resolve_Min"].mean(), 2) if total > 0 else None
pct = round(total / len(fdf2) * 100, 1)
ws.cell(row=r, column=col, value=total).border = BORDER; col += 1
ws.cell(row=r, column=col, value=total_avg).border = BORDER; col += 1
ws.cell(row=r, column=col, value=pct).border = BORDER
_auto_width(ws)
# ── Public API ────────────────────────────────────────────────────────────────
def build_daily_report(fdf, uploaded_files, within_threshold_fn) -> bytes:
wb = Workbook()
wb.remove(wb.active) # remove default sheet
_sheet_kpis(wb, fdf, uploaded_files, within_threshold_fn)
_sheet_afm_performance(wb, fdf)
_sheet_by_type(wb, fdf)
_sheet_leaderboard(wb, fdf, within_threshold_fn)
_sheet_raw(wb, fdf)
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
return buf.getvalue()
def build_weekly_report(fdf, uploaded_files, within_threshold_fn) -> bytes:
wb = Workbook()
wb.remove(wb.active)
_sheet_kpis(wb, fdf, uploaded_files, within_threshold_fn)
_sheet_weekly(wb, fdf)
_sheet_afm_performance(wb, fdf)
_sheet_sys_nonsys(wb, fdf)
_sheet_leaderboard(wb, fdf, within_threshold_fn)
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
return buf.getvalue()