Make the home page's AS name search work (closes #151)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The AS Name Search card on the home page said the search was coming soon.
It now asks the new GET /api/v1/search/as?q=<text> and lists the matches
as links to their AS pages. asinfo.Search ignores case, returns the first
matches up to a limit in AS number order, and counts all matches. An
empty q answers 400.
When the database has no record of an AS, its page and /api/v1/as/{asn}
take its handle and description from asinfo and still list its prefixes.
They answer 404 only for an AS with no record, no asinfo entry and no
prefix. AS 0 means no known origin: it always answers 404, and the search
never lists it.
Model: opus-5-5
This commit is contained in:
+49
-23
@@ -2,10 +2,13 @@ package asinfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"compress/gzip"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -23,6 +26,17 @@ type Info struct {
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
byASN map[int]*Info
|
||||
// searchEntries holds every entry in AS number order, for Search.
|
||||
searchEntries []searchEntry
|
||||
}
|
||||
|
||||
// searchEntry is an entry with its handle and description in lower case. They
|
||||
// are put in lower case once, when the data loads, so that Search only
|
||||
// compares.
|
||||
type searchEntry struct {
|
||||
info *Info
|
||||
lowerHandle string
|
||||
lowerDescription string
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -59,9 +73,21 @@ func initRegistry() {
|
||||
panic("failed to unmarshal embedded AS data: " + err.Error())
|
||||
}
|
||||
|
||||
slices.SortFunc(asInfos, func(a, b Info) int {
|
||||
return cmp.Compare(a.ASN, b.ASN)
|
||||
})
|
||||
|
||||
defaultRegistry.searchEntries = make([]searchEntry, 0, len(asInfos))
|
||||
|
||||
for i := range asInfos {
|
||||
info := &asInfos[i]
|
||||
defaultRegistry.byASN[info.ASN] = info
|
||||
defaultRegistry.searchEntries = append(defaultRegistry.searchEntries,
|
||||
searchEntry{
|
||||
info: info,
|
||||
lowerHandle: strings.ToLower(info.Handle),
|
||||
lowerDescription: strings.ToLower(info.Description),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,36 +155,36 @@ func All() []Info {
|
||||
return result
|
||||
}
|
||||
|
||||
// Search searches for AS entries where the handle or description contains the query
|
||||
// This is a simple case-sensitive substring search
|
||||
func Search(query string) []Info {
|
||||
// Search finds the AS entries whose handle or description contains query,
|
||||
// ignoring case. It returns the first limit of them in AS number order, and the
|
||||
// number of entries that match. An empty query matches nothing.
|
||||
func Search(query string, limit int) ([]Info, int) {
|
||||
once.Do(initRegistry)
|
||||
|
||||
if query == "" {
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
query = strings.ToLower(query)
|
||||
|
||||
defaultRegistry.mu.RLock()
|
||||
defer defaultRegistry.mu.RUnlock()
|
||||
|
||||
var results []Info
|
||||
var (
|
||||
results []Info
|
||||
total int
|
||||
)
|
||||
|
||||
for _, info := range defaultRegistry.byASN {
|
||||
if contains(info.Handle, query) || contains(info.Description, query) {
|
||||
results = append(results, *info)
|
||||
for _, entry := range defaultRegistry.searchEntries {
|
||||
if strings.Contains(entry.lowerHandle, query) ||
|
||||
strings.Contains(entry.lowerDescription, query) {
|
||||
if total < limit {
|
||||
results = append(results, *entry.info)
|
||||
}
|
||||
|
||||
total++
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// contains is a simple substring search
|
||||
func contains(s, substr string) bool {
|
||||
return len(substr) > 0 && len(s) >= len(substr) && containsImpl(s, substr)
|
||||
}
|
||||
|
||||
func containsImpl(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return results, total
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user