From f5e4c4f9727a400f2dac068b7a074ca463838959 Mon Sep 17 00:00:00 2001 From: Jon Lundy Date: Sat, 18 Mar 2023 19:55:24 -0600 Subject: [PATCH] fix(peers): fixing sort on results --- app/peerfinder/ev-request.go | 6 ++- app/peerfinder/http.go | 43 ++++++++++++++------ app/peerfinder/pages/home.go.tpl | 4 +- app/peerfinder/pages/req.go.tpl | 70 ++++++++++++++++---------------- 4 files changed, 72 insertions(+), 51 deletions(-) diff --git a/app/peerfinder/ev-request.go b/app/peerfinder/ev-request.go index 1f6b679..8a04870 100644 --- a/app/peerfinder/ev-request.go +++ b/app/peerfinder/ev-request.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "math" "net/netip" "strconv" "time" @@ -106,10 +107,11 @@ func (lis ListResponse) Len() int { return len(lis) } func (lis ListResponse) Less(i, j int) bool { - if lis[i].Latency == 0.0 { + if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { return false } - return lis[i].Latency < lis[j].Latency + + return lis[j].Latency >= lis[i].Latency } func (lis ListResponse) Swap(i, j int) { lis[i], lis[j] = lis[j], lis[i] diff --git a/app/peerfinder/http.go b/app/peerfinder/http.go index b7d6218..b8d007f 100644 --- a/app/peerfinder/http.go +++ b/app/peerfinder/http.go @@ -4,11 +4,13 @@ import ( "context" "embed" "encoding/json" + "errors" "fmt" "html/template" "io" "io/fs" "log" + "math" "net" "net/http" "sort" @@ -91,7 +93,7 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { case strings.HasPrefix(r.URL.Path, "/peers/status"): s.state.Modify(r.Context(), func(ctx context.Context, state *state) error { for id, p := range state.peers { - fmt.Fprintln(w, "PEER: ", id[24:], " ", p.Owner) + fmt.Fprintln(w, "PEER:", id[24:], p.Owner, p.Name) } for id, rq := range state.requests { @@ -99,9 +101,6 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { for id, r := range rq.Responses { fmt.Fprintln(w, " RES: ", id, r.PeerID[24:], r.Latency, r.Jitter) } - for p := range rq.peers { - fmt.Fprintln(w, " PEER: ", p[24:]) - } } return nil @@ -172,7 +171,7 @@ func (s *service) getPending(w http.ResponseWriter, r *http.Request, peerID stri peerResults := &PeerResults{} peerResults.SetStreamID(aggPeer(peerID)) err = s.es.Load(ctx, peerResults) - if err != nil { + if err != nil && !errors.Is(err, ev.ErrNotFound) { span.RecordError(fmt.Errorf("peer not found: %w", err)) w.WriteHeader(http.StatusNotFound) } @@ -546,7 +545,7 @@ type peer struct { Jitter float64 VPNTypes []string - Results []peerResult + Results peerResults } type listPeer []peer @@ -554,22 +553,36 @@ func (lis listPeer) Len() int { return len(lis) } func (lis listPeer) Less(i, j int) bool { - if lis[i].Latency == 0.0 { + if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { return false } - return lis[i].Latency < lis[j].Latency + return lis[j].Latency >= lis[i].Latency } func (lis listPeer) Swap(i, j int) { lis[i], lis[j] = lis[j], lis[i] } +type peerResults []peerResult + +func (lis peerResults) Len() int { + return len(lis) +} +func (lis peerResults) Less(i, j int) bool { + if diff := math.Abs(lis[i].Latency - 0.0); diff < 0.0001 { + return false + } + return lis[j].Latency >= lis[i].Latency +} +func (lis peerResults) Swap(i, j int) { + lis[i], lis[j] = lis[j], lis[i] +} + func fnOrderByPeer(rq *Request) any { peers := make(map[string]peer) - sort.Sort(ListResponse(rq.Responses)) - - for _, rs := range rq.Responses { + for i := range rq.Responses { + rs := rq.Responses[i] p, ok := peers[rs.Peer.Owner] if !ok { @@ -593,7 +606,13 @@ func fnOrderByPeer(rq *Request) any { } peerList := make(listPeer, 0, len(peers)) - for _, v := range peers { + for i := range peers { + v := peers[i] + sort.Sort(v.Results) + + v.Latency = v.Results[0].Latency + v.Jitter = v.Results[0].Jitter + peerList = append(peerList, v) } diff --git a/app/peerfinder/pages/home.go.tpl b/app/peerfinder/pages/home.go.tpl index b7072ac..cc3d516 100644 --- a/app/peerfinder/pages/home.go.tpl +++ b/app/peerfinder/pages/home.go.tpl @@ -45,7 +45,8 @@

Results

{{ with $args := . }} - {{range $req := .Requests}} + {{ range $req := .Requests }} + {{ if ne $req.RequestID "" }} {{end}} diff --git a/app/peerfinder/pages/req.go.tpl b/app/peerfinder/pages/req.go.tpl index f3f1289..08dcfd2 100644 --- a/app/peerfinder/pages/req.go.tpl +++ b/app/peerfinder/pages/req.go.tpl @@ -9,44 +9,42 @@ {{range .Requests}}

Results to {{.RequestIP}}{{if .Hidden}} 👁️{{end}}

- {{with (orderByPeer .)}} - {{range .}} -
- -
- Note: {{.Note}}
- VPN Types: {{range .VPNTypes}} {{.}} {{end}}
- IRC: {{.Nick}} -

Other Results

- - - - - - - - - - - - {{range .Results}} - - - - - - - {{end}} - -
Peer NameCountryLatencyJitter
{{.Name}}{{.Country}}{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}{{ if eq .Jitter 0.0 }}—{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}
+ {{range orderByPeer .}} +
+ - {{end}} +
+ Note: {{.Note}}
+ VPN Types: {{range .VPNTypes}} {{.}} {{end}}
+ IRC: {{.Nick}} +

Other Results

+ + + + + + + + + + + + {{range .Results}} + + + + + + + {{end}} + +
Peer NameCountryLatencyJitter
{{.Name}}{{.Country}}{{ if eq .Latency 0.0 }}—{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}{{ if eq .Jitter 0.0 }}—{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}
+
+
{{end}} {{end}} {{end}} \ No newline at end of file