add result sorting controls
This commit is contained in:
64
app/main.py
64
app/main.py
@@ -23,6 +23,15 @@ WORK_ROOT.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
APP_TITLE = "\u68c0\u6d4b\u6570\u636e\u5904\u7406"
|
||||
UNMATCHED = "\u672a\u5339\u914d\u68c0\u6d4b\u5185\u5bb9"
|
||||
SORT_OPTIONS = {
|
||||
"name": "\u59d3\u540d",
|
||||
"sample_time": "\u91c7\u6837\u65f6\u95f4",
|
||||
"reason": "\u68c0\u6d4b\u539f\u56e0",
|
||||
}
|
||||
SORT_ORDERS = {
|
||||
"asc": "\u5347\u5e8f",
|
||||
"desc": "\u964d\u5e8f",
|
||||
}
|
||||
|
||||
app = FastAPI(title=APP_TITLE)
|
||||
|
||||
@@ -117,16 +126,22 @@ def result_page(
|
||||
include_basic_sheets: bool = Query(True),
|
||||
include_unmatched_items: bool = Query(True),
|
||||
include_summary_sheet: bool = Query(True),
|
||||
sort_by: str = Query("sample_time"),
|
||||
sort_order: str = Query("asc"),
|
||||
) -> str:
|
||||
job_dir = _get_job_dir(job_id)
|
||||
try:
|
||||
rows = _clean_preview_rows(preview_rows)
|
||||
clean_sort_by = _clean_sort_by(sort_by)
|
||||
clean_sort_order = _clean_sort_order(sort_order)
|
||||
result = summarize_job(
|
||||
job_dir,
|
||||
rows,
|
||||
include_basic_sheets=include_basic_sheets,
|
||||
include_unmatched_items=include_unmatched_items,
|
||||
include_summary_sheet=include_summary_sheet,
|
||||
sort_by=clean_sort_by,
|
||||
sort_order=clean_sort_order,
|
||||
)
|
||||
except ProcessingError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
@@ -136,6 +151,8 @@ def result_page(
|
||||
include_basic_sheets=include_basic_sheets,
|
||||
include_unmatched_items=include_unmatched_items,
|
||||
include_summary_sheet=include_summary_sheet,
|
||||
sort_by=clean_sort_by,
|
||||
sort_order=clean_sort_order,
|
||||
)
|
||||
|
||||
|
||||
@@ -145,6 +162,8 @@ def download_all(
|
||||
include_basic_sheets: bool = Query(True),
|
||||
include_unmatched_items: bool = Query(True),
|
||||
include_summary_sheet: bool = Query(True),
|
||||
sort_by: str = Query("sample_time"),
|
||||
sort_order: str = Query("asc"),
|
||||
) -> FileResponse:
|
||||
job_dir = _get_job_dir(job_id)
|
||||
try:
|
||||
@@ -153,6 +172,8 @@ def download_all(
|
||||
include_basic_sheets=include_basic_sheets,
|
||||
include_unmatched_items=include_unmatched_items,
|
||||
include_summary_sheet=include_summary_sheet,
|
||||
sort_by=_clean_sort_by(sort_by),
|
||||
sort_order=_clean_sort_order(sort_order),
|
||||
)
|
||||
except ProcessingError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
@@ -166,6 +187,8 @@ def download_file(
|
||||
include_basic_sheets: bool = Query(True),
|
||||
include_unmatched_items: bool = Query(True),
|
||||
include_summary_sheet: bool = Query(True),
|
||||
sort_by: str = Query("sample_time"),
|
||||
sort_order: str = Query("asc"),
|
||||
) -> FileResponse:
|
||||
job_dir = _get_job_dir(job_id)
|
||||
try:
|
||||
@@ -175,6 +198,8 @@ def download_file(
|
||||
include_basic_sheets=include_basic_sheets,
|
||||
include_unmatched_items=include_unmatched_items,
|
||||
include_summary_sheet=include_summary_sheet,
|
||||
sort_by=_clean_sort_by(sort_by),
|
||||
sort_order=_clean_sort_order(sort_order),
|
||||
)
|
||||
except ProcessingError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
@@ -207,12 +232,26 @@ def _checked(value: bool) -> str:
|
||||
return "checked" if value else ""
|
||||
|
||||
|
||||
def _selected(value: str, current: str) -> str:
|
||||
return "selected" if value == current else ""
|
||||
|
||||
|
||||
def _clean_sort_by(sort_by: str) -> str:
|
||||
return sort_by if sort_by in SORT_OPTIONS else "sample_time"
|
||||
|
||||
|
||||
def _clean_sort_order(sort_order: str) -> str:
|
||||
return sort_order if sort_order in SORT_ORDERS else "asc"
|
||||
|
||||
|
||||
def _render_result(
|
||||
result: ProcessingResult,
|
||||
preview_rows: int = 20,
|
||||
include_basic_sheets: bool = True,
|
||||
include_unmatched_items: bool = True,
|
||||
include_summary_sheet: bool = True,
|
||||
sort_by: str = "sample_time",
|
||||
sort_order: str = "asc",
|
||||
) -> str:
|
||||
total_sheets = sum(len(file.sheets) for file in result.files)
|
||||
total_rows = sum(max(sheet.rows - 1, 0) for file in result.files for sheet in file.sheets)
|
||||
@@ -230,6 +269,16 @@ def _render_result(
|
||||
<label><input class="export-option" type="checkbox" data-param="include_basic_sheets" {_checked(include_basic_sheets)}> \u57fa\u672c\u5de5\u4f5c\u8868</label>
|
||||
<label><input class="export-option" type="checkbox" data-param="include_unmatched_items" {_checked(include_unmatched_items)}> \u672a\u5339\u914d\u68c0\u6d4b\u5185\u5bb9\u9879</label>
|
||||
<label><input class="export-option" type="checkbox" data-param="include_summary_sheet" {_checked(include_summary_sheet)}> \u672a\u68c0\u6d4b\u5230\u5185\u5bb9\u6c47\u603b\u8868</label>
|
||||
<label for="sort_by">\u6392\u5e8f</label>
|
||||
<select id="sort_by" class="result-option" data-param="sort_by">
|
||||
<option value="name" {_selected("name", sort_by)}>\u6309\u59d3\u540d</option>
|
||||
<option value="sample_time" {_selected("sample_time", sort_by)}>\u6309\u91c7\u6837\u65f6\u95f4</option>
|
||||
<option value="reason" {_selected("reason", sort_by)}>\u6309\u68c0\u6d4b\u539f\u56e0</option>
|
||||
</select>
|
||||
<select id="sort_order" class="result-option" data-param="sort_order">
|
||||
<option value="asc" {_selected("asc", sort_order)}>\u5347\u5e8f</option>
|
||||
<option value="desc" {_selected("desc", sort_order)}>\u964d\u5e8f</option>
|
||||
</select>
|
||||
</section>
|
||||
<form class="row-control" method="get" action="/result/{html.escape(result.job_id)}">
|
||||
<label for="preview_rows">\u6bcf\u4e2a\u5de5\u4f5c\u8868\u9884\u89c8\u884c\u6570</label>
|
||||
@@ -411,6 +460,11 @@ def _page_shell(
|
||||
margin: 0;
|
||||
font-weight: 500;
|
||||
}}
|
||||
.export-options select {{
|
||||
width: auto;
|
||||
min-width: 128px;
|
||||
padding: 8px 10px;
|
||||
}}
|
||||
.checks label {{
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
@@ -591,6 +645,9 @@ def _page_shell(
|
||||
const options = Array.from(document.querySelectorAll('.export-option'));
|
||||
const params = new URLSearchParams();
|
||||
options.forEach((option) => params.set(option.dataset.param, option.checked ? 'true' : 'false'));
|
||||
document.querySelectorAll('.result-option').forEach((option) => {{
|
||||
params.set(option.dataset.param, option.value);
|
||||
}});
|
||||
document.querySelectorAll('.export-link').forEach((link) => {{
|
||||
const base = link.dataset.base;
|
||||
const sep = base.includes('?') ? '&' : '?';
|
||||
@@ -606,6 +663,9 @@ def _page_shell(
|
||||
document.querySelectorAll('.export-option').forEach((option) => {{
|
||||
url.searchParams.set(option.dataset.param, option.checked ? 'true' : 'false');
|
||||
}});
|
||||
document.querySelectorAll('.result-option').forEach((option) => {{
|
||||
url.searchParams.set(option.dataset.param, option.value);
|
||||
}});
|
||||
window.location.href = url.toString();
|
||||
}}
|
||||
const resultForm = document.querySelector('.row-control');
|
||||
@@ -619,6 +679,10 @@ def _page_shell(
|
||||
updateExportLinks();
|
||||
refreshResultWithOptions();
|
||||
}}));
|
||||
document.querySelectorAll('.result-option').forEach((option) => option.addEventListener('change', () => {{
|
||||
updateExportLinks();
|
||||
refreshResultWithOptions();
|
||||
}}));
|
||||
updateExportLinks();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user