1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/backend/api_endpoints_search.go

114 lines
2.6 KiB
Go
Raw Normal View History

2018-09-22 15:51:20 +02:00
package main
import (
"github.com/alice-lg/alice-lg/backend/api"
"github.com/julienschmidt/httprouter"
"net/http"
2018-09-24 22:36:23 +02:00
"sort"
2018-09-22 15:51:20 +02:00
"time"
)
// Handle global lookup
2018-09-24 22:29:13 +02:00
func apiLookupPrefixGlobal(
req *http.Request,
params httprouter.Params,
) (api.Response, error) {
// TODO: This function is too long
2018-09-22 15:51:20 +02:00
// Get prefix to query
q, err := validateQueryString(req, "q")
if err != nil {
return nil, err
}
q, err = validatePrefixQuery(q)
if err != nil {
return nil, err
}
// Check what we want to query
// Prefix -> fetch prefix
// _ -> fetch neighbours and routes
lookupPrefix := MaybePrefix(q)
// Measure response time
t0 := time.Now()
// Perform query
var routes api.LookupRoutes
if lookupPrefix {
routes = AliceRoutesStore.LookupPrefix(q)
} else {
neighbours := AliceNeighboursStore.LookupNeighbours(q)
routes = AliceRoutesStore.LookupPrefixForNeighbours(neighbours)
}
2018-09-24 22:29:13 +02:00
// Split routes
// TODO: Refactor at neighbors store
totalResults := len(routes)
imported := make(api.LookupRoutes, 0, totalResults)
filtered := make(api.LookupRoutes, 0, totalResults)
// Now, as we have allocated even more space, split routes
for _, r := range routes {
switch r.State {
case "filtered":
filtered = append(filtered, r)
break
case "imported":
imported = append(imported, r)
break
}
2018-09-22 15:51:20 +02:00
}
2018-09-24 22:36:23 +02:00
// Homogenize results
sort.Sort(imported)
sort.Sort(filtered)
2018-09-24 22:29:13 +02:00
// Paginate results
pageImported := apiQueryMustInt(req, "page_imported", 0)
pageSizeImported := AliceConfig.Ui.Pagination.RoutesAcceptedPageSize
routesImported, paginationImported := apiPaginateLookupRoutes(
imported, pageImported, pageSizeImported,
)
2018-09-22 15:51:20 +02:00
2018-09-24 22:29:13 +02:00
pageFiltered := apiQueryMustInt(req, "page_filtered", 0)
pageSizeFiltered := AliceConfig.Ui.Pagination.RoutesFilteredPageSize
routesFiltered, paginationFiltered := apiPaginateLookupRoutes(
filtered, pageFiltered, pageSizeFiltered,
)
2018-09-25 21:47:30 +02:00
// Calculate query duration
2018-09-24 22:29:13 +02:00
queryDuration := time.Since(t0)
2018-09-22 15:51:20 +02:00
2018-09-24 22:29:13 +02:00
// Make response
response := api.PaginatedRoutesLookupResponse{
2018-09-25 21:26:11 +02:00
Api: api.ApiStatus{
CacheStatus: api.CacheStatus{
CachedAt: AliceRoutesStore.CachedAt(),
},
ResultFromCache: true, // Well.
Ttl: AliceRoutesStore.CacheTtl(),
},
TimedResponse: api.TimedResponse{
2018-09-24 22:29:13 +02:00
RequestDuration: DurationMs(queryDuration),
},
Imported: &api.LookupRoutesResponse{
Routes: routesImported,
PaginatedResponse: &api.PaginatedResponse{
Pagination: paginationImported,
},
},
Filtered: &api.LookupRoutesResponse{
Routes: routesFiltered,
PaginatedResponse: &api.PaginatedResponse{
Pagination: paginationFiltered,
},
},
2018-09-22 15:51:20 +02:00
}
return response, nil
}