Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for manually setting a limit on results(copy) #464

Merged
merged 2 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
const (
defaultLinesOfContext uint = 2
maxLinesOfContext uint = 20
maxLimit int = 100000
)

type Stats struct {
Expand Down Expand Up @@ -128,11 +129,25 @@ func parseAsUintValue(sv string, min, max, def uint) uint {
return max
}
if min != 0 && uint(iv) < min {
return max
return min
}
return uint(iv)
}

func parseAsIntValue(sv string, min, max, def int) int {
iv, err := strconv.ParseInt(sv, 10, 64)
if err != nil {
return def
}
if max != 0 && int(iv) > max {
return max
}
if min != 0 && int(iv) < min {
return min
}
return int(iv)
}

func parseRangeInt(v string, i *int) {
*i = 0
if v == "" {
Expand All @@ -159,8 +174,7 @@ func parseRangeValue(rv string) (int, int) {
return b, e
}

func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {

func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher, defaultMaxResults int) {
m.HandleFunc("/api/v1/repos", func(w http.ResponseWriter, r *http.Request) {
res := map[string]*config.Repo{}
for name, srch := range idx {
Expand All @@ -181,6 +195,11 @@ func Setup(m *http.ServeMux, idx map[string]*searcher.Searcher) {
opt.ExcludeFileRegexp = r.FormValue("excludeFiles")
opt.IgnoreCase = parseAsBool(r.FormValue("i"))
opt.LiteralSearch = parseAsBool(r.FormValue("literal"))
opt.MaxResults = parseAsIntValue(
r.FormValue("limit"),
-1,
maxLimit,
defaultMaxResults)
opt.LinesOfContext = parseAsUintValue(
r.FormValue("ctx"),
0,
Expand Down
34 changes: 34 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import (
"testing"
)

var parseAsIntAndUintTests = map[string]struct {
numResults string
min int
max int
def int
expected int
} {
"parse error test case": {"not a parsable integer", 101, 1000, 5000, 5000},
"less than min test case": {"100", 101, 1000, 5000, 101},
"greater than max test case": {"1001", 101, 1000, 5000, 1000},
"within limits test case": {"100", 0, 100, 5000, 100},
}

func TestParseAsIntAndUint(t *testing.T) {
for name, td := range parseAsIntAndUintTests {
t.Run(name, func(t *testing.T) {
if got, expected :=
parseAsUintValue(td.numResults, uint(td.min), uint(td.max), uint(td.def)), uint(td.expected); got != expected {
t.Fatalf("parseAsUintValue - %s: returned %d; expected %d", name, got, expected)
}

if got, expected :=
parseAsIntValue(td.numResults, td.min, td.max, td.def), td.expected; got != expected {
t.Fatalf("parseAsIntValue - %s: returned %d; expected %d", name, got, expected)
}
})
}
}
2 changes: 1 addition & 1 deletion cmds/houndd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func runHttp( //nolint
}

m.Handle("/", h)
api.Setup(m, idx)
api.Setup(m, idx, cfg.ResultLimit)
return http.ListenAndServe(addr, m)
}

Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
defaultBaseUrl = "{url}/blob/{rev}/{path}{anchor}"
defaultAnchor = "#L{line}"
defaultHealthCheckURI = "/healthz"
defaultResultLimit = 5000
)

type UrlPattern struct {
Expand Down Expand Up @@ -63,6 +64,7 @@ type Config struct {
MaxConcurrentIndexers int `json:"max-concurrent-indexers"`
HealthCheckURI string `json:"health-check-uri"`
VCSConfigMessages map[string]*SecretMessage `json:"vcs-config"`
ResultLimit int `json:"result-limit"`
}

// SecretMessage is just like json.RawMessage but it will not
Expand Down Expand Up @@ -130,6 +132,10 @@ func initConfig(c *Config) error {
c.HealthCheckURI = defaultHealthCheckURI
}

if c.ResultLimit == 0 {
c.ResultLimit = defaultResultLimit
}

return mergeVCSConfigs(c)
}

Expand Down
Loading