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/pkg/backend/api_endpoints_search.go

162 lines
3.8 KiB
Go
Raw Normal View History

2021-03-22 16:25:47 +01:00
package backend
2018-09-22 15:51:20 +02:00
import (
"net/http"
2018-09-24 22:36:23 +02:00
"sort"
2018-09-22 15:51:20 +02:00
"time"
2021-03-22 16:50:08 +01:00
"github.com/julienschmidt/httprouter"
"github.com/alice-lg/alice-lg/pkg/api"
2018-09-22 15:51:20 +02:00
)
// Handle global lookup
2018-09-24 22:29:13 +02:00
func apiLookupPrefixGlobal(
req *http.Request,
params httprouter.Params,
) (api.Response, error) {
2019-10-07 18:13:08 +02:00
// TODO: This function is way too long
2018-09-24 22:29:13 +02:00
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()
2018-10-15 10:55:27 +02:00
// Get additional filter criteria
filtersApplied, err := api.FiltersFromQuery(req.URL.Query())
2018-10-17 15:57:20 +02:00
if err != nil {
return nil, err
}
2018-10-15 10:55:27 +02:00
2018-09-22 15:51:20 +02:00
// 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)
2018-10-16 18:27:54 +02:00
// Now, as we have allocated even more space process routes by, splitting,
// filtering and updating the available filters...
2018-10-17 10:44:38 +02:00
filtersAvailable := api.NewSearchFilters()
2018-09-24 22:29:13 +02:00
for _, r := range routes {
2018-10-17 15:57:20 +02:00
if !filtersApplied.MatchRoute(r) {
2018-10-17 15:57:20 +02:00
continue // Exclude route from results set
}
2018-09-24 22:29:13 +02:00
switch r.State {
case "filtered":
filtered = append(filtered, r)
break
case "imported":
imported = append(imported, r)
break
}
2018-10-17 10:44:38 +02:00
filtersAvailable.UpdateFromLookupRoute(r)
2018-09-22 15:51:20 +02:00
}
// Remove applied filters from available
filtersApplied.MergeProperties(filtersAvailable)
filtersAvailable = filtersAvailable.Sub(filtersApplied)
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.
2021-03-22 17:35:20 +01:00
Ttl: AliceRoutesStore.CacheTTL(),
2018-09-25 21:26:11 +02:00
},
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-10-17 10:44:38 +02:00
FilterableResponse: api.FilterableResponse{
2018-10-18 15:02:34 +02:00
FiltersAvailable: filtersAvailable,
FiltersApplied: filtersApplied,
2018-10-17 10:44:38 +02:00
},
2018-09-22 15:51:20 +02:00
}
return response, nil
}
2019-10-05 12:10:27 +02:00
2019-10-07 18:13:08 +02:00
func apiLookupNeighborsGlobal(
2019-10-05 12:10:27 +02:00
req *http.Request,
params httprouter.Params,
) (api.Response, error) {
2019-10-07 18:29:25 +02:00
// Query neighbors store
filter := api.NeighborFilterFromQuery(req.URL.Query())
neighbors := AliceNeighboursStore.FilterNeighbors(filter)
2019-10-07 23:22:01 +02:00
sort.Sort(neighbors)
2019-10-07 18:29:25 +02:00
// Make response
response := &api.NeighboursResponse{
Api: api.ApiStatus{
CacheStatus: api.CacheStatus{
CachedAt: AliceNeighboursStore.CachedAt(),
},
ResultFromCache: true, // You would not have guessed.
2021-03-22 17:35:20 +01:00
Ttl: AliceNeighboursStore.CacheTTL(),
2019-10-07 18:29:25 +02:00
},
Neighbours: neighbors,
}
return response, nil
2019-10-05 12:10:27 +02:00
}