@@ -50,6 +50,18 @@ def index(request: Request):
5050 else :
5151 df = app .state .df_cache [batch_name ]
5252
53+ # Calculate average score and alert count for the batch, handling NaN values
54+ avg_score = (
55+ df ["metric_score" ].fillna (0 ).mean ()
56+ if not df .empty and "metric_score" in df .columns
57+ else 0
58+ )
59+ alert_count = (
60+ df ["metric_alert" ].fillna (0 ).sum ()
61+ if not df .empty and "metric_alert" in df .columns
62+ else 0
63+ )
64+
5365 latest_timestamp = df ["metric_timestamp" ].max () if not df .empty else "No data"
5466 if latest_timestamp != "No data" :
5567 from datetime import datetime
@@ -81,8 +93,19 @@ def index(request: Request):
8193 batch_stats [batch_name ] = {
8294 "unique_metrics" : len (df ["metric_name" ].unique ()),
8395 "latest_timestamp" : latest_timestamp ,
96+ "avg_score" : avg_score ,
97+ "alert_count" : alert_count # Add alert count to stats
8498 }
8599
100+ # Sort the metric batches by alert count (primary) and avg score (secondary)
101+ sorted_batch_names = sorted (
102+ app .state .metric_batches ,
103+ key = lambda x : (
104+ - batch_stats [x ]["alert_count" ], # Negative for descending order
105+ - batch_stats [x ]["avg_score" ] # Negative for descending order
106+ )
107+ )
108+
86109 main_content = Div (
87110 Card (
88111 DivLAligned (
@@ -145,12 +168,28 @@ def index(request: Request):
145168 ),
146169 cls = "space-x-2" ,
147170 ),
171+ DivLAligned (
172+ UkIcon ("bar-chart" , cls = "text-purple-500" ),
173+ P (
174+ f"Avg Score: { batch_stats [batch_name ]['avg_score' ]:.1%} " ,
175+ cls = TextPresets .muted_sm ,
176+ ),
177+ cls = "space-x-2" ,
178+ ),
179+ DivLAligned (
180+ UkIcon ("alert-circle" , cls = "text-red-500" ),
181+ P (
182+ f"{ batch_stats [batch_name ]['alert_count' ]} alerts" ,
183+ cls = TextPresets .muted_sm ,
184+ ),
185+ cls = "space-x-2" ,
186+ ),
148187 cls = "space-y-2" ,
149188 )
150189 ),
151190 ),
152191 Button (
153- "View Metrics" ,
192+ batch_name ,
154193 hx_get = f"/batch/{ batch_name } " ,
155194 hx_push_url = f"/batch/{ batch_name } " ,
156195 hx_target = "#main-content" ,
@@ -162,7 +201,7 @@ def index(request: Request):
162201 ),
163202 cls = "p-6 hover:border-primary transition-colors duration-200" ,
164203 )
165- for batch_name in app .state .metric_batches
204+ for batch_name in sorted_batch_names # Use sorted list instead of app.state.metric_batches
166205 ],
167206 cols = 3 ,
168207 gap = 4 ,
0 commit comments