feat(search): add configurable sorting for submission results
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m17s
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m17s
Introduce a `sort` query parameter in submission search paths and pass it into store search logic. Matching results can now be ordered by `newest`, `oldest`, `score_desc`, `score_asc`, `mops_desc`, or `mops_asc`, with invalid values safely defaulting to `newest`. Update README and HTTP examples to document the new sort behavior and usage so clients can control result ordering server-side without extra post-processing.feat(search): add configurable sorting for submission results Introduce a `sort` query parameter in submission search paths and pass it into store search logic. Matching results can now be ordered by `newest`, `oldest`, `score_desc`, `score_asc`, `mops_desc`, or `mops_asc`, with invalid values safely defaulting to `newest`. Update README and HTTP examples to document the new sort behavior and usage so clients can control result ordering server-side without extra post-processing.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -116,11 +117,12 @@ func (s *Store) ListSubmissions(page, pageSize int) ([]model.Submission, int) {
|
||||
return results, total
|
||||
}
|
||||
|
||||
func (s *Store) SearchSubmissions(text, cpu, thread, platform string, intensity, durationSecs int) []model.Submission {
|
||||
func (s *Store) SearchSubmissions(text, cpu, thread, platform, sortBy string, intensity, durationSecs int) []model.Submission {
|
||||
queryText := normalizeSearchText(text)
|
||||
cpuText := normalizeSearchText(cpu)
|
||||
thread = normalizeThreadFilter(thread)
|
||||
platform = normalizePlatformFilter(platform)
|
||||
sortBy = normalizeSortOption(sortBy)
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
@@ -159,6 +161,7 @@ func (s *Store) SearchSubmissions(text, cpu, thread, platform string, intensity,
|
||||
results = append(results, *model.CloneSubmission(record.submission))
|
||||
}
|
||||
|
||||
sortSubmissions(results, sortBy)
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -296,6 +299,70 @@ func normalizePlatformFilter(value string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeSortOption(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "", "newest":
|
||||
return "newest"
|
||||
case "oldest":
|
||||
return "oldest"
|
||||
case "score_desc":
|
||||
return "score_desc"
|
||||
case "score_asc":
|
||||
return "score_asc"
|
||||
case "mops_desc":
|
||||
return "mops_desc"
|
||||
case "mops_asc":
|
||||
return "mops_asc"
|
||||
default:
|
||||
return "newest"
|
||||
}
|
||||
}
|
||||
|
||||
func sortSubmissions(submissions []model.Submission, sortBy string) {
|
||||
switch sortBy {
|
||||
case "oldest":
|
||||
sort.SliceStable(submissions, func(i, j int) bool {
|
||||
if submissions[i].SubmittedAt.Equal(submissions[j].SubmittedAt) {
|
||||
return submissions[i].SubmissionID < submissions[j].SubmissionID
|
||||
}
|
||||
|
||||
return submissions[i].SubmittedAt.Before(submissions[j].SubmittedAt)
|
||||
})
|
||||
case "score_desc":
|
||||
sort.SliceStable(submissions, func(i, j int) bool {
|
||||
if submissions[i].Score == submissions[j].Score {
|
||||
return submissions[i].SubmittedAt.After(submissions[j].SubmittedAt)
|
||||
}
|
||||
|
||||
return submissions[i].Score > submissions[j].Score
|
||||
})
|
||||
case "score_asc":
|
||||
sort.SliceStable(submissions, func(i, j int) bool {
|
||||
if submissions[i].Score == submissions[j].Score {
|
||||
return submissions[i].SubmittedAt.After(submissions[j].SubmittedAt)
|
||||
}
|
||||
|
||||
return submissions[i].Score < submissions[j].Score
|
||||
})
|
||||
case "mops_desc":
|
||||
sort.SliceStable(submissions, func(i, j int) bool {
|
||||
if submissions[i].MOpsPerSec == submissions[j].MOpsPerSec {
|
||||
return submissions[i].SubmittedAt.After(submissions[j].SubmittedAt)
|
||||
}
|
||||
|
||||
return submissions[i].MOpsPerSec > submissions[j].MOpsPerSec
|
||||
})
|
||||
case "mops_asc":
|
||||
sort.SliceStable(submissions, func(i, j int) bool {
|
||||
if submissions[i].MOpsPerSec == submissions[j].MOpsPerSec {
|
||||
return submissions[i].SubmittedAt.After(submissions[j].SubmittedAt)
|
||||
}
|
||||
|
||||
return submissions[i].MOpsPerSec < submissions[j].MOpsPerSec
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func pageBounds(page, pageSize, total int) (int, int, int) {
|
||||
if pageSize <= 0 {
|
||||
pageSize = 50
|
||||
|
||||
@@ -33,6 +33,7 @@ type indexPageData struct {
|
||||
QueryCPU string
|
||||
QueryThread string
|
||||
QueryPlatform string
|
||||
QuerySort string
|
||||
QueryIntensity int
|
||||
QueryDuration int
|
||||
Page int
|
||||
@@ -104,6 +105,7 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
cpu := strings.TrimSpace(r.URL.Query().Get("cpu"))
|
||||
thread := strings.TrimSpace(r.URL.Query().Get("thread"))
|
||||
platform := strings.TrimSpace(r.URL.Query().Get("platform"))
|
||||
sortBy := normalizeSortFilter(r.URL.Query().Get("sort"))
|
||||
intensity := parsePositiveInt(r.URL.Query().Get("intensity"), 0)
|
||||
durationSecs := parsePositiveInt(r.URL.Query().Get("durationSecs"), 0)
|
||||
|
||||
@@ -112,8 +114,8 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
totalCount int
|
||||
)
|
||||
|
||||
if text != "" || cpu != "" || thread != "" || platform != "" || intensity > 0 || durationSecs > 0 {
|
||||
matches := a.store.SearchSubmissions(text, cpu, thread, platform, intensity, durationSecs)
|
||||
if text != "" || cpu != "" || thread != "" || platform != "" || intensity > 0 || durationSecs > 0 || sortBy != "newest" {
|
||||
matches := a.store.SearchSubmissions(text, cpu, thread, platform, sortBy, intensity, durationSecs)
|
||||
totalCount = len(matches)
|
||||
start, end, normalizedPage := pageBounds(page, a.pageSize, totalCount)
|
||||
page = normalizedPage
|
||||
@@ -134,6 +136,7 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
QueryCPU: cpu,
|
||||
QueryThread: normalizeThreadFilter(thread),
|
||||
QueryPlatform: normalizePlatformFilter(platform),
|
||||
QuerySort: sortBy,
|
||||
QueryIntensity: intensity,
|
||||
QueryDuration: durationSecs,
|
||||
Page: page,
|
||||
@@ -141,8 +144,8 @@ func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
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),
|
||||
PrevURL: buildIndexURL(max(1, page-1), text, cpu, thread, platform, sortBy, intensity, durationSecs),
|
||||
NextURL: buildIndexURL(max(1, min(totalPageCount, page+1)), text, cpu, thread, platform, sortBy, intensity, durationSecs),
|
||||
}
|
||||
|
||||
if err := a.templates.ExecuteTemplate(w, "index.html", data); err != nil {
|
||||
@@ -163,6 +166,7 @@ func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Query().Get("cpu"),
|
||||
r.URL.Query().Get("thread"),
|
||||
r.URL.Query().Get("platform"),
|
||||
r.URL.Query().Get("sort"),
|
||||
parsePositiveInt(r.URL.Query().Get("intensity"), 0),
|
||||
parsePositiveInt(r.URL.Query().Get("durationSecs"), 0),
|
||||
)
|
||||
@@ -359,7 +363,7 @@ func visibleBounds(page, pageSize, visibleCount, total int) (int, int) {
|
||||
return from, to
|
||||
}
|
||||
|
||||
func buildIndexURL(page int, text, cpu, thread, platform string, intensity, durationSecs int) string {
|
||||
func buildIndexURL(page int, text, cpu, thread, platform, sortBy string, intensity, durationSecs int) string {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
@@ -378,6 +382,9 @@ func buildIndexURL(page int, text, cpu, thread, platform string, intensity, dura
|
||||
if normalizedPlatform := normalizePlatformFilter(platform); normalizedPlatform != "" {
|
||||
values.Set("platform", normalizedPlatform)
|
||||
}
|
||||
if normalizedSort := normalizeSortFilter(sortBy); normalizedSort != "newest" {
|
||||
values.Set("sort", normalizedSort)
|
||||
}
|
||||
if intensity > 0 {
|
||||
values.Set("intensity", strconv.Itoa(intensity))
|
||||
}
|
||||
@@ -430,6 +437,25 @@ func normalizePlatformFilter(value string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeSortFilter(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "", "newest":
|
||||
return "newest"
|
||||
case "oldest":
|
||||
return "oldest"
|
||||
case "score_desc":
|
||||
return "score_desc"
|
||||
case "score_asc":
|
||||
return "score_asc"
|
||||
case "mops_desc":
|
||||
return "mops_desc"
|
||||
case "mops_asc":
|
||||
return "mops_asc"
|
||||
default:
|
||||
return "newest"
|
||||
}
|
||||
}
|
||||
|
||||
func formatInt64(value int64) string {
|
||||
negative := value < 0
|
||||
if negative {
|
||||
|
||||
Reference in New Issue
Block a user