Fix prefix length page to show unique prefixes only
- Change GetRandomPrefixesByLength to return unique prefixes instead of all routes - Use CTE to first select random unique prefixes, then join to get their latest route info - This ensures each prefix appears only once in the list
This commit is contained in:
parent
9a63553f8d
commit
5fb3fc0381
@ -1230,17 +1230,24 @@ func (d *Database) GetPrefixDetails(prefix string) ([]LiveRoute, error) {
|
||||
|
||||
// GetRandomPrefixesByLength returns a random sample of prefixes with the specified mask length
|
||||
func (d *Database) GetRandomPrefixesByLength(maskLength, ipVersion, limit int) ([]LiveRoute, error) {
|
||||
// Select unique prefixes with their most recent route information
|
||||
query := `
|
||||
SELECT DISTINCT
|
||||
prefix, mask_length, ip_version, origin_asn, as_path,
|
||||
peer_ip, last_updated
|
||||
FROM live_routes
|
||||
WHERE mask_length = ? AND ip_version = ?
|
||||
ORDER BY RANDOM()
|
||||
LIMIT ?
|
||||
WITH unique_prefixes AS (
|
||||
SELECT prefix, MAX(last_updated) as max_updated
|
||||
FROM live_routes
|
||||
WHERE mask_length = ? AND ip_version = ?
|
||||
GROUP BY prefix
|
||||
ORDER BY RANDOM()
|
||||
LIMIT ?
|
||||
)
|
||||
SELECT lr.prefix, lr.mask_length, lr.ip_version, lr.origin_asn, lr.as_path,
|
||||
lr.peer_ip, lr.last_updated
|
||||
FROM live_routes lr
|
||||
INNER JOIN unique_prefixes up ON lr.prefix = up.prefix AND lr.last_updated = up.max_updated
|
||||
WHERE lr.mask_length = ? AND lr.ip_version = ?
|
||||
`
|
||||
|
||||
rows, err := d.db.Query(query, maskLength, ipVersion, limit)
|
||||
rows, err := d.db.Query(query, maskLength, ipVersion, limit, maskLength, ipVersion)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query random prefixes: %w", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user