Skip to content

Commit 27c821e

Browse files
committed
fix the search functionality
1 parent 607ae6a commit 27c821e

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

dashboard/components.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,27 @@ def _create_search_form(batch_name):
164164
"""
165165
Create the search form for the dashboard.
166166
"""
167+
state = get_state()
168+
current_search = state.search_term.get(batch_name, "")
169+
167170
return Form(
168171
Input(
169-
type="text",
172+
type="search",
170173
name="search",
171174
placeholder="Search metrics...",
175+
value=current_search,
172176
cls="uk-input uk-form-small",
173177
style="width: 200px;",
174178
uk_tooltip="Filter metrics by name",
179+
autocomplete="off",
180+
aria_label="Search metrics",
175181
),
176-
hx_post=f"/batch/{batch_name}/search",
182+
hx_get=f"/batch/{batch_name}/search",
177183
hx_target="#charts-container",
178-
hx_trigger="keyup changed delay:500ms, search",
184+
hx_trigger="input changed delay:300ms, search",
185+
hx_indicator="#loading",
186+
hx_swap="outerHTML",
187+
onsubmit="return false;",
179188
)
180189

181190

dashboard/routes.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,32 +343,55 @@ def get(batch_name: str, start_index: int):
343343

344344

345345
@rt("/batch/{batch_name}/search")
346-
def post(batch_name: str, search: str = ""):
346+
def get(batch_name: str, search: str = ""):
347347
"""
348348
Search for a given batch name and search string.
349349
"""
350350
import re
351351

352+
# Store the search term in state
353+
app.state.search_term[batch_name] = search
354+
355+
# Ensure we have stats for this batch
356+
if batch_name not in app.state.stats_cache:
357+
app.state.calculate_metric_stats(batch_name)
358+
352359
metric_stats = app.state.stats_cache[batch_name]
353360
try:
354361
pattern = re.compile(search, re.IGNORECASE) if search else None
355-
filtered_stats = [
356-
stat
357-
for stat in metric_stats
362+
363+
# Keep track of original indices while filtering
364+
filtered_stats_with_indices = [
365+
(i, stat) # Keep original index with the stat
366+
for i, stat in enumerate(metric_stats)
358367
if not pattern or pattern.search(stat["metric_name"])
359368
]
360-
placeholders = []
361-
for i, stat in enumerate(filtered_stats):
362-
placeholders.append(
369+
370+
# Return empty state if no results
371+
if not filtered_stats_with_indices:
372+
return Div(
373+
P("No matching metrics found", cls="text-muted-foreground p-4 text-center"),
374+
id="charts-grid",
375+
)
376+
377+
# Create chart placeholders using original indices
378+
return Div(
379+
*[
363380
ChartManager.create_chart_placeholder(
364-
stat["metric_name"], i, batch_name
381+
stat["metric_name"],
382+
original_index, # Use the original index instead of enumeration
383+
batch_name
365384
)
366-
)
367-
return (
368-
Div(*placeholders) if placeholders else Div(P("No matching metrics found"))
385+
for original_index, stat in filtered_stats_with_indices
386+
],
387+
id="charts-grid",
388+
cls=f"grid grid-cols-{2 if app.state.two_columns else 1} gap-4"
369389
)
370390
except re.error:
371-
return Div(P("Invalid search pattern"))
391+
return Div(
392+
P("Invalid search pattern", cls="text-red-500 p-4 text-center"),
393+
id="charts-grid",
394+
)
372395

373396

374397
@rt("/batch/{batch_name}/update-n")

dashboard/state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(self):
3535
self.alert_max_n = {}
3636
self.line_width = 2
3737
self.show_legend = False
38+
self.search_term = {}
3839

3940
def clear_batch_cache(self, batch_name):
4041
"""

0 commit comments

Comments
 (0)