feat(search): support platform and benchmark config filters
Some checks failed
Build and Publish Docker Image / deploy (push) Failing after 1m2s
Some checks failed
Build and Publish Docker Image / deploy (push) Failing after 1m2s
Add platform handling to submissions and persist a normalized value (`windows`, `linux`, `macos`) with a default of `windows` when omitted. Extend search/index filtering to support `thread`, `platform`, `intensity`, and `durationSecs` alongside existing text/CPU token matching, and wire these params through request parsing, page data, and navigation URLs. Update API/README docs and examples to reflect the new submission inputs and search capabilities so users can run more precise queries.feat(search): support platform and benchmark config filters Add platform handling to submissions and persist a normalized value (`windows`, `linux`, `macos`) with a default of `windows` when omitted. Extend search/index filtering to support `thread`, `platform`, `intensity`, and `durationSecs` alongside existing text/CPU token matching, and wire these params through request parsing, page data, and navigation URLs. Update API/README docs and examples to reflect the new submission inputs and search capabilities so users can run more precise queries.
This commit is contained in:
147
lib/web/app.go
147
lib/web/app.go
@@ -28,20 +28,25 @@ type App struct {
|
||||
}
|
||||
|
||||
type indexPageData struct {
|
||||
Submissions []model.Submission
|
||||
QueryText string
|
||||
QueryCPU string
|
||||
Page int
|
||||
TotalPages int
|
||||
TotalCount int
|
||||
ShowingFrom int
|
||||
ShowingTo int
|
||||
PrevURL string
|
||||
NextURL string
|
||||
Submissions []model.Submission
|
||||
QueryText string
|
||||
QueryCPU string
|
||||
QueryThread string
|
||||
QueryPlatform string
|
||||
QueryIntensity int
|
||||
QueryDuration int
|
||||
Page int
|
||||
TotalPages int
|
||||
TotalCount int
|
||||
ShowingFrom int
|
||||
ShowingTo int
|
||||
PrevURL string
|
||||
NextURL string
|
||||
}
|
||||
|
||||
type jsonSubmissionEnvelope struct {
|
||||
Submitter string `json:"submitter"`
|
||||
Platform string `json:"platform"`
|
||||
Benchmark *model.BenchmarkResult `json:"benchmark"`
|
||||
Result *model.BenchmarkResult `json:"result"`
|
||||
Data *model.BenchmarkResult `json:"data"`
|
||||
@@ -49,6 +54,7 @@ type jsonSubmissionEnvelope struct {
|
||||
|
||||
type flatSubmissionEnvelope struct {
|
||||
Submitter string `json:"submitter"`
|
||||
Platform string `json:"platform"`
|
||||
model.BenchmarkResult
|
||||
}
|
||||
|
||||
@@ -96,14 +102,18 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
page := parsePositiveInt(r.URL.Query().Get("page"), 1)
|
||||
text := strings.TrimSpace(r.URL.Query().Get("text"))
|
||||
cpu := strings.TrimSpace(r.URL.Query().Get("cpu"))
|
||||
thread := strings.TrimSpace(r.URL.Query().Get("thread"))
|
||||
platform := strings.TrimSpace(r.URL.Query().Get("platform"))
|
||||
intensity := parsePositiveInt(r.URL.Query().Get("intensity"), 0)
|
||||
durationSecs := parsePositiveInt(r.URL.Query().Get("durationSecs"), 0)
|
||||
|
||||
var (
|
||||
submissions []model.Submission
|
||||
totalCount int
|
||||
)
|
||||
|
||||
if text != "" || cpu != "" {
|
||||
matches := a.store.SearchSubmissions(text, cpu)
|
||||
if text != "" || cpu != "" || thread != "" || platform != "" || intensity > 0 || durationSecs > 0 {
|
||||
matches := a.store.SearchSubmissions(text, cpu, thread, platform, intensity, durationSecs)
|
||||
totalCount = len(matches)
|
||||
start, end, normalizedPage := pageBounds(page, a.pageSize, totalCount)
|
||||
page = normalizedPage
|
||||
@@ -119,16 +129,20 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
showingFrom, showingTo := visibleBounds(page, a.pageSize, len(submissions), totalCount)
|
||||
|
||||
data := indexPageData{
|
||||
Submissions: submissions,
|
||||
QueryText: text,
|
||||
QueryCPU: cpu,
|
||||
Page: page,
|
||||
TotalPages: totalPageCount,
|
||||
TotalCount: totalCount,
|
||||
ShowingFrom: showingFrom,
|
||||
ShowingTo: showingTo,
|
||||
PrevURL: buildIndexURL(max(1, page-1), text, cpu),
|
||||
NextURL: buildIndexURL(max(1, min(totalPageCount, page+1)), text, cpu),
|
||||
Submissions: submissions,
|
||||
QueryText: text,
|
||||
QueryCPU: cpu,
|
||||
QueryThread: normalizeThreadFilter(thread),
|
||||
QueryPlatform: normalizePlatformFilter(platform),
|
||||
QueryIntensity: intensity,
|
||||
QueryDuration: durationSecs,
|
||||
Page: page,
|
||||
TotalPages: totalPageCount,
|
||||
TotalCount: totalCount,
|
||||
ShowingFrom: showingFrom,
|
||||
ShowingTo: showingTo,
|
||||
PrevURL: buildIndexURL(max(1, page-1), text, cpu, thread, platform, intensity, durationSecs),
|
||||
NextURL: buildIndexURL(max(1, min(totalPageCount, page+1)), text, cpu, thread, platform, intensity, durationSecs),
|
||||
}
|
||||
|
||||
if err := a.templates.ExecuteTemplate(w, "index.html", data); err != nil {
|
||||
@@ -144,14 +158,21 @@ func (a *App) handleHealth(w http.ResponseWriter, _ *http.Request) {
|
||||
}
|
||||
|
||||
func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
results := a.store.SearchSubmissions(r.URL.Query().Get("text"), r.URL.Query().Get("cpu"))
|
||||
results := a.store.SearchSubmissions(
|
||||
r.URL.Query().Get("text"),
|
||||
r.URL.Query().Get("cpu"),
|
||||
r.URL.Query().Get("thread"),
|
||||
r.URL.Query().Get("platform"),
|
||||
parsePositiveInt(r.URL.Query().Get("intensity"), 0),
|
||||
parsePositiveInt(r.URL.Query().Get("durationSecs"), 0),
|
||||
)
|
||||
writeJSON(w, http.StatusOK, results)
|
||||
}
|
||||
|
||||
func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxSubmissionBytes)
|
||||
|
||||
result, submitter, err := parseSubmissionRequest(r)
|
||||
result, submitter, platform, err := parseSubmissionRequest(r)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
|
||||
return
|
||||
@@ -162,7 +183,13 @@ func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
submission, err := a.store.SaveSubmission(result, submitter)
|
||||
platform = model.NormalizePlatform(platform)
|
||||
if err := model.ValidatePlatform(platform); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
submission, err := a.store.SaveSubmission(result, submitter, platform)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errorResponse{Error: fmt.Sprintf("store submission: %v", err)})
|
||||
return
|
||||
@@ -172,15 +199,16 @@ func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
"success": true,
|
||||
"submissionID": submission.SubmissionID,
|
||||
"submitter": submission.Submitter,
|
||||
"platform": submission.Platform,
|
||||
"submittedAt": submission.SubmittedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, error) {
|
||||
func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, string, error) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil && contentType != "" {
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("parse content type: %w", err)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse content type: %w", err)
|
||||
}
|
||||
|
||||
switch mediaType {
|
||||
@@ -189,55 +217,60 @@ func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, err
|
||||
case "multipart/form-data":
|
||||
return parseMultipartSubmission(r)
|
||||
default:
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("unsupported content type %q", mediaType)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("unsupported content type %q", mediaType)
|
||||
}
|
||||
}
|
||||
|
||||
func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, error) {
|
||||
func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("read request body: %w", err)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("read request body: %w", err)
|
||||
}
|
||||
|
||||
submitter := firstNonEmpty(
|
||||
r.URL.Query().Get("submitter"),
|
||||
r.Header.Get("X-Submitter"),
|
||||
)
|
||||
platform := firstNonEmpty(
|
||||
r.URL.Query().Get("platform"),
|
||||
r.Header.Get("X-Platform"),
|
||||
)
|
||||
|
||||
var nested jsonSubmissionEnvelope
|
||||
if err := json.Unmarshal(body, &nested); err == nil {
|
||||
submitter = firstNonEmpty(nested.Submitter, submitter)
|
||||
platform = firstNonEmpty(nested.Platform, platform)
|
||||
for _, candidate := range []*model.BenchmarkResult{nested.Benchmark, nested.Result, nested.Data} {
|
||||
if candidate != nil {
|
||||
return *candidate, submitter, nil
|
||||
return *candidate, submitter, platform, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var flat flatSubmissionEnvelope
|
||||
if err := json.Unmarshal(body, &flat); err != nil {
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("decode benchmark JSON: %w", err)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err)
|
||||
}
|
||||
|
||||
return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), nil
|
||||
return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), firstNonEmpty(flat.Platform, platform, "windows"), nil
|
||||
}
|
||||
|
||||
func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, error) {
|
||||
func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) {
|
||||
if err := r.ParseMultipartForm(maxSubmissionBytes); err != nil {
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("parse multipart form: %w", err)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse multipart form: %w", err)
|
||||
}
|
||||
|
||||
payload, err := readMultipartPayload(r)
|
||||
if err != nil {
|
||||
return model.BenchmarkResult{}, "", err
|
||||
return model.BenchmarkResult{}, "", "", err
|
||||
}
|
||||
|
||||
var result model.BenchmarkResult
|
||||
if err := json.Unmarshal(payload, &result); err != nil {
|
||||
return model.BenchmarkResult{}, "", fmt.Errorf("decode benchmark JSON: %w", err)
|
||||
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err)
|
||||
}
|
||||
|
||||
return result, r.FormValue("submitter"), nil
|
||||
return result, r.FormValue("submitter"), firstNonEmpty(r.FormValue("platform"), "windows"), nil
|
||||
}
|
||||
|
||||
func readMultipartPayload(r *http.Request) ([]byte, error) {
|
||||
@@ -326,7 +359,7 @@ func visibleBounds(page, pageSize, visibleCount, total int) (int, int) {
|
||||
return from, to
|
||||
}
|
||||
|
||||
func buildIndexURL(page int, text, cpu string) string {
|
||||
func buildIndexURL(page int, text, cpu, thread, platform string, intensity, durationSecs int) string {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
@@ -339,6 +372,18 @@ func buildIndexURL(page int, text, cpu string) string {
|
||||
if strings.TrimSpace(cpu) != "" {
|
||||
values.Set("cpu", cpu)
|
||||
}
|
||||
if normalizedThread := normalizeThreadFilter(thread); normalizedThread != "" {
|
||||
values.Set("thread", normalizedThread)
|
||||
}
|
||||
if normalizedPlatform := normalizePlatformFilter(platform); normalizedPlatform != "" {
|
||||
values.Set("platform", normalizedPlatform)
|
||||
}
|
||||
if intensity > 0 {
|
||||
values.Set("intensity", strconv.Itoa(intensity))
|
||||
}
|
||||
if durationSecs > 0 {
|
||||
values.Set("durationSecs", strconv.Itoa(durationSecs))
|
||||
}
|
||||
|
||||
return "/?" + values.Encode()
|
||||
}
|
||||
@@ -361,6 +406,30 @@ func firstNonEmpty(values ...string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func normalizeThreadFilter(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "single":
|
||||
return "single"
|
||||
case "multi":
|
||||
return "multi"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func normalizePlatformFilter(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "windows":
|
||||
return "windows"
|
||||
case "linux":
|
||||
return "linux"
|
||||
case "macos":
|
||||
return "macos"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func formatInt64(value int64) string {
|
||||
negative := value < 0
|
||||
if negative {
|
||||
|
||||
Reference in New Issue
Block a user