added pagination to the lookup query
This commit is contained in:
parent
f115e44efe
commit
a7ac32c39b
@ -188,14 +188,32 @@ func apiLookupPrefixGlobal(req *http.Request, params httprouter.Params) (api.Res
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get pagination params
|
||||
limit, offset, err := validatePaginationParams(req, 100, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make response
|
||||
t0 := time.Now()
|
||||
routes := AliceRoutesStore.Lookup(prefix)
|
||||
|
||||
// Paginate result
|
||||
totalRoutes := len(routes)
|
||||
cap := offset + limit
|
||||
if cap > totalRoutes {
|
||||
cap = totalRoutes
|
||||
}
|
||||
|
||||
queryDuration := time.Since(t0)
|
||||
response := api.LookupResponseGlobal{
|
||||
Routes: routes,
|
||||
Time: float64(queryDuration) / 1000.0 / 1000.0, // nano -> micro -> milli
|
||||
Routes: routes[offset:cap],
|
||||
|
||||
TotalRoutes: totalRoutes,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
|
||||
Time: float64(queryDuration) / 1000.0 / 1000.0, // nano -> micro -> milli
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
@ -67,5 +67,24 @@ func validatePrefixQuery(value string) (string, error) {
|
||||
// Get pagination parameters: limit and offset
|
||||
// Refer to defaults if none are given.
|
||||
func validatePaginationParams(req *http.Request, limit, offset int) (int, int, error) {
|
||||
query := req.URL.Query()
|
||||
queryLimit, ok := query["limit"]
|
||||
if ok {
|
||||
limit, _ = strconv.Atoi(queryLimit[0])
|
||||
}
|
||||
|
||||
queryOffset, ok := query["offset"]
|
||||
if ok {
|
||||
offset, _ = strconv.Atoi(queryOffset[0])
|
||||
}
|
||||
|
||||
// Cap limit to [1, 1000]
|
||||
if limit < 1 {
|
||||
limit = 1
|
||||
}
|
||||
if limit > 1000 {
|
||||
limit = 1000
|
||||
}
|
||||
|
||||
return limit, offset, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user