fix(peers): fixing sort on results for real

This commit is contained in:
Jon Lundy 2023-03-19 07:52:27 -06:00
parent 8ba2846e62
commit d27eb21ffb
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
4 changed files with 77 additions and 36 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"net/netip" "net/netip"
"strconv" "strconv"
"time" "time"
@ -107,7 +106,10 @@ func (lis ListResponse) Len() int {
return len(lis) return len(lis)
} }
func (lis ListResponse) Less(i, j int) bool { func (lis ListResponse) Less(i, j int) bool {
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
return true
}
if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
return false return false
} }

View File

@ -10,7 +10,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"log" "log"
"math"
"net" "net"
"net/http" "net/http"
"sort" "sort"
@ -91,21 +90,54 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
case strings.HasPrefix(r.URL.Path, "/peers/status"): case strings.HasPrefix(r.URL.Path, "/peers/status"):
var pickID string
if strings.HasPrefix(r.URL.Path, "/peers/status/") {
pickID = strings.TrimPrefix(r.URL.Path, "/peers/status/")
}
var requests []*Request
s.state.Modify(r.Context(), func(ctx context.Context, state *state) error { s.state.Modify(r.Context(), func(ctx context.Context, state *state) error {
for id, p := range state.peers { for id, p := range state.peers {
fmt.Fprintln(w, "PEER:", id[24:], p.Owner, p.Name) fmt.Fprintln(w, "PEER:", id[24:], p.Owner, p.Name)
} }
for id, rq := range state.requests { if pickID != "" {
fmt.Fprintln(w, "REQ: ", id, rq.RequestIP, len(rq.Responses)) if rq, ok := state.requests[pickID]; ok {
for id, r := range rq.Responses { requests = append(requests, rq)
fmt.Fprintln(w, " RES: ", id, r.PeerID[24:], r.Latency, r.Jitter) }
} else {
requests = make([]*Request, 0, len(state.requests))
for i := range state.requests {
rq := state.requests[i]
requests = append(requests, rq)
}
}
for i := range requests {
rq := requests[i]
for i := range rq.Responses {
res := rq.Responses[i]
if peer, ok := state.peers[res.PeerID]; ok {
res.Peer = peer
res.Peer.ID = ""
}
} }
} }
return nil return nil
}) })
for i, rq := range requests {
fmt.Fprintln(w, "REQ: ", i, rq.RequestIP, len(rq.Responses))
for i, peer := range fnOrderByPeer(rq) {
fmt.Fprintln(w, " PEER: ", i, peer.RequestID, peer.Name, peer.Latency, peer.Jitter)
for i, res := range peer.Results {
fmt.Fprintln(w, " RES: ", i, res.RequestID, res.Latency, res.Jitter)
}
}
}
default: default:
s.getResults(w, r) s.getResults(w, r)
return return
@ -531,19 +563,21 @@ var funcMap = map[string]any{
} }
type peerResult struct { type peerResult struct {
Name string RequestID string
Country string Name string
Latency float64 Country string
Jitter float64 Latency float64
Jitter float64
} }
type peer struct { type peer struct {
Name string RequestID string
Note string Name string
Nick string Note string
Country string Nick string
Latency float64 Country string
Jitter float64 Latency float64
VPNTypes []string Jitter float64
VPNTypes []string
Results peerResults Results peerResults
} }
@ -553,10 +587,14 @@ func (lis listPeer) Len() int {
return len(lis) return len(lis)
} }
func (lis listPeer) Less(i, j int) bool { func (lis listPeer) Less(i, j int) bool {
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
return true
}
if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
return false return false
} }
return lis[j].Latency >= lis[i].Latency
return lis[i].Latency < lis[j].Latency
} }
func (lis listPeer) Swap(i, j int) { func (lis listPeer) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i] lis[i], lis[j] = lis[j], lis[i]
@ -568,24 +606,31 @@ func (lis peerResults) Len() int {
return len(lis) return len(lis)
} }
func (lis peerResults) Less(i, j int) bool { func (lis peerResults) Less(i, j int) bool {
if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { if lis[j].Latency == 0.0 && lis[i].Latency > 0.0 {
return true
}
if lis[i].Latency == 0.0 && lis[j].Latency > 0.0 {
return false return false
} }
return lis[j].Latency >= lis[i].Latency
return lis[i].Latency < lis[j].Latency
} }
func (lis peerResults) Swap(i, j int) { func (lis peerResults) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i] lis[i], lis[j] = lis[j], lis[i]
} }
func fnOrderByPeer(rq *Request) any { func fnOrderByPeer(rq *Request) listPeer {
peers := make(map[string]peer) peers := make(map[string]peer)
for i := range rq.Responses { for i := range rq.Responses {
if rq.Responses[i] == nil || rq.Responses[i].Peer == nil {
continue
}
rs := rq.Responses[i] rs := rq.Responses[i]
p, ok := peers[rs.Peer.Owner] p, ok := peers[rs.Peer.Owner]
if !ok { if !ok {
p.RequestID = rq.RequestID
p.Country = rs.Peer.Country p.Country = rs.Peer.Country
p.Name = rs.Peer.Name p.Name = rs.Peer.Name
p.Nick = rs.Peer.Nick p.Nick = rs.Peer.Nick
@ -596,10 +641,11 @@ func fnOrderByPeer(rq *Request) any {
} }
p.Results = append(p.Results, peerResult{ p.Results = append(p.Results, peerResult{
Name: rs.Peer.Name, RequestID: rq.RequestID,
Country: rs.Peer.Country, Name: rs.Peer.Name,
Latency: rs.Latency, Country: rs.Peer.Country,
Jitter: rs.Jitter, Latency: rs.Latency,
Jitter: rs.Jitter,
}) })
peers[rs.Peer.Owner] = p peers[rs.Peer.Owner] = p
@ -617,7 +663,6 @@ func fnOrderByPeer(rq *Request) any {
} }
sort.Sort(peerList) sort.Sort(peerList)
return peerList return peerList
} }
func fnCountResponses(rq *Request) int { func fnCountResponses(rq *Request) int {

1
go.mod
View File

@ -29,7 +29,6 @@ require github.com/patrickmn/go-cache v2.1.0+incompatible
require ( require (
github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/taigrr/go-colorhash v0.0.0-20220329080504-742db7f45eae // indirect github.com/taigrr/go-colorhash v0.0.0-20220329080504-742db7f45eae // indirect
golang.org/dl v0.0.0-20230112164944-626922ede476 // indirect
) )
require ( require (

5
go.sum
View File

@ -69,7 +69,6 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
@ -424,8 +423,6 @@ gitlab.com/jamietanna/content-negotiation-go v0.2.0 h1:vT0OLEPQ6DYRG3/1F7joXSNjV
gitlab.com/jamietanna/content-negotiation-go v0.2.0/go.mod h1:n4ZZ8/X5TstnjYRnjEtR/fC7MCTe+aRKM7PQlLBH3PQ= gitlab.com/jamietanna/content-negotiation-go v0.2.0/go.mod h1:n4ZZ8/X5TstnjYRnjEtR/fC7MCTe+aRKM7PQlLBH3PQ=
go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa h1:KBxzYJMWP7MXd72RgqsMCGOSEqV6aaDDSdSb8usJCzQ= go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa h1:KBxzYJMWP7MXd72RgqsMCGOSEqV6aaDDSdSb8usJCzQ=
go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa/go.mod h1:bQ9yvK7wwThD4tzoioJq/YAuwYOB2XA9tAUHIYtjre8= go.mills.io/salty v0.0.0-20220322161301-ce2b9f6573fa/go.mod h1:bQ9yvK7wwThD4tzoioJq/YAuwYOB2XA9tAUHIYtjre8=
go.mills.io/salty v0.0.0-20230121154700-b5dab41cf9e7 h1:T6kxzIrbniu7CzRF3tB2sfx5etCWlgktwh/9gryZ1rc=
go.mills.io/salty v0.0.0-20230121154700-b5dab41cf9e7/go.mod h1:bQ9yvK7wwThD4tzoioJq/YAuwYOB2XA9tAUHIYtjre8=
go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829 h1:rzgfYKbCt8N0vVD3CAMoPwtvj4Zr1l3Cyl3rjN4+kHg= go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829 h1:rzgfYKbCt8N0vVD3CAMoPwtvj4Zr1l3Cyl3rjN4+kHg=
go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829/go.mod h1:ldLxf9b9mfq3QMHXenH42tvkUGJ0UlSQ/QUoTKvefs8= go.mills.io/saltyim v0.0.0-20230128070719-15a64de82829/go.mod h1:ldLxf9b9mfq3QMHXenH42tvkUGJ0UlSQ/QUoTKvefs8=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
@ -470,8 +467,6 @@ go.yarn.social/lextwt v0.0.0-20221221200320-31bca76a2587/go.mod h1:XcveSVLWxkBBW
go.yarn.social/types v0.0.0-20221025190911-9524f5b4a743/go.mod h1:XN+G4HprNn/Gp7OF2zveqsCRSWFCHtOaIRh2GlcK+U4= go.yarn.social/types v0.0.0-20221025190911-9524f5b4a743/go.mod h1:XN+G4HprNn/Gp7OF2zveqsCRSWFCHtOaIRh2GlcK+U4=
go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1 h1:H3W7HmWrVpHs7WcncxifE7lr9JUApKPGqZTWmIaU5F4= go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1 h1:H3W7HmWrVpHs7WcncxifE7lr9JUApKPGqZTWmIaU5F4=
go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1/go.mod h1:+xnDkQ0T0S8emxWIsvxlCAoyF8gBaj0q81hr/VrKc0c= go.yarn.social/types v0.0.0-20221027173319-2d00e96a95c1/go.mod h1:+xnDkQ0T0S8emxWIsvxlCAoyF8gBaj0q81hr/VrKc0c=
golang.org/dl v0.0.0-20230112164944-626922ede476 h1:Fajk6nhvJWnFeoRvOR9a6XyjMgpBIsxgwySoz7uJDU4=
golang.org/dl v0.0.0-20230112164944-626922ede476/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=